Search code examples
c#log4netlog4net-configurationamazon-cloudwatchlogs

Log4net duplicate entries when only one (root) logger is used


My log4net configuration looks like this:

<log4net>
 <appender name="CloudWatchLogsAppender" type="CloudWatchAppender.CloudWatchLogsAppender, CloudWatchAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger.%method - %message%newline" />
  </layout>
  <groupName value="agroupname" />
  <streamName value="astreamname" />
 </appender>
 <root>
  <level value="INFO" />
  <appender-ref ref="CloudWatchLogsAppender" />
 </root>
 <logger name="Amazon">
  <level value="OFF" />
 </logger>
</log4net>

In classes, I instantiate the logger with

private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

and call the Log.Error(message); only once but in my logs, all the errors are duplicated. Also, this method is being called in a controller class for a WebApi project.

I've read in other questions that it can happen when using multiple loggers and them propagating messages upto the root logger, however, I am only using the root logger in this instance.


Solution

  • It seems that the problem was an AWS SDK specific configuration:

         <add key="AWSLogging" value="log4net" />
    

    Somehow, this duplicated the appenders and caused the double messages.

    To fix, just remove this setting or change the value to "none".