Skip to content

Commit 7a22f6d

Browse files
committed
Add ForContext support to logging.
1 parent e82b0b9 commit 7a22f6d

7 files changed

Lines changed: 35 additions & 8 deletions

File tree

source/Halibut.Tests.DotMemory/Support/TestContextConnectionLog.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public IList<LogEvent> GetLogs()
3636
throw new NotImplementedException();
3737
}
3838

39+
public ILog ForContext<T>()
40+
{
41+
return this;
42+
}
43+
3944
void WriteInternal(LogEvent logEvent)
4045
{
4146
var logEventLogLevel = GetLogLevel(logEvent);

source/Halibut.Tests/Support/Logging/InMemoryLogWriter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public IList<LogEvent> GetLogs()
3030
{
3131
return events.ToArray();
3232
}
33+
34+
public ILog ForContext<T>()
35+
{
36+
return this;
37+
}
3338
}
3439
}

source/Halibut.Tests/Support/Logging/TestContextConnectionLog.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ public class TestContextConnectionLog : ILog, ILogWriter
1515
readonly string endpoint;
1616
readonly string name;
1717
readonly LogLevel logLevel;
18+
readonly Type? forContextType;
1819

19-
public TestContextConnectionLog(string endpoint, string name, LogLevel logLevel)
20+
public TestContextConnectionLog(string endpoint, string name, LogLevel logLevel, Type? forContextType = null)
2021
{
2122
this.endpoint = endpoint;
2223
this.name = name;
2324
this.logLevel = logLevel;
25+
this.forContextType = forContextType;
2426
}
2527

2628
public void Write(EventType type, string message, params object?[] args)
@@ -38,15 +40,20 @@ public IList<LogEvent> GetLogs()
3840
throw new NotImplementedException();
3941
}
4042

43+
public ILog ForContext<T>()
44+
{
45+
return new TestContextConnectionLog(endpoint, this.name, logLevel, typeof(T));
46+
}
47+
4148
void WriteInternal(LogEvent logEvent)
4249
{
4350
var logEventLogLevel = GetLogLevel(logEvent);
4451

4552
if (logEventLogLevel >= logLevel)
4653
{
47-
new SerilogLoggerBuilder().Build()
48-
.ForContext<TestContextConnectionLog>()
49-
.Write(GetSerilogLevel(logEvent), string.Format("{5, 16}: {0}:{1} {2} {3} {4}", logEventLogLevel, logEvent.Error, endpoint, Thread.CurrentThread.ManagedThreadId, logEvent.FormattedMessage, name));
54+
var builder = new SerilogLoggerBuilder().Build();
55+
var methodLogger = forContextType != null ? builder.ForContext(forContextType) : builder.ForContext<TestContextConnectionLog>();
56+
methodLogger.Write(GetSerilogLevel(logEvent), string.Format("{5, 16}: {0}:{1} {2} {3} {4}", logEventLogLevel, logEvent.Error, endpoint, Thread.CurrentThread.ManagedThreadId, logEvent.FormattedMessage, name));
5057
}
5158
}
5259

source/Halibut/Diagnostics/ILog.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ namespace Halibut.Diagnostics
77
public interface ILog : ILogWriter
88
{
99
IList<LogEvent> GetLogs();
10+
11+
ILog ForContext<T>();
1012
}
1113
}

source/Halibut/Diagnostics/InMemoryConnectionLog.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public IList<LogEvent> GetLogs()
3939
return events.ToArray();
4040
}
4141

42+
public ILog ForContext<T>() => this;
43+
4244
void WriteInternal(LogEvent logEvent)
4345
{
4446
var logLevel = GetLogLevel(logEvent);

source/Halibut/Diagnostics/LogCreators/AggregateLogWriterLogCreator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Halibut.Diagnostics.LogCreators
66
public class AggregateLogWriterLogCreator : ICreateNewILog
77
{
88
readonly ICreateNewILog logCreator;
9-
readonly Func<string, ILogWriter[]> logWriterFactoryForPrefix;
9+
readonly Func<string, ILog[]> logWriterFactoryForPrefix;
1010

11-
public AggregateLogWriterLogCreator(ICreateNewILog logCreator, Func<string, ILogWriter[]> logWriterFactoryForPrefix)
11+
public AggregateLogWriterLogCreator(ICreateNewILog logCreator, Func<string, ILog[]> logWriterFactoryForPrefix)
1212
{
1313
this.logCreator = logCreator;
1414
this.logWriterFactoryForPrefix = logWriterFactoryForPrefix;

source/Halibut/Diagnostics/LogWriters/AggregateLogWriter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Halibut.Diagnostics.LogWriters
56
{
@@ -9,9 +10,9 @@ namespace Halibut.Diagnostics.LogWriters
910
public class AggregateLogWriter : ILog
1011
{
1112
readonly ILog log;
12-
readonly ILogWriter[] logWriter;
13+
readonly ILog[] logWriter;
1314

14-
public AggregateLogWriter(ILog log, ILogWriter[] logWriter)
15+
public AggregateLogWriter(ILog log, ILog[] logWriter)
1516
{
1617
this.log = log;
1718
this.logWriter = logWriter;
@@ -39,5 +40,10 @@ public IList<LogEvent> GetLogs()
3940
{
4041
return log.GetLogs();
4142
}
43+
44+
public ILog ForContext<T>()
45+
{
46+
return new AggregateLogWriter(log.ForContext<T>(), logWriter.Select(lw => lw.ForContext<T>()).ToArray());
47+
}
4248
}
4349
}

0 commit comments

Comments
 (0)