.net标准1.3项目中的log4net配置 [英] log4net configuration in .net standard 1.3 project

查看:66
本文介绍了.net标准1.3项目中的log4net配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆