本文介绍了.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 anwser,它推荐使用 .net 标准 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()在 .net 标准 1.3 中不受支持.

In the post LogManager.GetRepository(Assembly.GetEntryAssembly()) method used, but Assembly.GetEntryAssembly() is not supported in .net standard 1.3.

官方文档也被破坏了,因为XmlConfigurator.Configure 方法签名与其示例用法不匹配.

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 Framework 和 .NET Core 中均受支持.

Assembly.GetEntryAssembly() is supported in both the full .NET Framework and .NET Core.

这篇关于.net 标准 1.3 项目中的 log4net 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:50