两个记录器写入同一个文件
Two loggers writing to same file
我配置了两个记录器并希望它们同时记录到事件日志和文本文件,但它们都记录到文本。
这是我的配置文件:
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="NavRiskFileImportError.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="{%level}{%date} – %message%newline"/>
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="ApplicationName" value="NavriskFromExcel" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!--loggers-->
<logger name="FileLogger">
<level value="ALL" />
<appender-ref ref="RollingFile" />
</logger>
<logger name="EventLogger">
<level value="ALL" />
<appender-ref ref="EventLogAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
在我的申请中我有:
private static ILog logger= LogManager.GetLogger("FileLogger");
private static ILog eventLogger= LogManager.GetLogger("EventLogger");
但是两个记录器都在将数据写入文件,而事件日志中没有任何内容。我做错了什么?
您必须在各个记录器上指定 additivity="false"
,否则它们会从根记录器继承附加程序:
Additivity is set to true by default, that is children inherit the
appenders of their ancestors by default. If this variable is set to
false then the appenders found in the ancestors of this logger are not
used (source)
所以这意味着:
<logger name="EventLogger" additivity="false">
这就解释了为什么两者都写入文件。
所以剩下的问题是为什么 EventLogAppender 不工作。您可以使用此代码检查配置错误:
var messages = LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>();
if (messages.Any())
{
string message = messages.Aggregate("Log4net Configuration Errors: "
+ Environment.NewLine,
(s, log) => s + log.Message + Environment.NewLine);
}
如果没有显示任何内容,enable log4net internal debugging 并检查输出。
编辑:文档说明 EventLogAppender will fail if you try to write using an event source that doesn't exist unless it is running with local administrator privileges but the event source needs to be created by an elevated process, or an installer - 必须为每台机器创建一次事件源。
我配置了两个记录器并希望它们同时记录到事件日志和文本文件,但它们都记录到文本。 这是我的配置文件:
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="NavRiskFileImportError.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<datePattern value="yyyyMMdd" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="{%level}{%date} – %message%newline"/>
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<param name="ApplicationName" value="NavriskFromExcel" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!--loggers-->
<logger name="FileLogger">
<level value="ALL" />
<appender-ref ref="RollingFile" />
</logger>
<logger name="EventLogger">
<level value="ALL" />
<appender-ref ref="EventLogAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
在我的申请中我有:
private static ILog logger= LogManager.GetLogger("FileLogger");
private static ILog eventLogger= LogManager.GetLogger("EventLogger");
但是两个记录器都在将数据写入文件,而事件日志中没有任何内容。我做错了什么?
您必须在各个记录器上指定 additivity="false"
,否则它们会从根记录器继承附加程序:
Additivity is set to true by default, that is children inherit the appenders of their ancestors by default. If this variable is set to false then the appenders found in the ancestors of this logger are not used (source)
所以这意味着:
<logger name="EventLogger" additivity="false">
这就解释了为什么两者都写入文件。
所以剩下的问题是为什么 EventLogAppender 不工作。您可以使用此代码检查配置错误:
var messages = LogManager.GetRepository().ConfigurationMessages.Cast<LogLog>();
if (messages.Any())
{
string message = messages.Aggregate("Log4net Configuration Errors: "
+ Environment.NewLine,
(s, log) => s + log.Message + Environment.NewLine);
}
如果没有显示任何内容,enable log4net internal debugging 并检查输出。
编辑:文档说明 EventLogAppender will fail if you try to write using an event source that doesn't exist unless it is running with local administrator privileges but the event source needs to be created by an elevated process, or an installer - 必须为每台机器创建一次事件源。