我创建了一个示例程序,每次运行此程序时,它都会在事件日志中创建一个虚拟条目,如下所示。如何在不创建记录器的情况下避免使用?
2014-02-24 18:04:53,557 [8]错误Log4NetSample.Program [(null)] - 错误消息
我的代码:
using log4net;
using log4net.Config;
namespace Log4NetSample
{
class Program
{
static void Main(string[] args)
{
Program pg= new Program();
pg.Log4netCode();
}
private void Log4netCode()
{
XmlConfigurator.Configure();
ILog log = LogManager.GetLogger(typeof(Program));
log.Debug("Debug message");
log.Info("Info message");
log.Warn("Warning message");
log.Error("Error message");
log.Fatal("Fatal message");
}
}
}
配置文件:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="C:\Log\MyFirstLogger.log"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="2KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="WARN" />
</filter>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<Eventid value="100"/>
<LogName value="Log4NetSample" />
<applicationName value="Log4NetSample" source="Api" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="LogFileAppender" />
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
答案 0 :(得分:0)
您的消息是来自行log.Error的实际消息。初始化配置后,您无需立即记录或测试记录器。