我试图找到比我更聪明的人来验证我写的一些语法。这个想法是将我的 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 文件设置来轮换文件吗?
非常感激!