오늘은 log4net을 사용하기 시작했으며 지금까지 정말 좋아합니다. 현재 로깅 기능을 유지하려면 응용 프로그램을 시작할 때마다 응용 프로그램에서 새 로그 파일을 만들어야합니다. 로그 파일 이름에는 날짜 및 시간 스탬프가 인코딩되어 있습니다. 현재 XmlConfigurator
을 통해 log4net을 구성했는데, 이는 RollingFileAppender
의 파일 이름이 구성 XML 파일에 하드 코드 된 것을 제외하고는 훌륭하게 작동합니다.XML 파일 * 및 * 코드를 통해 log4net appenders 구성
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