Skip to content

Commit 768db12

Browse files
Merge pull request #132 from NLog/netstandard2
.NET Standard 2 support
2 parents 3b7e471 + fe0d6fe commit 768db12

File tree

4 files changed

+74
-32
lines changed

4 files changed

+74
-32
lines changed

src/NLog.Extensions.Logging/ConfigureExtensions.cs

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace NLog.Extensions.Logging
1111
/// </summary>
1212
public static class ConfigureExtensions
1313
{
14+
1415
/// <summary>
1516
/// Enable NLog as logging provider in .NET Core.
1617
/// </summary>
@@ -28,6 +29,47 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory)
2829
/// <param name="options">NLog options</param>
2930
/// <returns>ILoggerFactory for chaining</returns>
3031
public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
32+
{
33+
ConfigureHiddenAssemblies();
34+
35+
using (var provider = new NLogLoggerProvider(options))
36+
{
37+
factory.AddProvider(provider);
38+
}
39+
return factory;
40+
}
41+
42+
#if NETSTANDARD2_0
43+
44+
/// <summary>
45+
/// Enable NLog as logging provider in .NET Core.
46+
/// </summary>
47+
/// <param name="factory"></param>
48+
/// <returns>ILoggerFactory for chaining</returns>
49+
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
50+
{
51+
return AddNLog(factory, null);
52+
}
53+
54+
/// <summary>
55+
/// Enable NLog as logging provider in .NET Core.
56+
/// </summary>
57+
/// <param name="factory"></param>
58+
/// <param name="options">NLog options</param>
59+
/// <returns>ILoggerFactory for chaining</returns>
60+
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
61+
{
62+
ConfigureHiddenAssemblies();
63+
64+
using (var provider = new NLogLoggerProvider(options))
65+
{
66+
factory.AddProvider(provider);
67+
}
68+
return factory;
69+
}
70+
#endif
71+
72+
private static void ConfigureHiddenAssemblies()
3173
{
3274
//ignore this
3375
LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging")));
@@ -43,24 +85,19 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
4385
{
4486
//ignore
4587
}
46-
47-
LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
4888

49-
using (var provider = new NLogLoggerProvider(options))
50-
{
51-
factory.AddProvider(provider);
52-
}
53-
return factory;
89+
LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
5490
}
5591

5692
/// <summary>
5793
/// Apply NLog configuration from XML config.
5894
/// </summary>
59-
/// <param name="env"></param>
95+
/// <param name="loggerFactory"></param>
6096
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
61-
/// <returns>Current configuration for chaining.</returns>
62-
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, string configFileRelativePath)
97+
/// <returns>Current configuration for chaining.</returns>
98+
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, string configFileRelativePath)
6399
{
100+
64101
#if NETCORE
65102
var rootPath = System.AppContext.BaseDirectory;
66103
#else
@@ -71,24 +108,24 @@ public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, string
71108
return ConfigureNLog(fileName);
72109
}
73110

74-
/// <summary>
75-
/// Apply NLog configuration from config object.
76-
/// </summary>
77-
/// <param name="env"></param>
78-
/// <param name="config">New NLog config.</param>
79-
/// <returns>Current configuration for chaining.</returns>
80-
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, LoggingConfiguration config)
81-
{
82-
LogManager.Configuration = config;
111+
/// <summary>
112+
/// Apply NLog configuration from config object.
113+
/// </summary>
114+
/// <param name="loggerFactory"></param>
115+
/// <param name="config">New NLog config.</param>
116+
/// <returns>Current configuration for chaining.</returns>
117+
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, LoggingConfiguration config)
118+
{
119+
LogManager.Configuration = config;
83120

84-
return config;
85-
}
121+
return config;
122+
}
86123

87124
/// <summary>
88125
/// Apply NLog configuration from XML config.
89126
/// </summary>
90127
/// <param name="fileName">absolute path NLog configuration file.</param>
91-
/// <returns>Current configuration for chaining.</returns>
128+
/// <returns>Current configuration for chaining.</returns>
92129
private static LoggingConfiguration ConfigureNLog(string fileName)
93130
{
94131
var config = new XmlLoggingConfiguration(fileName, true);

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Description>NLog provider for Microsoft.Extensions.Logging</Description>
44
<VersionPrefix>1.0.0-rtm-beta5</VersionPrefix>
55
<Authors>Microsoft;Julian Verdurmen</Authors>
6-
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
6+
<TargetFrameworks>net451;netstandard1.3;netstandard2.0</TargetFrameworks>
77
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
88
<AssemblyName>NLog.Extensions.Logging</AssemblyName>
99
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
@@ -18,22 +18,27 @@
1818
<RepositoryType>git</RepositoryType>
1919
<RepositoryUrl>git://github.com/NLog/NLog.Extensions.Logging</RepositoryUrl>
2020
</PropertyGroup>
21-
<ItemGroup>
22-
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.2" />
23-
</ItemGroup>
21+
2422
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
25-
<PackageReference Include="NLog" Version="4.4.11" />
23+
<PackageReference Include="NLog" Version="4.4.12" />
24+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
2625
<Reference Include="System.Xml" />
2726
<Reference Include="System.Runtime" />
2827
<Reference Include="System.Xml.Serialization" />
2928
<Reference Include="System" />
3029
<Reference Include="Microsoft.CSharp" />
3130
</ItemGroup>
32-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
31+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard2.0'">
3332
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
3433
</PropertyGroup>
3534
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
36-
<PackageReference Include="NLog" Version="5.0.0-beta09" />
35+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
36+
<PackageReference Include="NLog" Version="5.0.0-beta11" />
37+
<PackageReference Include="System.AppContext" Version="4.3.0" />
38+
</ItemGroup>
39+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
40+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
41+
<PackageReference Include="NLog" Version="4.5.0-beta01" />
3742
<PackageReference Include="System.AppContext" Version="4.3.0" />
3843
</ItemGroup>
3944
</Project>

src/NLog.Extensions.Logging/NLogLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId
3838
eventInfo.Exception = exception;
3939
if (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name))
4040
{
41-
/// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
41+
// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
4242
var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple<string, string, string>(null, null, null);
4343
var eventIdSeparator = _options.EventIdSeparator ?? string.Empty;
4444
if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))

test/NLog.Extensions.Logging.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
9-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
8+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
9+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
1010
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
1111
<PackageReference Include="xunit" Version="2.2.0" />
1212
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />

0 commit comments

Comments
 (0)