简体   繁体   中英

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.

Here is my webconfig file

   <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. Or make it an attribute.
  2. your TestLog.txt is going into the current directory of running application. 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. Or to see where it is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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