Log4Net

App.config 配制

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
  <log4net>
    <logger name="UseAnalysisLog">
      <level value="ALL"/>
      <appender-ref ref="RollingFile"/></logger>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="Logs/UseAnalysisLog.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/><!--可以认为是按照日期或文件大小来生成新的log文件,比较常用的如Size、Date-->
      <MaxSizeRollBackups value="30"/>
      <MaximumFileSize value="500KB"/><!--rollingStyle必须设置为Size才有效(日志文件最大的Size)-->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d %-4r [%t] %-5p %c %x - %m%n"/>
      </layout>
    </appender>
  </log4net>

 

LogUtil类

public class LogUtil
    {
        private static ILog log;

        static LogUtil()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        /// <summary>
        /// 根据主程序的assembly name 初始化日志
        /// </summary>
        /// <param name="assemblyType"></param>
        public static void Init(Type assemblyType)
        {
            log = LogManager.GetLogger(assemblyType.Assembly.GetName().Name);
            WriteHead();
        }

        /// <summary>
        /// 根据日志名称初始化日志
        /// </summary>
        /// <param name="logName">日志名称</param>
        public static void Init(string logName)
        {
            log = LogManager.GetLogger(logName);
            WriteHead();
        }

        /// <summary>
        /// 写文件头部
        /// </summary>
        private static void WriteHead()
        {
            StringBuilder sb = new StringBuilder();

            Process curProcess = Process.GetCurrentProcess();
            sb.Append(Environment.NewLine);
            sb.AppendLine("============================================================");
            sb.AppendLine("Subject:   Log File");
            sb.AppendLine("MainApp:   " + curProcess.MainModule.FileName);
            sb.AppendLine("Version:   " + curProcess.MainModule.FileVersionInfo.FileVersion);
            sb.AppendLine("=============================================================");
            Log.Info(sb.ToString());
        }

        public static ILog Log
        {
            get
            {
                if (log != null)
                    return log;

                //如果没有调Init方法,为不使程序崩溃.配置一个日志
                log = LogManager.GetLogger("default");
                return log;
            }
        }
    }

 

日志初始化

注意:在程序启动时初始化一下log4net,name必须和配制文件中的longger.name一致

<log4net>
    <logger name="UseAnalysisLog">//初始化需要用到这个name做为参数
      <level value="ALL"/>
      <appender-ref ref="RollingFile"/>
    </logger>

 

LogUtil.Init("UseAnalysisLog");

 

使用演示

LogUtil.Log.Info("--------------------Log4Net Info--------------");
LogUtil.Log.Error("--------------------Log4Net Error--------------",exception);
LogUtil.Log.Debug("--------------------Log4Net Error--------------");

 

posted @ 2012-12-06 10:33  古月天  阅读(279)  评论(0编辑  收藏  举报