log4net的简单使用

最近记录log时用了一下log4net,下面简单介绍下log4net的用法。

1、下载引用log4net组件

2、配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>  
  <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> 
 </configSections>     
 <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>
</configuration>

3、工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;

namespace Wind.Cloud.Lua
{
    public class LogHelper
    {
        private LogHelper()
        {
        }

        private static readonly ILog loginfo = log4net.LogManager.GetLogger("loginfo");

        private static readonly ILog logerror = log4net.LogManager.GetLogger("logerror");

        public static void StartLog()
        {
            //读取配置
            log4net.Config.XmlConfigurator.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
    }
}

4、使用

LogHelper.StartLog();

string info = "test";

LogHelper.WriteInfoLog(info);


 

posted @ 2011-10-23 19:36  wander.chu  阅读(521)  评论(0编辑  收藏  举报