I'm using log4net to log a project.
I want to log into 3 different files : requests, responses, and errors.
<log4net debug="true">
<!--To turn off an appender, simply set it's threshold value to "OFF"-->
<appender type="log4net.Appender.RollingFileAppender" name="RequestAppender">
<file value="requests.txt" />
<threshold value="INFO" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender type="log4net.Appender.RollingFileAppender" name="ResponseAppender">
<file value="responses.txt" />
<threshold value="INFO" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<appender type="log4net.Appender.RollingFileAppender" name="ErrorAppender">
<file value="errors.txt" />
<threshold value="ERROR" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
</log4net>
However, I do not know how to get an instance for each logger in the .NET side. With a standrard, 1 appender config, I used to following:
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
But this doesn't specify the appenderName, and I didn't find a way to specify it.
Any ideas?
Thanks!
EDIT: I think I'm missing something as I have no definition in my config file. I do not understand the separation between the loggers and appenders.
EDIT #2: I noticed something weird. Before the multiple 3 loggers config I had a 1 logger config. now, the writings I write into the 3 loggers write to that log file, though It's no longer in the config file. For some reason, it doesn't load the new config file. How can I force it to do so?
To use the names you want you can make 3 loggers:
private static readonly log4net.ILog logError = log4net.LogManager.GetLogger("ErrorAppender");
private static readonly log4net.ILog logResponse = log4net.LogManager.GetLogger("ResponseAppender");
private static readonly log4net.ILog logRequest = log4net.LogManager.GetLogger("RequestAppender");
When logging a log message you have to use the correct logger. Your appenders are configured to handle each logger in a seperate file.
The problem with your current code is the:
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType
Because you use static logger the loggers names can probably not formed correctly. There is no System.Reflection.MethodBase.GetCurrentMethod()
because when the static initalize is called there is not GetCurrentMethod
. You can change that to typeof(..).Name
. However you appenders need to be reconfigured to log your classnames.