我今天開始玩log4net,到目前爲止,我真的很喜歡它。爲了保留我們當前的日誌記錄功能,應用程序需要在應用程序啓動時創建一個新的日誌文件。日誌文件名稱中包含日期和時間戳記。目前,我已經通過XmlConfigurator
配置了log4net,這很好,除了我的RollingFileAppender
的文件名在配置XML文件中被硬編碼之外。配置log4net appenders通過XML文件*和*代碼
我想繼續使用XmlConfigurator
,但在致電Configure()
後,我想要獲取RollingFileAppender
,並在代碼中將其文件值更改爲動態生成的字符串。示例documentation online似乎現在正在進行中,但我已經透過SDK參考,看起來我可以使用Heirarchy
和GetAppenders()
來做我需要做的事情。我在正確的軌道上嗎?
好吧,我參加了一個刺在此,想下面的代碼,它沒有工作:
private static readonly ILog _log = LogManager.GetLogger(typeof(GUI));
// in the config file, I've set the filename to example.log, and it works
XmlConfigurator.Configure(new FileInfo("log_config.xml"));
Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
if(hierarchy != null) {
// get the appenders
IAppender[] appenders = hierarchy.GetAppenders();
// change the filename for the RollingFileAppender
foreach(IAppender a in appenders) {
RollingFileAppender rfa = a as RollingFileAppender;
if(rfa == null)
continue;
rfa.File = "newfile.log"; // no runtime error, but doesn't work.
}
}
_log.Info("Application started");
絕對正確的!謝謝! – Dave 2010-05-11 14:50:20