使用.NET Core应用程序进行Log4Net配置 [英] Log4Net config with .NET Core app
问题描述
我需要在新的.NET Core应用程序上使用log4net,该应用程序引用一些刚刚使用log4net编写的程序集.我进行了搜索,但是所有示例都仅将 FileInfo
传递给 log4net.Config.XmlConfigurator.Configure
,但是使用最新版本时,要求输入第一个参数> ILoggerRepository存储库
I need to use log4net on a new .NET Core app that references some just written assemblies which uses log4net.
I've searched around but all the examples passes just a FileInfo
to the log4net.Config.XmlConfigurator.Configure
but using the latest version asks for a first paremeter of type ILoggerRepository repository
我应该通过什么?
我的代码是
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
ContainerConfig.RegisterDependencies(configuration);
MappingConfig.RegisterMapping();
var fileInfo = new FileInfo(@"\config.log4net");
log4net.Config.XmlConfigurator.Configure(null, fileInfo);
var core = ContainerWrapper.Container.GetInstance<ICore>();
core.StartAsync().Wait();
Console.Write("Press a key to exit");
Console.ReadLine();
}
但是使用 null
会崩溃
有什么建议吗?
推荐答案
您可以通过下面的语句获取默认的存储库
,
并将该参数作为 Configure
方法的参数传递.
You can get the default repository
via the statement here below,
and pass that one as argument of the Configure
method.
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
根据您的设置,您可能需要使用 Assembly.GetExecutingAssembly()
.
Depending on your setup, you might need to use Assembly.GetExecutingAssembly()
instead.
这篇关于使用.NET Core应用程序进行Log4Net配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!