.net标准1.3项目中的log4net配置 [英] log4net configuration in .net standard 1.3 project
问题描述
我正在尝试将c#项目从.net Framework v4.6迁移到.net标准. 该项目具有log4net v2.0.8依赖性.
I'm trying to migrate a c# project from .net framework v4.6 to .net standard. The project has log4net v2.0.8 dependency.
我发现了这个 SO ananser ,它建议使用.net standard 1.3,并引用了
I found this SO anwser, which recommends to use .net standard 1.3 and gives reference to this post to get more detailed solution.
使用XmlConfigurator.Configure
方法配置log4net时出现问题,该方法要求ILoggerRepository
作为第一个参数.
The problem occurs when configuring log4net with XmlConfigurator.Configure
method, which requires ILoggerRepository
as the first argument.
在使用的LogManager.GetRepository(Assembly.GetEntryAssembly())
方法中,但Assembly.GetEntryAssembly()
是 .
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.
那么,如何在.net标准1.3项目中配置log4net?
So, how can I configure log4net in .net standard 1.3 project?
推荐答案
在您的.NET Standard 1.3类库项目中,在方法的签名中提供一个Assembly
参数,并注意Log4net
配置,例如:
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"));
// ...
}
在完整的.NET Framework或.NET Core中开发的实际应用程序中调用此方法,并通过例如Assembly.GetEntryAssembly()
传入此Assembly
参数.
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()
.
这篇关于.net标准1.3项目中的log4net配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!