William-Ren

导航

log4net简易配置

WEB.Config
在<configuration>加入代码:

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

     <log4net debug="true">
       <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
         <param name="File" value="log.txt"/>
         <layout type="log4net.Layout.PatternLayout">
           <param name="ConversionPattern" value="[%d] %-5p %c %m%n"/>
         </layout>
       </appender>

       <logger name="File">
         <level value="All" />
         <appender-ref ref="LogFileAppender" />
       </logger>
     </log4net>

2.添加log4net.dll的引用

3.写一个调用类:

/// <summary>
/// MyEventlog 的摘要说明
/// </summary>
public class MyEventlog
{
public MyEventlog()
{
     //
     // TODO: 在此处添加构造函数逻辑
     //
}
       private static log4net.ILog log = null;
    
       private static object lockHelper = new object();

       public static log4net.ILog Log
       {
           get
           {
               if (log == null)
                   lock (lockHelper)
                       if (log == null)
                           log = log4net.LogManager.GetLogger("File");
               return log;
           }
       }

}
最后记得在GLOBE.ASA文件里面添加LOG4NET
      void Application_Start(object sender, EventArgs e)
      {
          // 在应用程序启动时运行的代码
          //启动日志log4net
          log4net.Config.XmlConfigurator.Configure();

      }

现在就可以在程序里面调用了:

例:           MyEventlog.Log.Error("asdfsadfbsadf");
       这样在你的程序的根目录里面的log.txt就有:
      [2007-08-10 10:26:14,656] ERROR File asdfsadfbsadf

posted on 2007-12-25 13:49  William-ren  阅读(184)  评论(0编辑  收藏  举报