Log4Net config with .NET Core app

只谈情不闲聊 提交于 2020-08-07 01:33:21

问题


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

What should I pass?

My code is

   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();
    }

But with null it crashes

Any suggestions?


回答1:


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());

Depending on your setup, you might need to use Assembly.GetExecutingAssembly() instead.



来源:https://stackoverflow.com/questions/50835087/log4net-config-with-net-core-app

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