简体   繁体   English

Log4net没有写入日志文件

[英]Log4net not writing to log file

I have setup log4net as below, but it is not writing to the file. 我已经设置了如下的log4net,但它没有写入文件。 I have ran the application with a Breakpoint to check for Exceptions but NO exception is thrown. 我已经使用Breakpoint运行应用程序来检查异常但是没有抛出异常。 Please help Web.config 请帮助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 Global.asax中

<%@ Application Language="C#" %>

void Application_Start(object sender, EventArgs e) 
{
    log4net.Config.XmlConfigurator.Configure();

}
//More empty methods below

Default.aspx 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(); }

}

Need to specify the proper appender reference. 需要指定正确的appender引用。 It is set to FileAppender. 它设置为FileAppender。 Should be RollingFileAppender. 应该是RollingFileAppender。

<root>
      <level value="INFO" />
      <appender-ref ref="RollingFileAppender" />
</root>

Also note, you can use .InfoFormat instead of separate string.Format() call. 另请注意,您可以使用.InfoFormat而不是单独的string.Format()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM