5

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?

Niv
  • 2,294
  • 5
  • 29
  • 41
  • I think this is what you are looking for http://stackoverflow.com/questions/1372435/configure-log4net-to-write-to-multiple-files – Rajeev Bera Jul 29 '13 at 06:54
  • I think so too, and the syntax seems ok, but it's not working. It compiles and runs just fine.However, all the isLevelEnabled members of all the loggers are set to TRUE even though that's not how they're set, and the test logs I've written are not being logged. – Niv Jul 29 '13 at 07:13
  • Try this - [link](http://stackoverflow.com/questions/308436/log4net-programmatically-specify-multiple-loggers-with-multiple-file-appenders) – Nilesh Jul 29 '13 at 08:02

1 Answers1

2

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.

Peter
  • 27,590
  • 8
  • 64
  • 84
  • I have 2 questions: 1)After reading a bit, I noticed I didn't define a element in my config. Should I define a with an ? 2)I now noticed, using the internal log4net debug, that my new config file doesn't take at all - it still uses the old config! All the writing I do to the logError, logResponse, logRequest are sent as if they're sent using the old logger I had (They're written to a log.txt file I used before). I tried cleaning my project and it still doesn't work. – Niv Jul 29 '13 at 09:20
  • 1
    Just a note, `System.Reflection.MethodBase.GetCurrentMethod().DeclaringType` does work in this context... it's evaluationg to the type of the class the logger is declared inside of. So you're ending up with a logger name like `MyCorp.MyClass` instead of `ErrorAppender` like you wanted. – John Gibb Jan 22 '14 at 21:17