我正在尝试寻找比我更聪明的人来验证我写的一些语法。我的想法是将RollingFileAppender的文件名配置为程序集的名称,以使其更易于在我的项目中重复使用。
我看过这篇SO文章, 但它并不能完全回答我的问题...
我花费了很多时间来理解Log4net的内部组件,以下是我想到的(位于Global.asax文件的Application_Start方法中):
// Bind to the root hierarchy of log4net
log4net.Repository.Hierarchy.Hierarchy root =
log4net.LogManager.GetRepository()
as log4net.Repository.Hierarchy.Hierarchy;
if (root != null)
{
// Bind to the RollingFileAppender
log4net.Appender.RollingFileAppender rfa =
(log4net.Appender.RollingFileAppender)root.Root.GetAppender("RollingLogFileAppender");
if (rfa != null)
{
// Set the file name based on the assembly name
string filePath =
string.Format("~/App_Data/{0}.log", GetType().Assembly.GetName().Name);
// Assign the value to the appender
rfa.File = Server.MapPath(filePath);
// Apply changes to the appender
rfa.ActivateOptions();
}
}
有人能告诉我,“这很丑陋”,或者“这应该可以正常工作”吗?另外,如果我动态设置文件,我仍然可以期望log4net根据log4net.config文件设置轮换文件的行为吗?
非常感谢!
log4net.Config.XmlConfigurator.ConfigureAndWatch("log4net.config.xml")
重新加载配置,这才对我起作用。 - Tobias