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>
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.
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.
In your case, the log file will be in bin\Debug\netcoreapp3.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With