Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4net configuration in .net standard 1.3 project

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?

like image 704
Sanan Fataliyev Avatar asked Mar 05 '23 00:03

Sanan Fataliyev


1 Answers

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.

like image 83
pfx Avatar answered Mar 10 '23 22:03

pfx