diff --git a/appveyor.yml b/appveyor.yml index 6e64132f..dd4bab21 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -22,10 +22,13 @@ artifacts: test_script: - nuget.exe install OpenCover -ExcludeVersion -DependencyVersion Ignore - OpenCover\tools\OpenCover.Console.exe -register:user -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp1.1 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp2.0 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.0 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp2.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" - - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp2.1 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.1 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net5.0 -c debug NLog.Extensions.Logging.Tests" -filter:"+[NLog.Extensions.Logging]* -[NLog.Extensions.Logging.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp2.1 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f netcoreapp3.1 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - OpenCover\tools\OpenCover.Console.exe -register:user -mergeoutput -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:"test -f net5.0 -c debug NLog.Extensions.Hosting.Tests" -filter:"+[NLog.Extensions.Logging]* +[NLog.Extensions.Hosting]* -[NLog.Extensions.Logging.Tests]* -[NLog.Extensions.Hosting.Tests]*" -output:"coverage.xml" -oldstyle -targetdir:"test" + - pip install codecov - pip install codecov - codecov -f "coverage.xml" - ps: .\run-sonar.ps1 diff --git a/build.ps1 b/build.ps1 index a3eaf36c..bb895044 100644 --- a/build.ps1 +++ b/build.ps1 @@ -17,11 +17,11 @@ dotnet restore .\src\NLog.Extensions.Hosting\ if (-Not $LastExitCode -eq 0) { exit $LastExitCode } -msbuild /t:Pack .\src\NLog.Extensions.Logging\ /p:targetFrameworks='"net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0"' /p:VersionPrefix=$versionPrefix /p:VersionSuffix=$versionSuffix /p:FileVersion=$versionFile /p:ProductVersion=$versionProduct /p:Configuration=Release /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg /p:PackageOutputPath=..\..\artifacts /verbosity:minimal +msbuild /t:Pack .\src\NLog.Extensions.Logging\ /p:targetFrameworks='"net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0;net5.0"' /p:VersionPrefix=$versionPrefix /p:VersionSuffix=$versionSuffix /p:FileVersion=$versionFile /p:ProductVersion=$versionProduct /p:Configuration=Release /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg /p:PackageOutputPath=..\..\artifacts /verbosity:minimal if (-Not $LastExitCode -eq 0) { exit $LastExitCode } -msbuild /t:Pack .\src\NLog.Extensions.Hosting\ /p:targetFrameworks='"netstandard2.0;netcoreapp3.0"' /p:VersionPrefix=$versionPrefix /p:VersionSuffix=$versionSuffix /p:FileVersion=$versionFile /p:ProductVersion=$versionProduct /p:Configuration=Release /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg /p:PackageOutputPath=..\..\artifacts /verbosity:minimal +msbuild /t:Pack .\src\NLog.Extensions.Hosting\ /p:targetFrameworks='"netstandard2.0;netcoreapp3.0;net5.0"' /p:VersionPrefix=$versionPrefix /p:VersionSuffix=$versionSuffix /p:FileVersion=$versionFile /p:ProductVersion=$versionProduct /p:Configuration=Release /p:IncludeSymbols=true /p:SymbolPackageFormat=snupkg /p:PackageOutputPath=..\..\artifacts /verbosity:minimal if (-Not $LastExitCode -eq 0) { exit $LastExitCode } diff --git a/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj b/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj index d77bdf98..f4b7f5bf 100644 --- a/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj +++ b/src/NLog.Extensions.Hosting/NLog.Extensions.Hosting.csproj @@ -1,7 +1,7 @@  - netstandard2.0;netcoreapp3.0 + netstandard2.0;netcoreapp3.0;net5.0 Full true true @@ -41,6 +41,10 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN + + + + diff --git a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs index a5b52638..e66fe1c5 100644 --- a/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs +++ b/src/NLog.Extensions.Logging/Extensions/ConfigureExtensions.cs @@ -84,6 +84,19 @@ public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfigurati return factory; } + /// + /// Enable NLog as logging provider for Microsoft Extension Logging + /// + /// + /// Configuration + /// NLog options + /// ILoggingBuilder for chaining + public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration, NLogProviderOptions options) + { + AddNLogLoggerProvider(factory.Services, configuration, options, CreateNLogLoggerProvider); + return factory; + } + /// /// Enable NLog as logging provider for Microsoft Extension Logging /// diff --git a/src/NLog.Extensions.Logging/Logging/ActivityExtensions.cs b/src/NLog.Extensions.Logging/Logging/ActivityExtensions.cs new file mode 100644 index 00000000..eab2bbc6 --- /dev/null +++ b/src/NLog.Extensions.Logging/Logging/ActivityExtensions.cs @@ -0,0 +1,44 @@ +#if NET5_0 + +using System.Diagnostics; + +namespace NLog.Extensions.Logging +{ + /// + /// Helpers for getting the right values from Activity no matter the format (w3c or hierarchical) + /// + 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 \ No newline at end of file diff --git a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs index 8dd0db6c..185ee396 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogBeginScopeParser.cs @@ -31,7 +31,7 @@ public IDisposable ParseBeginScope(T state) { if (state is IReadOnlyList> scopePropertyList) { - return ScopeProperties.CaptureScopeProperties(scopePropertyList); + return ScopeProperties.CaptureScopeProperties(scopePropertyList, _options.IncludeActivtyIdsWithBeginScope); } if (!(state is string)) @@ -90,18 +90,92 @@ private static IDisposable CreateScopeProperties(object scopeObject, IReadOnlyLi return NestedDiagnosticsLogicalContext.Push(scopeObject); } - public static IDisposable CaptureScopeProperties(IReadOnlyList> scopePropertyList) + public static IDisposable CaptureScopeProperties(IReadOnlyList> 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> IncludeActivityIdsProperties(IReadOnlyList> scopePropertyList) + { + return scopePropertyList; + } +#else + private static IReadOnlyList> IncludeActivityIdsProperties(IReadOnlyList> scopePropertyList) + { + if (scopePropertyList.Count > 1 && "RequestPath".Equals(scopePropertyList[1].Key)) + { + var activty = System.Diagnostics.Activity.Current; + if (activty != null) + return new ScopePropertiesWithActivityIds(scopePropertyList, activty); + } + + return scopePropertyList; + } + + private class ScopePropertiesWithActivityIds : IReadOnlyList> + { + private readonly IReadOnlyList> _originalPropertyList; + private readonly System.Diagnostics.Activity _currentActivity; + + public ScopePropertiesWithActivityIds(IReadOnlyList> originalPropertyList, System.Diagnostics.Activity currentActivity) + { + _originalPropertyList = originalPropertyList; + _currentActivity = currentActivity; + } + + public KeyValuePair this[int index] + { + get + { + int offset = index - _originalPropertyList.Count; + if (offset < 0) + { + return _originalPropertyList[index]; + } + else + { + switch (offset) + { + case 0: return new KeyValuePair(nameof(_currentActivity.SpanId), _currentActivity.GetSpanId()); + case 1: return new KeyValuePair(nameof(_currentActivity.TraceId), _currentActivity.GetTraceId()); + case 2: return new KeyValuePair(nameof(_currentActivity.ParentId), _currentActivity.GetParentId()); + } + } + + throw new ArgumentOutOfRangeException(nameof(index)); + } + } + + public int Count => _originalPropertyList.Count + 3; + + public IEnumerator> GetEnumerator() + { + for (int i = 0; i < Count; ++i) + yield return this[i]; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IEnumerable)_originalPropertyList).GetEnumerator(); + } + } +#endif + private static IReadOnlyList> ExcludeOriginalFormatProperty(IReadOnlyList> scopePropertyList) { if (scopePropertyList.Count == 2 && !NLogLogger.OriginalFormatPropertyName.Equals(scopePropertyList[0].Key)) diff --git a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs index 1354271d..c1185a17 100644 --- a/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs +++ b/src/NLog.Extensions.Logging/Logging/NLogProviderOptions.cs @@ -43,6 +43,19 @@ public class NLogProviderOptions /// public bool ShutdownOnDispose { get; set; } +#if NET5_0 + /// + /// Automatically include , and + /// + /// For Net5.0 where these properties are no longer included by default for performance reasons +#else + /// + /// Automatically include Activity.SpanId, Activity.TraceId and Activity.ParentId. + /// + /// For Net5.0 where these properties are no longer included by default for performance reasons +#endif + public bool IncludeActivtyIdsWithBeginScope { get; set; } + /// Initializes a new instance NLogProviderOptions with default values. public NLogProviderOptions() { diff --git a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj index ec1a39d8..c04f6843 100644 --- a/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj +++ b/src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj @@ -2,7 +2,7 @@ PackageReference - net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0 + net451;net461;netstandard1.3;netstandard1.5;netstandard2.0;netcoreapp3.0;net5.0 Full true true @@ -38,6 +38,11 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN ..\NLog.snk true + + + + + NLog.Extensions.Logging for .NET Framework 4.6.1 true @@ -48,23 +53,27 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN $(DefineConstants);NETCORE1_0 - NLog.Extensions.Logging for NetStandard 1.3 + NLog.Extensions.Logging for .NET Standard 1.3 1.6.0 $(DefineConstants);NETCORE1_0;NETSTANDARD - NLog.Extensions.Logging for NetStandard 1.5 + NLog.Extensions.Logging for .NET Standard 1.5 1.6.0 $(DefineConstants);NETCORE1_0;NETSTANDARD - NLog.Extensions.Logging for NetStandard 2.0 + NLog.Extensions.Logging for .NET Standard 2.0 $(DefineConstants);NETSTANDARD NLog.Extensions.Logging for .NET Core 3 $(DefineConstants);NETSTANDARD + + NLog.Extensions.Logging for .NET 5.0 + $(DefineConstants);NETSTANDARD + @@ -91,17 +100,20 @@ Full changelog: https://github.com/NLog/NLog.Extensions.Logging/blob/master/CHAN - + + + + + - $(Title) latest diff --git a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj index b6709160..2d58a731 100644 --- a/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj +++ b/test/NLog.Extensions.Hosting.Tests/NLog.Extensions.Hosting.Tests.csproj @@ -2,7 +2,7 @@ PackageReference - netcoreapp2.0;netcoreapp3.0 + netcoreapp2.1;netcoreapp3.1;net5.0 Library false full @@ -15,12 +15,16 @@ latest - + - - + + + + + + diff --git a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj index 76becd50..ac161f2f 100644 --- a/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj +++ b/test/NLog.Extensions.Logging.Tests/NLog.Extensions.Logging.Tests.csproj @@ -3,7 +3,7 @@ PackageReference - netcoreapp1.1;netcoreapp2.0;netcoreapp3.0;net452;net461 + netcoreapp1.1;netcoreapp2.1;netcoreapp3.1;net452;net461;net5.0 Library false Full @@ -31,11 +31,15 @@ - + - - + + + + + +