Skip to content

Commit 22aec32

Browse files
authored
InMemoryConnectionLog can now log to only memory (#675)
* InMemoryConnectionLog can now log to only memory * .
1 parent 670e010 commit 22aec32

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

source/Halibut/Diagnostics/InMemoryConnectionLog.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ namespace Halibut.Diagnostics
99
internal class InMemoryConnectionLog : ILog
1010
{
1111
readonly string endpoint;
12-
readonly Logging.ILog logger;
12+
readonly Logging.ILog? logger;
1313
readonly ConcurrentQueue<LogEvent> events = new();
1414

15+
/// <summary>
16+
/// Writes logs to an in memory queue of events as well as to the logger returned
17+
/// by LogProvider.GetLogger("Halibut")
18+
/// </summary>
19+
/// <param name="endpoint"></param>
1520
public InMemoryConnectionLog(string endpoint)
1621
{
1722
this.endpoint = endpoint;
1823
this.logger = LogProvider.GetLogger("Halibut");
1924
}
2025

21-
public InMemoryConnectionLog(string endpoint, Logging.ILog logger)
26+
/// <summary>
27+
///
28+
/// </summary>
29+
/// <param name="endpoint"></param>
30+
/// <param name="logger">When null, this will not write to the Logging.ILog</param>
31+
public InMemoryConnectionLog(string endpoint, Logging.ILog? logger)
2232
{
2333
this.endpoint = endpoint;
2434
this.logger = logger;
@@ -43,8 +53,7 @@ public IList<LogEvent> GetLogs()
4353

4454
void WriteInternal(LogEvent logEvent)
4555
{
46-
var logLevel = GetLogLevel(logEvent);
47-
SendToTrace(logEvent, logLevel);
56+
SendToTrace(logEvent);
4857

4958
events.Enqueue(logEvent);
5059

@@ -71,9 +80,13 @@ static LogLevel GetLogLevel(LogEvent logEvent)
7180
}
7281
}
7382

74-
void SendToTrace(LogEvent logEvent, LogLevel level)
83+
void SendToTrace(LogEvent logEvent)
7584
{
76-
logger.Log(level, () => "{0,-30} {1,4} {2}", logEvent.Error, endpoint, Thread.CurrentThread.ManagedThreadId, logEvent.FormattedMessage);
85+
if (logger != null)
86+
{
87+
var level = GetLogLevel(logEvent);
88+
logger.Log(level, () => "{0,-30} {1,4} {2}", logEvent.Error, endpoint, Thread.CurrentThread.ManagedThreadId, logEvent.FormattedMessage);
89+
}
7790
}
7891
}
7992
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using Halibut.Logging;
3+
4+
namespace Halibut.Diagnostics.LogCreators
5+
{
6+
/// <summary>
7+
/// Creates loggers that log to the in memory queue only and to no other place.
8+
/// </summary>
9+
public class InMemoryOnlyConnectionLogCreator : ICreateNewILog
10+
{
11+
public ILog CreateNewForPrefix(string prefix)
12+
{
13+
return new InMemoryConnectionLog(prefix, null);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)