.net-corelog4netlog4net-configuration

Log4Net config with .NET Core app


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?


Solution

  • 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.