Configure Log4Net in a .NET core application

Log4net

Previously there was some workaround I used and describted here: Configure Log4Net in a .NET Core app

The alternative is to install the nuget package: Microsoft.Extensions.Logging.Log4Net.AspNetCore, then configure it:


builder.Host.ConfigureLogging((loggingBuilder) =>
{
    loggingBuilder.AddLog4Net(new Log4NetProviderOptions()
    {
        Watch = true
    });
});

Create a log4net.config file, like below:


<log4net>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFile" />
  </root>
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <appendToFile value="true" />
    <file value="..\logs\MyApp\log" />
    <encoding value="utf-8" />
    <rollingStyle value="Date" />
    <datePattern value="_yyyy.MM.dd.TXT" />
    <staticLogFileName value="false" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %-5level %logger.%method [%line] - MESSAGE: %message%newline" />
    </layout>
  </appender>
</log4net>

Post a Comment

Previous Post Next Post