I'm trying to migrate a c# project from .net framework v4.6 to .net standard. The project has log4net v2.0.8 dependency.
I found this SO anwser, which recommends to use .net standard 1.3 and gives reference to this post to get more detailed solution.
The problem occurs when configuring log4net with XmlConfigurator.Configure
method, which requires ILoggerRepository
as the first argument.
In the post LogManager.GetRepository(Assembly.GetEntryAssembly())
method used, but Assembly.GetEntryAssembly()
is not supported in .net standard 1.3.
Official documentation is also broken, because XmlConfigurator.Configure
method signature and it's example usage doesn't match.
So, how can I configure log4net in .net standard 1.3 project?
In your .NET Standard 1.3 class library project, provide an Assembly
argument in the signature of the method taking care of the Log4net
configuration, eg:
public static void Configure(Assembly assembly)
{
ILoggerRepository repository = LogManager.GetRepository(assembly);
XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
// ...
}
Call this method from your actual application, being developed in either the full .NET Framework or .NET Core, passing in this Assembly
argument via eg: Assembly.GetEntryAssembly()
.
Assembly.GetEntryAssembly()
is supported in both the full .NET Framework and .NET Core.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With