Skip to content

Commit d7384e7

Browse files
authored
Update to NLog 5.0 Preview 1 with ScopeContext instead of MDLC (#519)
1 parent 185d8b8 commit d7384e7

11 files changed

Lines changed: 198 additions & 248 deletions

src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs

Lines changed: 164 additions & 210 deletions
Large diffs are not rendered by default.

src/NLog.Extensions.Logging/Logging/NLogLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ private static LogLevel ConvertLogLevel(Microsoft.Extensions.Logging.LogLevel lo
466466
}
467467

468468
/// <summary>
469-
/// Begin a scope. Use in config with ${ndlc}
469+
/// Begin a scope. Use in config with ${scopeproperty} or ${scopenested}
470470
/// </summary>
471471
/// <param name="state">The state (message)</param>
472472
/// <returns></returns>

src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class NLogProviderOptions
3434
public bool ParseMessageTemplates { get; set; }
3535

3636
/// <summary>
37-
/// Enable capture of scope information and inject into <see cref="NestedDiagnosticsLogicalContext" /> and <see cref="MappedDiagnosticsLogicalContext" />
37+
/// Enable capture of scope information and inject into <see cref="NLog.ScopeContext" />
3838
/// </summary>
3939
public bool IncludeScopes { get; set; } = true;
4040

src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
7171
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
7272
</PropertyGroup>
7373
<ItemGroup>
74-
<PackageReference Include="NLog" Version="4.7.11" />
74+
<PackageReference Include="NLog" Version="5.0.0-preview.1" />
7575
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
7676
</ItemGroup>
7777
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">

src/NLog.Extensions.Logging/Targets/MicrosoftILoggerTarget.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public MicrosoftILoggerTarget(Microsoft.Extensions.Logging.ILogger logger)
4242
{
4343
_logger = logger;
4444
Layout = "${message}";
45-
OptimizeBufferReuse = true;
4645
}
4746

4847
/// <summary>
@@ -53,7 +52,6 @@ public MicrosoftILoggerTarget(Microsoft.Extensions.Logging.ILoggerFactory logger
5352
{
5453
_loggerFactory = loggerFactory;
5554
Layout = "${message}";
56-
OptimizeBufferReuse = true;
5755
}
5856

5957
/// <inheritdoc />
@@ -91,7 +89,7 @@ protected override void Write(LogEventInfo logEvent)
9189
var layoutMessage = RenderLogEvent(Layout, logEvent);
9290

9391
IDictionary<string, object> contextProperties = null;
94-
if (ContextProperties.Count > 0 || IncludeMdlc || IncludeMdc || IncludeGdc)
92+
if (ContextProperties.Count > 0 || IncludeScopeProperties || IncludeGdc)
9593
{
9694
contextProperties = GetContextProperties(logEvent);
9795
if (contextProperties?.Count == 0)

test/NLog.Extensions.Logging.Tests/CustomBeginScopeTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CustomBeginScopeTest : NLogTestBase
1313
public void TestNonSerializableSayHello()
1414
{
1515
var runner = GetRunner<CustomBeginScopeTestRunner>();
16-
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
16+
var target = new Targets.MemoryTarget { Layout = "${message} ${scopeproperty:World}. Welcome ${ndlc}" };
1717
ConfigureNLog(target);
1818
runner.SayHello().Wait();
1919
Assert.Single(target.Logs);
@@ -24,7 +24,7 @@ public void TestNonSerializableSayHello()
2424
public void TestNonSerializableSayHelloWithScope()
2525
{
2626
var runner = GetRunner<CustomBeginScopeTestRunner>(new NLogProviderOptions { IncludeScopes = false });
27-
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
27+
var target = new Targets.MemoryTarget { Layout = "${message} ${scopeproperty:World}. Welcome ${ndlc}" };
2828
ConfigureNLog(target);
2929
runner.SayHello().Wait();
3030
Assert.Single(target.Logs);
@@ -35,7 +35,7 @@ public void TestNonSerializableSayHelloWithScope()
3535
public void TestNonSerializableSayHi()
3636
{
3737
var runner = GetRunner<CustomBeginScopeTestRunner>();
38-
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:World}. Welcome ${ndlc}" };
38+
var target = new Targets.MemoryTarget { Layout = "${message} ${scopeproperty:World}. Welcome ${ndlc}" };
3939
ConfigureNLog(target);
4040
var scopeString = runner.SayHi().Result;
4141
Assert.Single(target.Logs);
@@ -47,7 +47,7 @@ public void TestNonSerializableSayHi()
4747
public void TestNonSerializableSayHiToEarth()
4848
{
4949
var runner = GetRunner<CustomBeginScopeTestRunner>();
50-
var target = new Targets.MemoryTarget { Layout = "${message} ${mdlc:Planet}. Welcome to the ${mdlc:Galaxy}" };
50+
var target = new Targets.MemoryTarget { Layout = "${message} ${scopeproperty:Planet}. Welcome to the ${scopeproperty:Galaxy}" };
5151
ConfigureNLog(target);
5252
var scopeString = runner.SayHiToEarth().Result;
5353
Assert.Single(target.Logs);

test/NLog.Extensions.Logging.Tests/Extensions/ConfigureExtensionsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ public void AddNLog_LoggerFactory_LogInfoWithEventId_ShouldLogToNLogWithEventId(
5959
public void AddNLog_LoggerFactory_IncludeActivtyIdsWithBeginScope()
6060
{
6161
// Arrange
62-
var loggerFactory = new LoggerFactory();
63-
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{mdlc:ParentId}} - ${{message}}");
62+
var loggerFactory = LoggerFactory.Create(builder => builder.AddNLog(new NLogProviderOptions { IncludeActivtyIdsWithBeginScope = true }));
63+
var config = CreateConfigWithMemoryTarget(out var memoryTarget, $"${{scopeproperty:ParentId}} - ${{message}}");
6464

6565
// Act
66-
loggerFactory.AddNLog(new NLogProviderOptions { IncludeActivtyIdsWithBeginScope = true });
6766
LogManager.Configuration = config;
6867
var logger = loggerFactory.CreateLogger(nameof(AddNLog_LoggerFactory_IncludeActivtyIdsWithBeginScope));
6968
var activity = new System.Diagnostics.Activity("TestActivity").SetParentId("42").Start();

test/NLog.Extensions.Logging.Tests/LoggerTests.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ public void TestInvalidFormatString2()
131131
}
132132

133133
[Theory]
134-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL|message Exception of type 'System.Exception' was thrown.|20")]
135-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message Exception of type 'System.Exception' was thrown.|20")]
136-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR|message Exception of type 'System.Exception' was thrown.|20")]
137-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO|message Exception of type 'System.Exception' was thrown.|20")]
138-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE|message Exception of type 'System.Exception' was thrown.|20")]
139-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN|message Exception of type 'System.Exception' was thrown.|20")]
134+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
135+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
136+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
137+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
138+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
139+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN|message System.Exception: Exception of type 'System.Exception' was thrown.|20")]
140140
public void TestExceptionWithMessage(Microsoft.Extensions.Logging.LogLevel logLevel, string expectedLogMessage)
141141
{
142142
GetRunner().Log(logLevel, 20, new Exception(), "message");
@@ -146,12 +146,12 @@ public void TestExceptionWithMessage(Microsoft.Extensions.Logging.LogLevel logLe
146146
}
147147

148148
[Theory]
149-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL| Exception of type 'System.Exception' was thrown.|20")]
150-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG| Exception of type 'System.Exception' was thrown.|20")]
151-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR| Exception of type 'System.Exception' was thrown.|20")]
152-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO| Exception of type 'System.Exception' was thrown.|20")]
153-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE| Exception of type 'System.Exception' was thrown.|20")]
154-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN| Exception of type 'System.Exception' was thrown.|20")]
149+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
150+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
151+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
152+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
153+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
154+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN| System.Exception: Exception of type 'System.Exception' was thrown.|20")]
155155
public void TestExceptionWithEmptyMessage(Microsoft.Extensions.Logging.LogLevel logLevel, string expectedLogMessage)
156156
{
157157
GetRunner().Log(logLevel, 20, new Exception(), string.Empty);
@@ -161,12 +161,12 @@ public void TestExceptionWithEmptyMessage(Microsoft.Extensions.Logging.LogLevel
161161
}
162162

163163
[Theory]
164-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL|[null] Exception of type 'System.Exception' was thrown.|20")]
165-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|[null] Exception of type 'System.Exception' was thrown.|20")]
166-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR|[null] Exception of type 'System.Exception' was thrown.|20")]
167-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO|[null] Exception of type 'System.Exception' was thrown.|20")]
168-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE|[null] Exception of type 'System.Exception' was thrown.|20")]
169-
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN|[null] Exception of type 'System.Exception' was thrown.|20")]
164+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Critical, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|FATAL|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
165+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Debug, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|DEBUG|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
166+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Error, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|ERROR|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
167+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Information, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|INFO|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
168+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Trace, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|TRACE|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
169+
[InlineData(Microsoft.Extensions.Logging.LogLevel.Warning, "NLog.Extensions.Logging.Tests.LoggerTests.Runner|WARN|[null] System.Exception: Exception of type 'System.Exception' was thrown.|20")]
170170
public void TestExceptionWithNullMessage(Microsoft.Extensions.Logging.LogLevel logLevel, string expectedLogMessage)
171171
{
172172
GetRunner().Log(logLevel, 20, new Exception(), null);

test/NLog.Extensions.Logging.Tests/MicrosoftILoggerTargetTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public void OverrideLoggerNameILoggerFactoryMessageTest()
4545
{
4646
// Arrange
4747
var (logger, mock) = CreateLoggerFactoryMock(out var microsoftTarget);
48-
microsoftTarget.LoggerName = "${mdlc:FunctionName}";
48+
microsoftTarget.LoggerName = "${scopeproperty:FunctionName}";
4949

5050
// Act
51-
using (NLog.MappedDiagnosticsLogicalContext.SetScoped("FunctionName", nameof(OverrideLoggerNameILoggerFactoryMessageTest)))
51+
using (NLog.ScopeContext.PushProperty("FunctionName", nameof(OverrideLoggerNameILoggerFactoryMessageTest)))
5252
logger.Info("Hello World");
5353

5454
// Assert
@@ -162,7 +162,7 @@ public void IncludeEmptyMdc()
162162
{
163163
// Arrange
164164
var (logger, mock) = CreateLoggerMock(out var target);
165-
target.IncludeMdc = true;
165+
target.IncludeScopeProperties = true;
166166

167167
// Act
168168
logger.Info("Hello there");

test/NLog.Extensions.Logging.Tests/NLogLoggingConfigurationTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void LoadDefaultTargetParametersConfig()
156156
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
157157
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
158158
Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName?.Render(LogEventInfo.CreateNullEvent()));
159-
Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.Error);
159+
Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.StdErr);
160160
}
161161

162162
[Fact]
@@ -175,7 +175,7 @@ public void LoadTargetDefaultParametersConfig()
175175
Assert.Single(logConfig.AllTargets.Where(t => t is FileTarget));
176176
Assert.Single(logConfig.AllTargets.Where(t => t is ConsoleTarget));
177177
Assert.Equal("hello.txt", (logConfig.FindTargetByName("file") as FileTarget)?.FileName?.Render(LogEventInfo.CreateNullEvent()));
178-
Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.Error);
178+
Assert.True((logConfig.FindTargetByName("console") as ConsoleTarget)?.StdErr);
179179
}
180180

181181
[Fact]
@@ -255,7 +255,6 @@ public void SetupBuilderLoadConfigurationFromSection()
255255

256256
var logFactory = new LogFactory();
257257
logFactory.Setup()
258-
.SetupExtensions(s => s.AutoLoadAssemblies(false))
259258
.LoadConfigurationFromSection(configuration);
260259

261260
Assert.Single(logFactory.Configuration.LoggingRules);

0 commit comments

Comments
 (0)