我试图找到比我聪明的人来验证我写的一些语法.我的想法是将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();
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我,"这很可怕",或者"这应该可以正常工作"?另外,如果我动态设置文件,我仍然可以期望log4net行为根据log4net.config文件设置旋转文件吗?
非常感激!