log4net configuration in .net standard 1.3 project

南楼画角 提交于 2020-01-24 15:36:38

问题


I 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?


回答1:


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.



来源:https://stackoverflow.com/questions/53500512/log4net-configuration-in-net-standard-1-3-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!