我想在我的应用中使用两个不同的记录器,因此我可以将不同的消息记录到不同的文件中。 我的app.config看起来像
<log4net>
<appender name="Appender1" type="log4net.Appender.RollingFileAppender">
<file value="File1" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<datePattern value=".yyyy-MM-dd'.txt'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message %exception{Message} %newline" />
<IgnoresException value="False" />
</layout>
</appender>
<appender name="Appender2" type="log4net.Appender.RollingFileAppender">
<file value="File2.Log"/>
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
<rollingStyle value="Size" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %message %newline" />
<IgnoresException value="True" />
</layout>
</appender>
<logger Name="Logger1" additivity="false">
<level value="INFO"/>
<appender-ref ref="Appender1" />
</logger>
<logger Name="Logger2" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="Appender2" />
</logger>
在类中,记录器初始化为:
private static readonly ILog Log1 = LogManager.GetLogger("Logger1");
private static readonly ILog Log2 = LogManager.GetLogger("Logger2");
我写信息:
Log1.Info("Message1");
Log2.Debug("Message1");
创建日志文件但它们为空。我不确定我做错了什么。
答案 0 :(得分:2)
这适用于我..你可以按照你想要的方式调整模式
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Abc.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="45" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] %line - %message%newline" />
</layout>
</appender>
<root>
<level value="All" />
<appender-ref ref="RollingLogFileAppender" />
</root>
<!--For Call BAck Only-->
<appender name="Summary" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Callback.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="45" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] %line - %message%newline" />
</layout>
</appender>
<logger additivity="false" name="Summary">
<level value="DEBUG"/>
<appender-ref ref="Summary" />
</logger>
<!--One More Log-->
<appender name="Quartz" type="log4net.Appender.RollingFileAppender">
<file value="Logs/Quartz.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="45" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] %line - %message%newline" />
</layout>
</appender>
<logger additivity="false" name="Quartz">
<level value="DEBUG"/>
<appender-ref ref="Quartz" />
</logger>
</log4net>
答案 1 :(得分:2)
我有两个 appender,他们都多次记录每个错误。
我需要的属性在接受的答案中,additivity
<logger additivity="false" name="Summary">
<level value="DEBUG"/>
<appender-ref ref="Summary" />
</logger>
现在错误转到正确的文件并且不会被多次记录
答案 2 :(得分:0)
我认为你错过了这个参数:
<"param name="ImmediateFlush" value="true" />
每次调用Write方法时,ImmediateFlush都会刷新内容。
否则,你必须自己打电话给Flush:)