Skip to content

Commit 4c8701a

Browse files
committed
Add SerilogFactory constructor with custom logger
1 parent b58621c commit 4c8701a

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

src/ServiceStack.Logging.Serilog/SerilogFactory.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Serilog;
23

34
namespace ServiceStack.Logging.Serilog
45
{
@@ -7,14 +8,28 @@ namespace ServiceStack.Logging.Serilog
78
/// </summary>
89
public class SerilogFactory : ILogFactory
910
{
11+
private readonly ILogger logger;
12+
13+
public SerilogFactory() {}
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="SerilogFactory"/> class.
17+
/// </summary>
18+
public SerilogFactory(ILogger logger)
19+
{
20+
this.logger = logger;
21+
}
22+
1023
/// <summary>
1124
/// Gets the logger.
1225
/// </summary>
1326
/// <param name="type">The type.</param>
1427
/// <returns></returns>
1528
public ILog GetLogger(Type type)
1629
{
17-
return new SerilogLogger(type);
30+
return logger != null
31+
? new SerilogLogger(logger.ForContext(type))
32+
: new SerilogLogger(type);
1833
}
1934

2035
/// <summary>
@@ -24,7 +39,7 @@ public ILog GetLogger(Type type)
2439
/// <returns></returns>
2540
public ILog GetLogger(string typeName)
2641
{
27-
return new SerilogLogger(Type.GetType(typeName));
42+
return GetLogger(Type.GetType(typeName));
2843
}
2944
}
3045
}

tests/ServiceStack.Common.Tests/Messaging/MqServerAppHostTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void Any(ThrowVoid request)
164164
}
165165
}
166166

167-
public class MqTestsAppHost : AppHostHttpListenerBase
167+
public class MqTestsAppHost : AppSelfHostBase
168168
{
169169
private readonly Func<IMessageService> createMqServerFn;
170170

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
3+
using Serilog;
4+
using Serilog.Configuration;
5+
using Serilog.Core;
6+
using Serilog.Events;
27
using ServiceStack.Logging.Serilog;
38

49
namespace ServiceStack.Logging.Tests.UseCases
510
{
611
[TestFixture]
712
public class UsingSerilog
813
{
9-
[Test]
10-
public void SerilogUseCase()
14+
private void TestLog()
1115
{
12-
LogManager.LogFactory = new SerilogFactory();
1316
var log = LogManager.GetLogger(GetType());
1417

1518
log.Debug("Debug Event Log Entry.");
@@ -18,5 +21,50 @@ public void SerilogUseCase()
1821
log.Error("Error Event Log Entry.");
1922
log.Fatal("Fatal Event Log Entry.");
2023
}
24+
25+
[Test]
26+
public void Use_default_SerilogFactory()
27+
{
28+
LogManager.LogFactory = new SerilogFactory();
29+
TestLog();
30+
}
31+
32+
33+
34+
[Test]
35+
public void Use_Serilog_with_custom_configuration_and_sink()
36+
{
37+
LogManager.LogFactory = new SerilogFactory(new LoggerConfiguration()
38+
.WriteTo.MySink()
39+
.CreateLogger());
40+
41+
TestLog();
42+
}
43+
}
44+
45+
public class MySink : ILogEventSink
46+
{
47+
private readonly IFormatProvider formatProvider;
48+
49+
public MySink(IFormatProvider formatProvider)
50+
{
51+
this.formatProvider = formatProvider;
52+
}
53+
54+
public void Emit(LogEvent logEvent)
55+
{
56+
var message = logEvent.RenderMessage(formatProvider);
57+
Console.WriteLine(DateTimeOffset.Now + " " + message);
58+
}
59+
}
60+
61+
public static class MySinkExtensions
62+
{
63+
public static LoggerConfiguration MySink(
64+
this LoggerSinkConfiguration loggerConfiguration,
65+
IFormatProvider formatProvider = null)
66+
{
67+
return loggerConfiguration.Sink(new MySink(formatProvider));
68+
}
2169
}
2270
}

0 commit comments

Comments
 (0)