Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop log4net from writing to two separate files

I'm using the below log4net configuration. I'm trying to write performance logs to a separate file but the output is going to both files. Even though I'm specifiying:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger("PerformanceMetricsLogger");

How do I stop log4net from writing to the default appender in this case?

<log4net>
  <!-- Define some output appenders -->
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">    
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="2000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppenderForPerformance" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="c:\ilap\IlapAdServerLog_%property{log4net:HostName}.peformance.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="2000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>

  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender" />
  </root>

  <logger name="PerformanceMetricsLogger">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppenderForPerformance" />
  </logger>
</log4net>
like image 239
jbenckert Avatar asked Aug 26 '10 19:08

jbenckert


People also ask

How do I use multiple Appenders in log4net?

You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.

What is rolling file Appender in log4net?

RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.

Where does log4net write to?

In your case, the log file will be in bin\Debug\netcoreapp3.

Does log4net create directory?

Log4net watches for any new file created in the folder so simply creating the . log4net configuration file triggers the update within the component that is logging. When using a file appender, the destination folder does not have to exist. Log4net creates the folder.


1 Answers

All the logger levels below root inherit the appenders from the root logger. This is why your messages are logged twice. To disable inheritance of your logger set its additivity parameter to false:

 <logger additivity="false" name="PerformanceMetricsLogger">
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppenderForPerformance" />
  </logger>
like image 114
jdecuyper Avatar answered Nov 13 '22 13:11

jdecuyper