tags:

views:

563

answers:

2

Hi,

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 each log file. how can I write a message to a specific appender?

many thanks,

ofer

+6  A: 

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 Sajip
Nice ..:) however, I'll need to write separate messages to each log file. how can I write a message to a specific appender? thanks. ofer
ofer
@ofer- see my answer on how to do that.
RichardOD
+3  A: 

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.

RichardOD
I have this *same* setup in one of my apps and it doesnt work for me :(
Román