Log4net 不写入日志文件
Log4net not writing to log file
我已经如下设置了 log4net,但它没有写入文件。我有 运行 带有断点的应用程序来检查异常,但没有抛出异常。请帮忙
Web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Users\yperez\Desktop\Demos\AvatarPasswordReset\mylogfile.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileApender" />
</root>
</log4net>
Global.asax
<%@ Application Language="C#" %>
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
//More empty methods below
Default.aspx
using log4net;
//Other using statements
[assembly:log4net.Config.XmlConfigurator(Watch = true)]
public partial class _Default : System.Web.UI.Page{
//More code and methods...
public void AppendToLog(string new_password, string user_named_changed, string sent)
{
try
{
string date = DateTime.Now.ToString();
string PCUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string a = String.Format("|{0}|{1}|{2}|{3}|{4}", date, PCUser, user_named_changed, new_password, sent);
ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
logger.Info(a + "\r\n");
}
catch (Exception exc) { logger_status = exc.ToString(); }
}
需要指定正确的附加程序引用。它被设置为 FileAppender。应该是 RollingFileAppender。
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
</root>
另请注意,您可以使用 .InfoFormat 而不是单独的 string.Format() 调用。
我已经如下设置了 log4net,但它没有写入文件。我有 运行 带有断点的应用程序来检查异常,但没有抛出异常。请帮忙 Web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Users\yperez\Desktop\Demos\AvatarPasswordReset\mylogfile.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileApender" />
</root>
</log4net>
Global.asax
<%@ Application Language="C#" %>
void Application_Start(object sender, EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
}
//More empty methods below
Default.aspx
using log4net;
//Other using statements
[assembly:log4net.Config.XmlConfigurator(Watch = true)]
public partial class _Default : System.Web.UI.Page{
//More code and methods...
public void AppendToLog(string new_password, string user_named_changed, string sent)
{
try
{
string date = DateTime.Now.ToString();
string PCUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string a = String.Format("|{0}|{1}|{2}|{3}|{4}", date, PCUser, user_named_changed, new_password, sent);
ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
logger.Info(a + "\r\n");
}
catch (Exception exc) { logger_status = exc.ToString(); }
}
需要指定正确的附加程序引用。它被设置为 FileAppender。应该是 RollingFileAppender。
<root>
<level value="INFO" />
<appender-ref ref="RollingFileAppender" />
</root>
另请注意,您可以使用 .InfoFormat 而不是单独的 string.Format() 调用。