Skip to content

Commit

Permalink
NLog + log4net NetStandard Support
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Apr 2, 2018
1 parent c6f75ff commit a4023cc
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 34 deletions.
10 changes: 1 addition & 9 deletions src/Castle.Core.Tests/Castle.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageReference Include="NUnit.Console" Version="3.6.1" />
<PackageReference Include="NUnitLite" Version="3.6.1" />
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="NLog" Version="4.5.0" />
<PackageReference Include="Serilog" Version="2.0.0" />
<PackageReference Include="Serilog.Sinks.TextWriter" Version="2.0.0" />
<PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
Expand All @@ -66,20 +67,11 @@
<PackageReference Include="System.Xml.XPath" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XDocument" Version="4.3.0" />
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<PackageReference Include="NLog" Version="4.4.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<ProjectReference Include="..\Castle.Core\Castle.Core.csproj" />
<ProjectReference Include="..\Castle.Services.Logging.log4netIntegration\Castle.Services.Logging.log4netIntegration.csproj" />
<ProjectReference Include="..\Castle.Services.Logging.NLogIntegration\Castle.Services.Logging.NLogIntegration.csproj" />
<ProjectReference Include="..\Castle.Services.Logging.SerilogIntegration\Castle.Services.Logging.SerilogIntegration.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp1.1'">
<ProjectReference Include="..\Castle.Core\Castle.Core.csproj" />
<ProjectReference Include="..\Castle.Services.Logging.SerilogIntegration\Castle.Services.Logging.SerilogIntegration.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Windows.Forms" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\..\buildscripts\common.props"></Import>

<PropertyGroup>
<TargetFrameworks>net35;net40;net45</TargetFrameworks>
<TargetFrameworks>net35;net40;net45;netstandard1.3</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -32,17 +32,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.4.1" />
<PackageReference Include="NLog" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Castle.Core\Castle.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Configuration" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\buildscripts\CommonAssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ThreadContextProperties : IContextProperties
public object this[string key]
{
get { return MappedDiagnosticsContext.Get(key); }
set { MappedDiagnosticsContext.Set(key, value.ToString()); }
set { MappedDiagnosticsContext.Set(key, value); }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ThreadContextStack : IContextStack
/// <exception cref = "NotImplementedException" />
public int Count
{
get { throw new NotSupportedException("NLog does not implement a Count of it's stack."); }
get { return NestedDiagnosticsContext.GetAllObjects().Length; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@
<ProjectReference Include="..\Castle.Core\Castle.Core.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net45'">
<Reference Include="System.Configuration" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\..\buildscripts\common.props"></Import>

<PropertyGroup>
<TargetFrameworks>net35;net40;net45</TargetFrameworks>
<TargetFrameworks>net35;net40;net45;netstandard1.3</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -21,10 +21,6 @@
<PackageTags>castle logging log4net</PackageTags>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net35'OR'$(TargetFramework)'=='net40'OR'$(TargetFramework)'=='net45'">
<Reference Include="System.Configuration" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
<PackageReference Include="System.Collections.Specialized" Version="4.3.0" />
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ namespace Castle.Services.Logging.Log4netIntegration
{
using System;
using System.IO;
using System.Reflection;

using Castle.Core.Logging;

using log4net;
using log4net.Config;


public class ExtendedLog4netFactory : AbstractExtendedLoggerFactory
{
#if NET35 || NET40
static readonly Assembly _callingAssembly = typeof(Log4netFactory).Assembly;
#else
static readonly Assembly _callingAssembly = typeof(Log4netFactory).GetTypeInfo().Assembly;
#endif

public ExtendedLog4netFactory()
: this(Log4netFactory.defaultConfigFileName)
{
Expand All @@ -41,13 +49,13 @@ public ExtendedLog4netFactory(bool configuredExternally)
}

var file = GetConfigFile(Log4netFactory.defaultConfigFileName);
XmlConfigurator.ConfigureAndWatch(file);
XmlConfigurator.ConfigureAndWatch(LogManager.GetRepository(_callingAssembly), file);
}

public ExtendedLog4netFactory(String configFile)
{
var file = GetConfigFile(configFile);
XmlConfigurator.ConfigureAndWatch(file);
XmlConfigurator.ConfigureAndWatch(LogManager.GetRepository(_callingAssembly), file);
}

/// <summary>
Expand All @@ -56,15 +64,15 @@ public ExtendedLog4netFactory(String configFile)
/// <param name="config"> </param>
public ExtendedLog4netFactory(Stream config)
{
XmlConfigurator.Configure(config);
XmlConfigurator.Configure(LogManager.GetRepository(_callingAssembly), config);
}

/// <summary>
/// Creates a new extended logger.
/// </summary>
public override IExtendedLogger Create(string name)
{
var log = LogManager.GetLogger(name);
var log = LogManager.GetLogger(_callingAssembly, name);
return new ExtendedLog4netLogger(log, this);
}

Expand Down
14 changes: 10 additions & 4 deletions src/Castle.Services.Logging.log4netIntegration/Log4netFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Castle.Services.Logging.Log4netIntegration
{
using System;
using System.IO;
using System.Reflection;

using Castle.Core.Logging;

Expand All @@ -25,6 +26,11 @@ namespace Castle.Services.Logging.Log4netIntegration
public class Log4netFactory : AbstractLoggerFactory
{
internal const string defaultConfigFileName = "log4net.config";
#if NET35 || NET40
static readonly Assembly _callingAssembly = typeof(Log4netFactory).Assembly;
#else
static readonly Assembly _callingAssembly = typeof(Log4netFactory).GetTypeInfo().Assembly;
#endif

public Log4netFactory() : this(defaultConfigFileName)
{
Expand All @@ -33,7 +39,7 @@ public Log4netFactory() : this(defaultConfigFileName)
public Log4netFactory(String configFile)
{
var file = GetConfigFile(configFile);
XmlConfigurator.ConfigureAndWatch(file);
XmlConfigurator.ConfigureAndWatch(LogManager.GetRepository(_callingAssembly), file);
}

/// <summary>
Expand All @@ -48,7 +54,7 @@ public Log4netFactory(bool configuredExternally)
}

var file = GetConfigFile(defaultConfigFileName);
XmlConfigurator.ConfigureAndWatch(file);
XmlConfigurator.ConfigureAndWatch(LogManager.GetRepository(_callingAssembly), file);
}

/// <summary>
Expand All @@ -57,12 +63,12 @@ public Log4netFactory(bool configuredExternally)
/// <param name="config"> </param>
public Log4netFactory(Stream config)
{
XmlConfigurator.Configure(config);
XmlConfigurator.Configure(LogManager.GetRepository(_callingAssembly), config);
}

public override ILogger Create(String name)
{
var log = LogManager.GetLogger(name);
var log = LogManager.GetLogger(_callingAssembly, name);
return new Log4netLogger(log, this);
}

Expand Down

0 comments on commit a4023cc

Please sign in to comment.