Log4net 多个记录器 - 文件记录器不工作

Log4net multiple loggers - File logger not working

将配置记录到事件日志中,它适用于 SmtpSender。但是 filelogger 不记录,它在路径中创建日志文件,但不做任何调试日志。

<root>
  <level value="DEBUG" />      
  <appender-ref ref="EventLogAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
  <level value="FATAL"/>
  <appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
  <level value="DEBUG"/>
  <appender-ref ref="RollingFileAppender"/>
</logger>    

但是当我将根记录器更改为 RollingFileAppender 时,它会记录到文件,

<root>
  <level value="DEBUG" />      
  <appender-ref ref="RollingFileAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
  <level value="FATAL"/>
  <appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
  <level value="DEBUG"/>
  <appender-ref ref="RollingFileAppender"/>
</logger>    

知道为什么会这样吗?我怎样才能让文件记录器在这种情况下工作。

您没有 post appender 配置,但是找出问题所在的最简单方法是启用内部调试。这将告诉您滚动文件附加程序中出了什么问题:

There are 2 different ways to enable internal debugging in log4net. These are listed below. The preferred method is to specify the log4net.Internal.Debug option in the application's config file.

• Internal debugging can also be enabled by setting a value in the application's configuration file (not the log4net configuration file, unless the log4net config data is embedded in the application's config file). The log4net.Internal.Debug application setting must be set to the value true. For example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

This setting is read immediately on startup an will cause all internal debugging messages to be emitted.

• To enable log4net's internal debug programmatically you need to set the log4net.Util.LogLog.InternalDebugging property to true. Obviously the sooner this is set the more debug will be produced.

Internal debugging messages are written to the console and to the System.Diagnostics.Trace system. If the application does not have a console the messages logged there will be lost. Note that an application can redirect the console stream by setting the System.Console.Out. The Trace system will by default send the message to an attached debugger (where the messages will appear in the output window). If the process does not have a debugger attached then the messages are sent to the system debugger. A utility like DebugView from http://www.sysinternals.com may be used to capture these messages.

As log4net internal debug messages are written to the System.Diagnostics.Trace system it is possible to redirect those messages to a local file. You can define a trace listener by adding the following to your application's .config file:

<configuration>
    ...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>

    ...
</configuration>

Make sure that the process running your application has permission to write to this file. 

log4net faq

我设法完成了日志记录工作,事实上我是作为一个实验来做的,而且它奏效了。

向根元素添加了 RollingFileAppender,

<root>
  <level value="DEBUG" />      
  <appender-ref ref="EventLogAppender" />    
  <appender-ref ref="RollingFileAppender"/>
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>