Log4Net教學
Step1:下載Log4Net參考套件
找到 Download 下載
Step2:建立log4net.config的xml檔
首先我們要先設定 log4net.config 檔,在專案中加入一個XML檔案並且將檔名修改成 log4net.config
log4net.config檔案內容
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="Console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <conversionPattern value="%date %-5level [%thread] (%file:%line) - %message%newline" /> </layout> </appender> <!-- 寫入文字檔設定開始 --> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="TLog\Log_Result.log" /> <appendToFile value="true" /> <maximumFileSize value="100KB" /> <maxSizeRollBackups value="2" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level %thread %logger - %message%newline" /> </layout> </appender> <!-- 寫入文字檔設定結束 --> <root> <level value="DEBUG" /> <appender-ref ref="Console" /> <appender-ref ref="RollingFile" /> </root> </log4net> </configuration>
Step3:C#檔內容
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using log4net; using log4net.Config; public partial class _Default : System.Web.UI.Page { private static readonly ILog TxtLog = LogManager.GetLogger(typeof(_Default)); protected void Page_Load(object sender, EventArgs e) { XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/log4net.config"))); TxtLog.Info("Page_Load_Start"); TxtLog.Debug("Page_Load_Debug"); TxtLog.Info("Page_Load_End"); } }
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁