Skip to content

Commit 2160d34

Browse files
author
Jason Dryhurst-Smith
committed
add the logging provider for nlog and add some helpful extensions for service collection
1 parent a526aa5 commit 2160d34

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<PackageId>CorrelatorSharp.Logging.LoggerProvider.NLog</PackageId>
4+
5+
<Version>1.0.0</Version>
6+
7+
<!--Bump the assembly version only on major releases-->
8+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
9+
10+
<AssemblyName>CorrelatorSharp.Logging.LoggerProvider.NLog</AssemblyName>
11+
<Description>CorrelatorSharp, a set of tools for adding correlation identifiers for actions performed by users and machine across your applications. Your one stop shop for context-aware logging and diagnostics.</Description>
12+
<Authors>Ivan Zlatev, Jason Dryhurst-Smith</Authors>
13+
<TargetFrameworks>net47;netstandard2.0</TargetFrameworks>
14+
<PackageTags>context;correlation;logging;diagnostics</PackageTags>
15+
<PackageProjectUrl>https://github.com/CorrelatorSharp/CorrelatorSharp</PackageProjectUrl>
16+
<PackageLicenseUrl>https://github.com/CorrelatorSharp/CorrelatorSharp/blob/master/LICENSE.txt</PackageLicenseUrl>
17+
18+
</PropertyGroup>
19+
<ItemGroup>
20+
<PackageReference Include="NLog" Version="4.6.2" />
21+
<PackageReference Include="CorrelatorSharp.Logging.NLog" Version="[1.3.1,2)"/>
22+
<PackageReference Include="CorrelatorSharp.Logging.LoggerProvider" Version="[1.0.0,2)"/>
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace CorrelatorSharp.Logging.LoggerProvider.NLog
5+
{
6+
[ProviderAlias("CorrelatorSharp.NLog")]
7+
[ExcludeFromCodeCoverage]
8+
public class NLogCorrelatorSharpLoggerProvider : CorrelatorSharpLoggerProvider
9+
{
10+
public NLogCorrelatorSharpLoggerProvider()
11+
: base(new NLogScopeProvider())
12+
{
13+
LoggingConfiguration.Current.UseNLog();
14+
}
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using NLog;
3+
using NLog.Common;
4+
5+
namespace CorrelatorSharp.Logging.LoggerProvider.NLog
6+
{
7+
public class NLogScopeProvider : IScopeProvider
8+
{
9+
public IDisposable BeginScope<TState>(TState state)
10+
{
11+
return NestedDiagnosticsLogicalContext.Push(state);
12+
}
13+
14+
public void ErrorBuildingScope(Exception exception)
15+
{
16+
InternalLogger.Debug(exception, "Exception in BeginScope push NestedDiagnosticsLogicalContext");
17+
}
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using CorrelatorSharp.Logging.LoggerProvider.NLog;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace CorrelatorSharp.Logging
6+
{
7+
public static class ServiceCollectionExtensions
8+
{
9+
public static void UseCorrelatorSharpNLogLoggerProvider(this IServiceCollection serviceCollection)
10+
{
11+
serviceCollection.AddSingleton<ILoggerProvider, NLogCorrelatorSharpLoggerProvider>();
12+
}
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.572
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CorrelatorSharp.Logging.LoggerProvider.NLog", "CorrelatorSharp.Logging.LoggerProvider.NLog\CorrelatorSharp.Logging.LoggerProvider.NLog.csproj", "{32B51D9D-F29A-4926-98D8-26C83D0567F5}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{32B51D9D-F29A-4926-98D8-26C83D0567F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{32B51D9D-F29A-4926-98D8-26C83D0567F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{32B51D9D-F29A-4926-98D8-26C83D0567F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{32B51D9D-F29A-4926-98D8-26C83D0567F5}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6B83DABA-8D70-4222-9E8F-2B77E751025D}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)