现在的位置: 首页 > 综合 > 正文

读取log4net配置文件方法

2019年10月07日 ⁄ 综合 ⁄ 共 1199字 ⁄ 字号 评论关闭
//读取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);

【上篇】
【下篇】

抱歉!评论已关闭.