logo       

RE: Separate log records into different files by log level: msg#00022

log.log4net.user

Subject: RE: Separate log records into different files by log level

Marc,

To log to different files for specific levels you need to create an
appender for each level and then use an <appender-ref> in the <root> to
append to each of the appenders. Then you need to specify on the
<appender> a filter to use to restrict the events that are written to
each file.

If you only want INFO level messages in the file you will need to use a
LevelMatchFilter on the appender:

<!-- This filter will accept INFO events. -->
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>

<!-- The filter is a chain, therefore we have to explicitly deny the
other events -->
<filter type="log4net.Filter.DenyAllFilter"/>

So a complete example with DEBUG and INFO going to separate files would
be:

<log4net>

<appender name="File_Info" type="log4net.Appender.FileAppender">
<file value="log-file1.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="INFO" />
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
</appender>

<appender name="File_Error" type="log4net.Appender.FileAppender">
<file value="log-file2.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR" />
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="File_Info" />
<appender-ref ref="File_Error" />
</root>
</log4net>


Note that in the above configuration messages with levels not exactly
equal to INFO or ERROR will be lost because the appenders are ignoring
them. You would need to add additional appenders for each output file.

If the built-in filters don't do what you need you can easily write a
custom filter in your own assembly.

Nicko

> -----Original Message-----
> From: Marc Hoffmann [mailto:marchoffmann@xxxxxxxxx]
> Sent: 09 February 2005 00:49
> To: log4net-user@xxxxxxxxxxxxxxxxxx
> Subject: Separate log records into different files by log level
>
> I have tried to search the forum for this topic, but with no
> luck, so I am posting a (hopefully) new question.
>
> I am trying to use log4net with an ASP.Net Web Service and it
> works great with the FileAppender or RollingFileAppender.
> However, all log records for the defined log level or above
> flow into the same output file.
>
> My ultimate goal is to direct log entries for each level to a
> separate log file, i.e. log entries recorded with Log.Error()
> go to an ERROR.log file, Log.Debug() entries go to a
> DEBUG.log file, etc., but each file only contains the log
> entries associated with that level.
>
> Is that possible with log4net? And if so, how?
>
> I have tried various combinations of things, but nothing
> seems to achieve that goal. Any help or insight would be appreciated.
>
> Thank you.
>
> Marc
>



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise