1. 程式人生 > >讀取log4net配置檔案方法

讀取log4net配置檔案方法

//讀取log4net配置檔案,推薦方法二

/* ------------ 方法一(Start) ------------------------*/
//預設路徑為根目錄

//未指定配置檔案,預設監視Web.config
//[assembly: log4net.Config.XmlConfigurator(Watch = true)]

//and if you're using log4net.config file, use it like that instead:
//[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

//可指定路徑
//[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Practice/log4net.config", Watch = true)]
/* ------------ 方法一(End) ------------------------*/


/* ------------ 方法二(Start) ------------------------
 * Application_Start事件中讀取配置檔案

 * //注意:指明絕對路徑
 * //log4net.config為自定義的配置檔案
 * log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Practice/log4net.config")));
 * //OR
 * log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));
 * //OR
 * //當配置檔案有變化時,重新載入(推薦)
 * log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(Server.MapPath("~/Web.config")));
 ------------ 方法二(End) ------------------------*/

方法三:在例項化日誌物件時讀取日誌配置檔案(無順序限制,記錄日誌之前讀取日誌配置檔案即可)

log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(Server.MapPath("~/Web.config")));

log4net.ILog log =
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);