I have set a root level logger which basically logs all.
There is a requirement to append a 3rd party information in a separate log file. Initially in the program, this would get logged even if you create a separate logger in the code, as the root captures all logging information.
private static readonly ILog Logger
= LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog ThirdPartyLogger = LogManager.GetLogger("ThirdPartyLogger");
...
...
...
ThirdPartyLogger.Info($"This gets logged in the main log too, even though the appender is not set in the root");
The trick to omit it from the root level logger is to have the "additivity" attribute set in the logger.
<appender name="ServiceAppenderInfo" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ServiceLog_Info.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="ServiceAppenderError" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ServiceLog_Error.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="ThirdPartyAppenderInfo" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ThirdParty_Info.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ServiceAppenderInfo" />
<appender-ref ref="ServiceAppenderError" />
</root>
<logger name="ServiceLogger">
<level value="INFO" />
<appender-ref ref="ServiceAppenderInfo" />
<appender-ref ref="console"/>
</logger>
<logger name="ThirdPartyLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="ThirdPartyAppenderInfo" />
</logger>
There is a requirement to append a 3rd party information in a separate log file. Initially in the program, this would get logged even if you create a separate logger in the code, as the root captures all logging information.
private static readonly ILog Logger
= LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly ILog ThirdPartyLogger = LogManager.GetLogger("ThirdPartyLogger");
...
...
...
ThirdPartyLogger.Info($"This gets logged in the main log too, even though the appender is not set in the root");
The trick to omit it from the root level logger is to have the "additivity" attribute set in the logger.
<appender name="ServiceAppenderInfo" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ServiceLog_Info.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="20" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<appender name="ServiceAppenderError" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ServiceLog_Error.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="ERROR" />
</filter>
</appender>
<appender name="ThirdPartyAppenderInfo" type="log4net.Appender.RollingFileAppender">
<file value="C:\ProgramData\Service\ThirdParty_Info.log" />
<encoding value="utf-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ServiceAppenderInfo" />
<appender-ref ref="ServiceAppenderError" />
</root>
<logger name="ServiceLogger">
<level value="INFO" />
<appender-ref ref="ServiceAppenderInfo" />
<appender-ref ref="console"/>
</logger>
<logger name="ThirdPartyLogger" additivity="false">
<level value="INFO" />
<appender-ref ref="ThirdPartyAppenderInfo" />
</logger>