Skip to content
This repository was archived by the owner on Feb 27, 2022. It is now read-only.

Commit fb5f8f4

Browse files
committed
Adding an outputTemplate optional parameter to NLog sink
1 parent 14352e6 commit fb5f8f4

File tree

6 files changed

+115
-9
lines changed

6 files changed

+115
-9
lines changed

src/Serilog.Sinks.NLog/LoggerConfigurationsNLogExtensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Serilog.Sinks.NLog;
1717
using Serilog.Configuration;
1818
using Serilog.Events;
19+
using Serilog.Formatting.Display;
1920

2021
namespace Serilog
2122
{
@@ -24,6 +25,8 @@ namespace Serilog
2425
/// </summary>
2526
public static class LoggerConfigurationsNLogExtensions
2627
{
28+
const string DefaultOutputTemplate = "{Message}";
29+
2730
/// <summary>
2831
/// Adds a sink that writes adapted log events to NLog.
2932
/// </summary>
@@ -35,14 +38,17 @@ public static class LoggerConfigurationsNLogExtensions
3538
public static LoggerConfiguration NLog(
3639
this LoggerSinkConfiguration loggerConfiguration,
3740
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
41+
string outputTemplate = DefaultOutputTemplate,
3842
IFormatProvider formatProvider = null)
3943
{
4044
if (loggerConfiguration == null)
4145
{
42-
throw new ArgumentNullException("loggerConfiguration");
46+
throw new ArgumentNullException(nameof(loggerConfiguration));
4347
}
4448

45-
return loggerConfiguration.Sink(new NLogSink(formatProvider), restrictedToMinimumLevel);
49+
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
50+
51+
return loggerConfiguration.Sink(new NLogSink(formatter), restrictedToMinimumLevel);
4652
}
4753
}
4854
}

src/Serilog.Sinks.NLog/Sinks/NLog/NLogSink.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
using Serilog.Core;
44
using Serilog.Debugging;
55
using Serilog.Events;
6+
using Serilog.Formatting;
7+
using System.IO;
68

79
namespace Serilog.Sinks.NLog
810
{
911
class NLogSink : ILogEventSink
1012
{
11-
readonly IFormatProvider formatProvider;
13+
readonly ITextFormatter textFormatter;
1214

13-
public NLogSink(IFormatProvider formatProvider = null)
15+
public NLogSink(ITextFormatter textFormatter)
1416
{
15-
this.formatProvider = formatProvider;
17+
this.textFormatter = textFormatter;
1618
}
1719

1820
public void Emit(LogEvent logEvent)
@@ -30,10 +32,14 @@ public void Emit(LogEvent logEvent)
3032
}
3133

3234
var level = GetMappedLevel(logEvent);
33-
var message = logEvent.RenderMessage(formatProvider);
35+
36+
var renderSpace = new StringWriter();
37+
this.textFormatter.Format(logEvent, renderSpace);
38+
39+
//var message = logEvent.RenderMessage(formatProvider);
3440
var exception = logEvent.Exception;
3541

36-
var nlogEvent = new LogEventInfo(level, loggerName, message)
42+
var nlogEvent = new LogEventInfo(level, loggerName, renderSpace.ToString())
3743
{
3844
Exception = exception
3945
};

src/Serilog.Sinks.NLog/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.0-*",
2+
"version": "4.0.0-*",
33
"description": "Serilog event sink that writes to NLog. Merge your new Serilog event stream into your existing NLog infrastructure.",
44
"authors": [ "Serilog Contributors" ],
55
"packOptions": {

test/Serilog.Sinks.NLog.Tests/Serilog.Sinks.NLog.Tests.xproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
1212
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
1313
</PropertyGroup>
14-
1514
<PropertyGroup>
1615
<SchemaVersion>2.0</SchemaVersion>
1716
</PropertyGroup>
17+
<ItemGroup>
18+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
19+
</ItemGroup>
1820
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
1921
</Project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using NLog;
2+
using NLog.Config;
3+
using NLog.Targets;
4+
using Serilog.Core;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace Serilog.Sinks.NLog.Tests.Sinks.NLog
9+
{
10+
public class NLogSinkTest
11+
{
12+
[Fact]
13+
public void LogsAreSentToNLog()
14+
{
15+
// Fixture setup
16+
SetupNLogConfiguration();
17+
// Exercise system
18+
var log = new LoggerConfiguration()
19+
.WriteTo.NLog()
20+
.CreateLogger();
21+
log.Warning("The quick brown fox jumps over the lazy dog");
22+
// Verify outcome
23+
var result = GetLastLoggedLineFromNLogMemotyTarget();
24+
Assert.Equal("Default|Warn|The quick brown fox jumps over the lazy dog", result);
25+
// Teardown
26+
TeardownNLogConfiguration();
27+
}
28+
29+
[Fact]
30+
public void SourceContextIsUsedForNLogLogName()
31+
{
32+
// Fixture setup
33+
SetupNLogConfiguration();
34+
// Exercise system
35+
var log = new LoggerConfiguration()
36+
.Enrich.WithProperty(Constants.SourceContextPropertyName, "Test")
37+
.MinimumLevel.Verbose()
38+
.WriteTo.NLog()
39+
.CreateLogger();
40+
log.Debug("The quick brown fox jumps over the lazy dog");
41+
// Verify outcome
42+
var result = GetLastLoggedLineFromNLogMemotyTarget();
43+
Assert.Equal("Test|Debug|The quick brown fox jumps over the lazy dog", result);
44+
// Teardown
45+
TeardownNLogConfiguration();
46+
}
47+
48+
[Fact]
49+
public void TestOutputTemplate()
50+
{
51+
// Fixture setup
52+
SetupNLogConfiguration();
53+
// Exercise system
54+
var log = new LoggerConfiguration()
55+
.WriteTo.NLog(outputTemplate: "[{Level}] - {Message}")
56+
.CreateLogger();
57+
log.Information("The quick brown fox jumps over the lazy dog");
58+
// Verify outcome
59+
var result = GetLastLoggedLineFromNLogMemotyTarget();
60+
Assert.Equal("Default|Info|[Information] - The quick brown fox jumps over the lazy dog", result);
61+
// Teardown
62+
TeardownNLogConfiguration();
63+
}
64+
65+
private const string MemoryTargetName = "memory";
66+
private const string DefaultNLogLayout = @"${logger}|${level}|${message}";
67+
68+
private void SetupNLogConfiguration()
69+
{
70+
var config = new LoggingConfiguration();
71+
var memoryTarget = new MemoryTarget();
72+
config.AddTarget(MemoryTargetName, memoryTarget);
73+
memoryTarget.Layout = DefaultNLogLayout;
74+
var rule = new LoggingRule("*", LogLevel.Trace, memoryTarget);
75+
config.LoggingRules.Add(rule);
76+
LogManager.Configuration = config;
77+
}
78+
79+
private void TeardownNLogConfiguration()
80+
{
81+
LogManager.Configuration = null;
82+
}
83+
84+
private string GetLastLoggedLineFromNLogMemotyTarget()
85+
{
86+
var target = (MemoryTarget)LogManager.Configuration.FindTargetByName(MemoryTargetName);
87+
return target.Logs.Last();
88+
}
89+
}
90+
}

test/Serilog.Sinks.NLog.Tests/project.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"testRunner": "xunit",
33
"dependencies": {
44
"xunit": "2.2.0-beta2-build3300",
5+
"xunit.runner.visualstudio": "2.1.0",
56
"dotnet-test-xunit": "2.2.0-preview2-build1029",
7+
"NLog": "4.4.0-betaV15",
68
"Serilog.Sinks.NLog": { "target": "project" }
79
},
810
"frameworks": {

0 commit comments

Comments
 (0)