1. 程式人生 > >Log4Net配置使用簡記

Log4Net配置使用簡記

使用 日期 help log文件 onf 版本 lena info star

1,引用Log4Net.dll 。當前為2.0.8.0版,可添加Nuget包。我的辦法是從下載的包中直接引用相應.net版本的dll以減小項目體積

2,在App.config中增加<log4net>節,內容為:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <
log4net> <logger name="logerror"> <level value="ERROR" /> <appender-ref ref="ErrorAppender" /> </logger> <logger name="loginfo"> <level value="INFO" /> <appender-ref ref="InfoAppender" /> </logger> <appender
name="ErrorAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogError\\" /> <param name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups" value="100" /> <param name="MaxFileSize" value="10240" /> <param
name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%n異常時間:%d %n異常級別:%-5p%n異常內容:%m%n" /> </layout> <!--&lt; &gt; = <> %n = 回車--> </appender> <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="Log\\LogInfo\\" /> <param name="AppendToFile" value="true" /> <param name="MaxFileSize" value="10240" /> <param name="MaxSizeRollBackups" value="100" /> <param name="StaticLogFileName" value="false" /> <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" /> <param name="RollingStyle" value="Date" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="日誌時間:%d %n日誌級別:%-5p %n日誌內容:%m%n%n" /> </layout> </appender> </log4net> </configuration>

3,項目AssemblyInfo.cs文件中增加一行代碼,用以註冊log4net.config文件

[assembly: log4net.Config.XmlConfigurator(ConfigFileExtension = "config", Watch = true)]

4,項目中添加日誌類

 public class LogHelper
    {
        private LogHelper()
        {
        }

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

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

        public static void SetConfig()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        public static void SetConfig(FileInfo configFile)
        {
            log4net.Config.XmlConfigurator.Configure(configFile);
        }

        public static void WriteLog(string info)
        {
            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(info);
            }
        }

        public static void WriteLog(string info, Exception se)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(info, se);
            }
        }
    }

5,測試

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestLog4Net
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                LogHelper.WriteLog("程序啟動,正常記錄!");
                int a = Convert.ToInt32("zzz");
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("錯誤", ex);
            }
        }
    }
}

6,測試結果:運行目錄下生成log文件夾,其下兩個文件夾:LogError,LogInfo,其內是以日期為文件名的Txt日誌

Log4Net配置使用簡記