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 asp.net core approach we can use:

But that is asp.net core and I don’t want to reference that in my console app.

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.

Now it will all works!

 

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

Leave a Reply

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