log4net 日志文件已创建但始终为空
log4net log file is created but always empty
我有一个网站项目,我想在其中通过 log4net 登录。当我尝试时,日志文件已创建,但它始终为空。
这是 Global.asax 文件的一部分:
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Log4Net.config")));
var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Logging started");
}
这里是 Log4Net.config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="admin_log.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
有什么想法吗?
您的配置可能有问题,启用内部调试以查看问题所在:
<configuration>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
我有一个网站项目,我想在其中通过 log4net 登录。当我尝试时,日志文件已创建,但它始终为空。
这是 Global.asax 文件的一部分:
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Log4Net.config")));
var log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Info("Logging started");
}
这里是 Log4Net.config:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="INFO"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ConsoleAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="admin_log.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
</log4net>
有什么想法吗?
您的配置可能有问题,启用内部调试以查看问题所在:
<configuration>
...
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>