您的足迹:首页 > 未分类 >如何配置log4net,让日志分类记录到不同的日志文件中

如何配置log4net,让日志分类记录到不同的日志文件中

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
  <log4net>
    <root>
      <priority value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="RollingLogWarnFileAppender"/>
      <appender-ref ref="RollingLogFatalFileAppender"/>
      <appender-ref ref="RollingLogInfoFileAppender"/>
      <!--<appender-ref ref="ColoredConsoleAppender" />-->
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Error\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%r %date [%thread] %-5level- %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="ERROR" />
      </filter>
    </appender>
    <appender name="RollingLogWarnFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Warn\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%r %date [%thread] %-5level- %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN" />
        <levelMax value="WARN" />
      </filter>      
    </appender>
    <appender name="RollingLogFatalFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Fatal\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%r %date [%thread] %-5level- %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="FATAL" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <appender name="RollingLogInfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Info\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%r %date [%thread] %-5level- %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>    
    <!--<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
      <mapping>
        <level value="FATAL" />
        <foreColor value="Red, HighIntensity" />
      </mapping>
      <mapping>
        <level value="ERROR" />
        <foreColor value="Red" />
      </mapping>
      <mapping>
        <level value="WARN" />
        <foreColor value="Yellow" />
      </mapping>
      <mapping>
        <level value="INFO" />
        <foreColor value="White" />
      </mapping>
      <mapping>
        <level value="DEBUG" />
        <foreColor value="Green" />
      </mapping>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%r %date [%thread] %-5level- %message%newline" />
      </layout>
    </appender>-->
  </log4net>

</configuration>


使用

   private static readonly ILog logInfo = LogManager.GetLogger("loginfo");
        static LoggingHelper()
        {
            IntiLocalLog();
        }

        /// <summary>
        /// 记录严重错误日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Fatal(string message, Exception ex = null, bool isWriteDebug = true)
        {
            Task.Factory.StartNew(() =>
            {
                logInfo.Fatal(message, ex);
            }).ContinueWith((t) =>
            {
                LoggingHelper.Error("日志严重错误信息记录错误" + t.Exception.InnerException.GetType().Name);
            },
                    TaskContinuationOptions.OnlyOnFaulted);
        }


        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Error(string message, Exception ex = null, bool isWriteDebug = true)
        {
            Task.Factory.StartNew(() =>
            {
                logInfo.Error(message, ex);
            }).ContinueWith((t) =>
            {
                LoggingHelper.Error("日志异常信息记录错误" + t.Exception.InnerException.GetType().Name);
            },
                    TaskContinuationOptions.OnlyOnFaulted);
        }


        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Warn(string message, Exception ex = null, bool isWriteDebug = true)
        {
            Task.Factory.StartNew(() =>
            {
                 logInfo.Warn(message, ex);
            }).ContinueWith((t) =>
            {
                LoggingHelper.Error("日志警告信息记录错误" + t.Exception.InnerException.GetType().Name);
            },
                    TaskContinuationOptions.OnlyOnFaulted);
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Info(string message, Exception ex = null, bool isWriteDebug = true)
        {
            Task.Factory.StartNew(() =>
            {
                logInfo.Info(message, ex);
            }).ContinueWith((t) =>
            {
                LoggingHelper.Error("日志信息记录错误" + t.Exception.InnerException.GetType().Name);
            },
                    TaskContinuationOptions.OnlyOnFaulted);
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">消息内容</param>
        public static void Debug(string message)
        {
            Task.Factory.StartNew(() =>
            {
                logInfo.Debug(message);
            }).ContinueWith((t) =>
            {
                LoggingHelper.Error("日志调试信息记录错误" + t.Exception.InnerException.GetType().Name);
            },
                    TaskContinuationOptions.OnlyOnFaulted);
        }

        #region 私有方法

        private static void IntiLocalLog()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        #endregion

本博客所有文章如无特别注明均为原创。作者:cc复制或转载请以超链接形式注明转自 我的博客
原文地址《如何配置log4net,让日志分类记录到不同的日志文件中

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)