简体   繁体   English

log4net不写日志

[英]log4net not writing in log

I am programming a webform email sending application using C# .Net and i want to record in a log everytime a mail is sent. 我正在使用C#.Net编写Webform电子邮件发送应用程序,我希望每次发送邮件时都记录在日志中。

Here is my webconfig file 这是我的webconfig文件

   <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>


  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="TestLog.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="10MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c:%L] - %m%n"/>
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>

 protected void Page_Load(object sender, EventArgs e)
    {
      log4net.Config.XmlConfigurator.Configure();
    }

I have This attribute under my class definition 我的班级定义下有这个属性

public partial class enviarMails : System.Web.UI.Page
{
    private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

And i have this method for writing the log 而且我有这种写日志的方法

public void escribirLog(String msj) 
        {

            logger.Error(msj);

        }

There are at least 2 things you can change in your code which can make it better/or get it start logging: 您可以在代码中至少更改两件事,以使其变得更好/或使其开始记录:

  1. move the configure call to the startup of your project. 将configure调用移至项目的启动。 Or make it an attribute. 或使其成为属性。
  2. your TestLog.txt is going into the current directory of running application. 您的TestLog.txt将进入正在运行的应用程序的当前目录。 You probably do not have any rights in that directory. 您可能在该目录中没有任何权限。 So change it to a full path of which you know you have rights 因此,将其更改为您拥有权利的完整路径
  3. enable log4net internal debugging to see the internal error for not creating the file. 启用log4net内部调试以查看未创建文件的内部错误。 Or to see where it is created. 或查看它的创建位置。

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

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