2016/09/05

Log4Net output multiple logs

關鍵在於log4net.Filter.LevelRangeFilter,由Filter攔截level並指派給appender並輸出

程式碼:
using log4net;
using System;
using System.Reflection;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Logger
        /// </summary>
        private ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);

        /// <summary>
        /// Logger Error
        /// </summary>
        private ILog LogError = LogManager.GetLogger("ErrorsAppender");

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();

            try
            {
                var str = "Hi";
                Log.Info(str);
                Convert.ToDouble(str);
            }
            catch (FormatException ex)
            {
                LogError.Error(ex.StackTrace);
            }
        }

    }
}




App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <!--引入Log4Net-->
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <log4net>
    <!--Output File-->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--檔案名稱-->
      <file value="Log/Log.txt"/>
      <!--Unicode-->
      <encoding value="utf-8" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="1"/>
      <!--檔案最大容量-->
      <maximumFileSize value="100MB"/>
      <!--非固定的檔案名稱-->
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--日期 Thread 層級 Logger名稱 訊息 換行-->
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
      <!--Filter info-->
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>

    <!--錯誤訊息-->
    <appender name="ErrorsAppender" type="log4net.Appender.RollingFileAppender">
      <!--檔案名稱-->
      <file value="Log/Error.txt"/>
      <encoding value="utf-8" />
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="1"/>
      <maximumFileSize value="100MB"/>
      <staticLogFileName value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--日期 Thread 層級 Logger名稱 訊息 換行-->
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
      <!--Filter level-->
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
    </appender>

    <!--除錯訊息-->
    <appender name="DebugAppender" type="log4net.Appender.DebugAppender">
      <immediateFlush value="true"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--日期 Thread 層級 Logger名稱 訊息 換行-->
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
      </layout>
    </appender>
    
    <!--載入設定-->
    <root>
      <level value="ALL"/>
      <appender-ref ref="DebugAppender"/>
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="ErrorsAppender"/>
    </root>
  </log4net>
</configuration>

執行結果:



沒有留言:

張貼留言