I\'d like to write log to 2 different log files from the same process.
is that possible thing to do using log4net?
I\'ll need to write separate messages to
I wanted to log all messages to root logger, and to have a separate log with errors, here is how it can be done:
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="allMessages.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ErrorsFileAppender" type="log4net.Appender.FileAppender">
<file value="errorsLog.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ErrorsFileAppender" />
</root>
</log4net>
Notice the use of filter element.
These answers were helpful, but I wanted to share my answer with both the app.config part and the c# code part, so there is less guessing for the next person.
<log4net>
<appender name="SomeName" type="log4net.Appender.RollingFileAppender">
<file value="c:/Console.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
</appender>
<appender name="Summary" type="log4net.Appender.FileAppender">
<file value="SummaryFile.log" />
<appendToFile value="true" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="SomeName" />
</root>
<logger additivity="false" name="Summary">
<level value="DEBUG"/>
<appender-ref ref="Summary" />
</logger>
</log4net>
Then in code:
ILog Log = LogManager.GetLogger("SomeName");
ILog SummaryLog = LogManager.GetLogger("Summary");
Log.DebugFormat("Processing");
SummaryLog.DebugFormat("Processing2"));
Here c:/Console.txt will contain "Processing" ... and \SummaryFile.log will contain "Processing2"
Use below XML configuration to configure logs into two or more files:
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender2" type="log4net.Appender.RollingFileAppender">
<file value="logs\log1.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<logger additivity="false" name="RollingLogFileAppender2">
<level value="All"/>
<appender-ref ref="RollingLogFileAppender2" />
</logger>
</log4net>
Above XML configuration logs into two different files. To get specific instance of logger programmatically:
ILog logger = log4net.LogManager.GetLogger ("RollingLogFileAppender2");
You can append two or more appender elements inside log4net root element for logging into multiples files.
More info about above XML configuration structure or which appender is best for your application, read details from below links:
https://logging.apache.org/log4net/release/manual/configuration.html https://logging.apache.org/log4net/release/sdk/index.html
Yes, just add multiple FileAppenders to your logger. For example:
<log4net>
<appender name="File1Appender" type="log4net.Appender.FileAppender">
<file value="log-file-1.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline" />
</layout>
</appender>
<appender name="File2Appender" type="log4net.Appender.FileAppender">
<file value="log-file-2.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="File1Appender" />
<appender-ref ref="File2Appender" />
</root>
</log4net>
Vinay is correct. In answer to your comment in his answer, one way you can do it is as follows:
<root>
<level value="ALL" />
<appender-ref ref="File1Appender" />
</root>
<logger name="SomeName">
<level value="ALL" />
<appender-ref ref="File1Appender2" />
</logger>
This is how I have done it in the past. Then something like this for the other log:
private static readonly ILog otherLog = LogManager.GetLogger("SomeName");
And you can get your normal logger as follows:
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
Read the loggers and appenders section of the documentation to understand how this works.