Adding a log4Net provider in .net core console app

I recently was developing a console application in .net core, where I had to use log4net logging.

In the standard core approach we can use:

But this is .net core console application, where I’m creating LoggerFactory on my own, so it would not work.

In order to solve it, I had to implement my own Log4NetProvider, that would implement ILoggerProvider.

And the implementation of an actual logger:

One last touch is adding an extension for ILoggerFactory to be able to use AddLog4Net.

In my DI container registration, I added code:

Now it will all works!

To see the whole code, go to my GitHub repository and check this commit:

Hope that works for you!

12 thoughts on “Adding a log4Net provider in .net core console app

  1. Great! Thanks a lot for this. I had some issues with usings however in the end this worked like a charm!

    1. Hi Jocelyne,
      If you follow all the steps, you can inject it in your class constructor:

      public class CustomService : ICustomService
      private readonly ILogger _logger;

      public CustomService(ILoggerFactory loggerFactory)
      _logger = loggerFactory.CreateLogger(typeof(CustomService));

  2. Hi

    I tried your classes and have a problem with debug messages.
    All messages are displayed except debug ones.

    I put _skipDiagnosticLogs=false;

    In my log4net.config I tried with


    Nothing works

    What is strange is If I make a “step by step” on a Line Logger.LogDebug(“hello debug !”); it doesn’t enter in Log4NetLogger class.
    If I make a “step by step” on a Line Logger.LogInformation(“hello info !”); it enters in Log4NetLogger class.

    Any idea?

    1. Hello Remy!

      Not sure if you solved it or worked around this, but I’ll leave my findings here.
      Apparently the default LoggingFactory you instantiate has the minimum logging level set. The below code will set the log level back to the lowest level of trace.

      ILoggerFactory factory = LoggerFactory.Create(builder =>

    1. So a quick and dirty way would be to create things manually.

      In the program.cs Main method:

      var logger = new Log4NetProvider(“log4net.config”, false);
      var log = logger.CreateLogger(“Program”);
      log.Log(LogLevel.Information, “Hello world”);

      You can change the log4net.config file to:

      This will create a log.txt file in the folder running the exe.

      Make sure that the config file is set to “COPY IF NEWER”

      Hope this helps.

  3. Hi Michal,

    I am struggling running the log4net in my console app. Do you have this code in github so I could check it out and figure out, what is wrong in my implementation.

      1. Thanks a lot. It’s been very helpful. I am using the IServiceCollection for registering my services and all I had to add was this line of code: services.AddSingleton(loggerFactory);

Leave a Reply

Your email address will not be published. Required fields are marked *