安裝則可以透過Nuget輸入
Install-Package log4net
該範例則提供固定的log檔以及依照日期下去產生的log檔,使用前需要至App.config設定並在程式內調用Log4Net的方法
using log4net; using System; using System.Reflection; using System.Windows.Forms; namespace Test { public partial class Form1 : Form { /// <summary> /// Logger /// </summary> private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name); public Form1() { InitializeComponent(); //初始化Log4Net log4net.Config.XmlConfigurator.Configure(); } private void button1_Click(object sender, EventArgs e) { //輸出至log檔 log.Info("button1Click"); try { Convert.ToDouble("ss"); } catch (FormatException ex) { //將錯誤訊息輸出至log檔 log.Error(ex.StackTrace); } } } }
固定Log設定:
<?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/Test.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> </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"/> </root> </log4net> </configuration>
日期設定:
<?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.6.1" /> </startup> <log4net> <!--Output File--> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <!--路徑--> <file value="Log/"/> <!--檔案名稱--> <datePattern name="DatePattern" value="yyyy_MM_dd'.log'" ></datePattern> <!--Unicode--> <encoding value="utf-8" /> <appendToFile value="true"/> <rollingStyle value="Composite"/> <maxSizeRollBackups value="1"/> <!--檔案最大容量--> <maximumFileSize value="100MB"/> <!--非固定的檔案名稱--> <staticLogFileName value="false"/> <layout type="log4net.Layout.PatternLayout"> <!--日期 Thread 層級 Logger名稱 訊息 換行--> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </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"/> </root> </log4net> </configuration>
執行結果:
日期輸出:
固定Log輸出: