Skip to content

Commit

Permalink
Added NLogProviderOptions.IncludeActivtyIdsWithBeginScope
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Nov 23, 2020
1 parent 9c3c6f0 commit c179c30
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;net50</TargetFrameworks>
<DebugType Condition=" '$(Configuration)' == 'Debug' ">Full</DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -41,6 +41,10 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net50' ">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<ProjectReference Include="..\NLog.Extensions.Logging\NLog.Extensions.Logging.csproj" />
Expand Down
44 changes: 44 additions & 0 deletions src/NLog.Extensions.Logging/Logging/ActivityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#if NET5_0

using System.Diagnostics;

namespace NLog.Extensions.Logging
{
/// <summary>
/// Helpers for getting the right values from Activity no matter the format (w3c or hierarchical)
/// </summary>
internal static class ActivityExtensions
{
public static string GetSpanId(this Activity activity)
{
return activity.IdFormat switch
{
ActivityIdFormat.Hierarchical => activity.Id,
ActivityIdFormat.W3C => activity.SpanId.ToHexString(),
_ => null,
} ?? string.Empty;
}

public static string GetTraceId(this Activity activity)
{
return activity.IdFormat switch
{
ActivityIdFormat.Hierarchical => activity.RootId,
ActivityIdFormat.W3C => activity.TraceId.ToHexString(),
_ => null,
} ?? string.Empty;
}

public static string GetParentId(this Activity activity)
{
return activity.IdFormat switch
{
ActivityIdFormat.Hierarchical => activity.ParentId,
ActivityIdFormat.W3C => activity.ParentSpanId.ToHexString(),
_ => null,
} ?? string.Empty;
}
}
}

#endif
79 changes: 75 additions & 4 deletions src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public IDisposable ParseBeginScope<T>(T state)
{
if (state is IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
return ScopeProperties.CaptureScopeProperties(scopePropertyList);
return ScopeProperties.CaptureScopeProperties(scopePropertyList, _options.IncludeActivtyIdsWithBeginScope);
}

if (!(state is string))
Expand Down Expand Up @@ -90,18 +90,89 @@ private static IDisposable CreateScopeProperties(object scopeObject, IReadOnlyLi
return NestedDiagnosticsLogicalContext.Push(scopeObject);
}

public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
public static IDisposable CaptureScopeProperties(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList, bool includeActivtyIdsWithBeginScope)
{
object scopeObject = scopePropertyList;

if (scopePropertyList.Count > 0 && NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key))
if (scopePropertyList.Count > 0)
{
scopePropertyList = ExcludeOriginalFormatProperty(scopePropertyList);
if (NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[scopePropertyList.Count - 1].Key))
{
scopePropertyList = ExcludeOriginalFormatProperty(scopePropertyList);
}
else if (includeActivtyIdsWithBeginScope && "RequestId".Equals(scopePropertyList[0].Key))
{
scopePropertyList = IncludeActivityIdsProperties(scopePropertyList);
}
}

return CreateScopeProperties(scopeObject, scopePropertyList);
}

#if !NET5_0
private static IReadOnlyList<KeyValuePair<string, object>> IncludeActivityIdsProperties(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
return scopePropertyList;
}
#else
private static IReadOnlyList<KeyValuePair<string, object>> IncludeActivityIdsProperties(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
var activty = System.Diagnostics.Activity.Current;
if (activty != null)
return new ScopePropertiesWithActivityIds(scopePropertyList, activty);
else
return scopePropertyList;
}

private class ScopePropertiesWithActivityIds : IReadOnlyList<KeyValuePair<string, object>>
{
private readonly IReadOnlyList<KeyValuePair<string, object>> _originalPropertyList;
private readonly System.Diagnostics.Activity _currentActivity;

public ScopePropertiesWithActivityIds(IReadOnlyList<KeyValuePair<string, object>> originalPropertyList, System.Diagnostics.Activity currentActivity)
{
_originalPropertyList = originalPropertyList;
_currentActivity = currentActivity;
}

public KeyValuePair<string, object> this[int index]
{
get
{
int offset = index - _originalPropertyList.Count;
if (offset < 0)
{
return _originalPropertyList[index];
}
else
{
switch (offset)
{
case 0: return new KeyValuePair<string, object>(nameof(_currentActivity.SpanId), _currentActivity.GetSpanId());
case 1: return new KeyValuePair<string, object>(nameof(_currentActivity.TraceId), _currentActivity.GetTraceId());
case 2: return new KeyValuePair<string, object>(nameof(_currentActivity.ParentId), _currentActivity.GetParentId());
}
}

throw new ArgumentOutOfRangeException(nameof(index));
}
}

public int Count => _originalPropertyList.Count + 3;

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (int i = 0; i < Count; ++i)
yield return this[i];
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_originalPropertyList).GetEnumerator();
}
}
#endif

private static IReadOnlyList<KeyValuePair<string, object>> ExcludeOriginalFormatProperty(IReadOnlyList<KeyValuePair<string, object>> scopePropertyList)
{
if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key))
Expand Down
12 changes: 12 additions & 0 deletions src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public class NLogProviderOptions
/// </summary>
public bool ShutdownOnDispose { get; set; }

#if NET5_0
/// <summary>
/// Automatically include <see cref="System.Diagnostics.Activity.SpanId"/>, <see cref="System.Diagnostics.Activity.TraceId"/> and <see cref="System.Diagnostics.Activity.ParentId"/>
/// </summary>
#else
/// <summary>
/// Automatically include Activity.SpanId, Activity.TraceId and Activity.ParentId.
/// </summary>
#endif
/// <remarks>For Net5.0 where these properties are no longer included by default for performance reasons</remarks>
public bool IncludeActivtyIdsWithBeginScope { get; set; }

/// <summary>Initializes a new instance NLogProviderOptions with default values.</summary>
public NLogProviderOptions()
{
Expand Down
11 changes: 10 additions & 1 deletion src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

<TargetFrameworks>net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0;net50</TargetFrameworks>
<DebugType Condition=" '$(Configuration)' == 'Debug' ">Full</DebugType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down Expand Up @@ -65,6 +65,10 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
<Title>NLog.Extensions.Logging for .NET Core 3</Title>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net50' ">
<Title>NLog.Extensions.Logging for .NET 5.0</Title>
<DefineConstants>$(DefineConstants);NETSTANDARD</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.5" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
Expand Down Expand Up @@ -96,6 +100,11 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net50' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
</ItemGroup>

<PropertyGroup>
<AssemblyTitle>$(Title)</AssemblyTitle>
<LangVersion>latest</LangVersion>
Expand Down

0 comments on commit c179c30

Please sign in to comment.