开发者

Configuring log4net to write to different files based on log level

开发者 https://www.devze.com 2023-02-07 17:40 出处:网络
I\'m开发者_运维百科 setting up log4net and want to write debug messages in \"debug.log\", info messages in \"info.log\" and so on.

I'm开发者_运维百科 setting up log4net and want to write debug messages in "debug.log", info messages in "info.log" and so on.

To this end, I use several appenders, such as:

<appender name="DebugLogger" type="log4net.Appender.RollingFileAppender">
   <file value="..\Logs\Debug.log" />
   <threshold value="DEBUG" />
   <appendToFile value="true" />
   <rollingStyle value="Size" />
   <maxSizeRollBackups value="10" />
   <maximumFileSize value="1MB" />
   <staticLogFileName value="true" />
   <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] -    %message%newline" />
   </layout>

and similar ones for INFO, WARN and ERROR levels. This works ok, but seems like a lot of extra work, because all those appenders differ only on threshold value (debug or warn etc) and file name (debug.log or warn.log etc)

Is there any better way to achieve my goal? Maybe something like declaring "base appenders" first.

(on a separate note - if that's not possible in log4net but possible in NLog, I'd be grateful to know as well)

Thanks!


Ok, this is not possible in log4net - just wanted to close the question.

I checked out NLog though, and apparently it's quite easy there (see $level variable in the config):

<?xml version="1.0" encoding="utf-8" ?>
<nlog>
   <variable name="logDirectory" value="${basedir}\..\Logs"/>
   <targets>
       <target name="file" type="File" fileName="${logDirectory}\${level}.log" />
   </targets>
   <rules>
       <logger name="*" minlevel="Debug" writeTo="file" />
   </rules>
</nlog>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号