问题
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!
回答1:
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>
来源:https://stackoverflow.com/questions/4862868/configuring-log4net-to-write-to-different-files-based-on-log-level