Skip to content

Commit

Permalink
NLogBeginScopeParser - Skip neste state capture for List + Array + Di…
Browse files Browse the repository at this point in the history
…ctionary
  • Loading branch information
snakefoot committed Mar 18, 2023
1 parent 8ef4b2d commit c57a023
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ public IDisposable ParseBeginScope<T>(T state)
{
if (state is IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
if (scopePropertyList is IList)
return ScopeContext.PushProperties(scopePropertyList); // Probably List/Array without nested state

object scopeObject = scopePropertyList;
scopePropertyList = ParseScopeProperties(scopePropertyList);
return ScopeContext.PushNestedStateProperties(scopeObject, scopePropertyList);
}
else if (state is IReadOnlyCollection<KeyValuePair<string, object>> scopeProperties)
{
if (scopeProperties is IDictionary)
return ScopeContext.PushProperties(scopeProperties); // Probably Dictionary without nested state
else
return ScopeContext.PushNestedStateProperties(scopeProperties, scopeProperties);
}

if (!(state is string))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using System.Security;

namespace NLog.Extensions.Logging.Tests.Extensions
{
Expand Down Expand Up @@ -70,7 +71,7 @@ public void AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope()
var logger = loggerFactory.CreateLogger(nameof(AddNLog_LoggerFactory_IncludeActivityIdsWithBeginScope));
var activity = new System.Diagnostics.Activity("TestActivity").SetParentId("42").Start();
var scopeProperties = new Dictionary<string, object> { { "RequestId", "123" }, { "RequestPath", "Unknown" } };
using (logger.BeginScope(scopeProperties.ToList()))
using (logger.BeginScope(new ArraySegment<KeyValuePair<string, object>>(scopeProperties.ToArray())))
{
logger.LogInformation(default(EventId), "test message with {0} arg", 1);
}
Expand Down

0 comments on commit c57a023

Please sign in to comment.