Skip to content

Commit

Permalink
Merge pull request #132 from NLog/netstandard2
Browse files Browse the repository at this point in the history
.NET Standard 2 support
  • Loading branch information
304NotModified authored Sep 26, 2017
2 parents 3b7e471 + fe0d6fe commit 768db12
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 32 deletions.
81 changes: 59 additions & 22 deletions src/NLog.Extensions.Logging/ConfigureExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace NLog.Extensions.Logging
/// </summary>
public static class ConfigureExtensions
{

/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// </summary>
Expand All @@ -28,6 +29,47 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory)
/// <param name="options">NLog options</param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
{
ConfigureHiddenAssemblies();

using (var provider = new NLogLoggerProvider(options))
{
factory.AddProvider(provider);
}
return factory;
}

#if NETSTANDARD2_0

/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// </summary>
/// <param name="factory"></param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory)
{
return AddNLog(factory, null);
}

/// <summary>
/// Enable NLog as logging provider in .NET Core.
/// </summary>
/// <param name="factory"></param>
/// <param name="options">NLog options</param>
/// <returns>ILoggerFactory for chaining</returns>
public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
{
ConfigureHiddenAssemblies();

using (var provider = new NLogLoggerProvider(options))
{
factory.AddProvider(provider);
}
return factory;
}
#endif

private static void ConfigureHiddenAssemblies()
{
//ignore this
LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging")));
Expand All @@ -43,24 +85,19 @@ public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOp
{
//ignore
}

LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);

using (var provider = new NLogLoggerProvider(options))
{
factory.AddProvider(provider);
}
return factory;
LogManager.AddHiddenAssembly(typeof(ConfigureExtensions).GetTypeInfo().Assembly);
}

/// <summary>
/// Apply NLog configuration from XML config.
/// </summary>
/// <param name="env"></param>
/// <param name="loggerFactory"></param>
/// <param name="configFileRelativePath">relative path to NLog configuration file.</param>
/// <returns>Current configuration for chaining.</returns>
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, string configFileRelativePath)
/// <returns>Current configuration for chaining.</returns>
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, string configFileRelativePath)
{

#if NETCORE
var rootPath = System.AppContext.BaseDirectory;
#else
Expand All @@ -71,24 +108,24 @@ public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, string
return ConfigureNLog(fileName);
}

/// <summary>
/// Apply NLog configuration from config object.
/// </summary>
/// <param name="env"></param>
/// <param name="config">New NLog config.</param>
/// <returns>Current configuration for chaining.</returns>
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory env, LoggingConfiguration config)
{
LogManager.Configuration = config;
/// <summary>
/// Apply NLog configuration from config object.
/// </summary>
/// <param name="loggerFactory"></param>
/// <param name="config">New NLog config.</param>
/// <returns>Current configuration for chaining.</returns>
public static LoggingConfiguration ConfigureNLog(this ILoggerFactory loggerFactory, LoggingConfiguration config)
{
LogManager.Configuration = config;

return config;
}
return config;
}

/// <summary>
/// Apply NLog configuration from XML config.
/// </summary>
/// <param name="fileName">absolute path NLog configuration file.</param>
/// <returns>Current configuration for chaining.</returns>
/// <returns>Current configuration for chaining.</returns>
private static LoggingConfiguration ConfigureNLog(string fileName)
{
var config = new XmlLoggingConfiguration(fileName, true);
Expand Down
19 changes: 12 additions & 7 deletions src/NLog.Extensions.Logging/NLog.Extensions.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description>NLog provider for Microsoft.Extensions.Logging</Description>
<VersionPrefix>1.0.0-rtm-beta5</VersionPrefix>
<Authors>Microsoft;Julian Verdurmen</Authors>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
<TargetFrameworks>net451;netstandard1.3;netstandard2.0</TargetFrameworks>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>NLog.Extensions.Logging</AssemblyName>
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
Expand All @@ -18,22 +18,27 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/NLog/NLog.Extensions.Logging</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<PackageReference Include="NLog" Version="4.4.11" />
<PackageReference Include="NLog" Version="4.4.12" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
<Reference Include="System.Xml" />
<Reference Include="System.Runtime" />
<Reference Include="System.Xml.Serialization" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' Or '$(TargetFramework)' == 'netstandard2.0'">
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="NLog" Version="5.0.0-beta09" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.2" />
<PackageReference Include="NLog" Version="5.0.0-beta11" />
<PackageReference Include="System.AppContext" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
<PackageReference Include="NLog" Version="4.5.0-beta01" />
<PackageReference Include="System.AppContext" Version="4.3.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/NLog.Extensions.Logging/NLogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId
eventInfo.Exception = exception;
if (!_options.IgnoreEmptyEventId || eventId.Id != 0 || !string.IsNullOrEmpty(eventId.Name))
{
/// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
// Attempt to reuse the same string-allocations based on the current <see cref="NLogProviderOptions.EventIdSeparator"/>
var eventIdPropertyNames = _eventIdPropertyNames ?? new Tuple<string, string, string>(null, null, null);
var eventIdSeparator = _options.EventIdSeparator ?? string.Empty;
if (!ReferenceEquals(eventIdPropertyNames.Item1, eventIdSeparator))
Expand Down
4 changes: 2 additions & 2 deletions test/NLog.Extensions.Logging.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
Expand Down

0 comments on commit 768db12

Please sign in to comment.