Beacher-

log4net 日志组件使用方法

在项目中需要一个强大的日志文件来自身监测程序的运行记录,运行性能等等信息,推荐LOG4NET.DLL这个.net平台的日志组件

第一步:下载并引用到项目中 http://download.csdn.net/detail/liujiang_2007/839743

第二步 在config文件中注册

 <!--log4net的配置-->

 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  
  </configSections>
 
  <!--log4net配置-->
 
  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date - %message%newline" />
      </layout>
    </appender>
    <appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="log\ErrorLog\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaxFileSize" value="1024000" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[%d] %m%n" />
      </layout>
    </appender>
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="log\InfoLog\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaxFileSize" value="1024000" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="[%d] %m%n" />
      </layout>
    </appender>
    <logger name="logerror">
      <level value="ERROR" />
      <appender-ref ref="ErrorAppender" />
      <appender-ref ref="ConsoleAppender" />
    </logger>
    <logger name="loginfo">
      <level value="INFO" />
      <appender-ref ref="InfoAppender" />
      <appender-ref ref="ConsoleAppender" />
    </logger>

  </log4net>

 

第三步 在项目中使用

创建一个log类

 private static readonly ILog loginfo = log4net.LogManager.GetLogger("loginfo");
    private static readonly ILog logerror = log4net.LogManager.GetLogger("logerror");
    public LoggerHelper()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static void StartLog()
    {
        //读取配置
        log4net.Config.DOMConfigurator.Configure();
    }

    #region 写信息日志
    /// <summary>
    /// 写信息日志
    /// </summary>
    /// <param name="info"></param>
    public static void WriteInfoLog(string info)
    {
        if (loginfo.IsInfoEnabled)
        {
            loginfo.Info(info);
        }
    }
    #endregion

    #region 写错误日志
    /// <summary>
    /// 写错误日志
    /// </summary>
    /// <param name="info"></param>
    public static void WriteErrorLog(string info)
    {
        if (logerror.IsErrorEnabled)
        {
            logerror.Error(info);
        }
    }
    #endregion

 

 

第四步 使用

 protected void Page_Load(object sender, EventArgs e)
    {
        LoggerHelper.StartLog();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string info = "记录运行状态";
        LoggerHelper.WriteInfoLog(info);
    }

posted on 2011-10-28 15:25  Beacher-  阅读(351)  评论(0编辑  收藏  举报

导航