Configuring log4net appenders via XML file *and* code

牧云@^-^@ 提交于 2019-12-02 03:35:21

Do you in this case need the rolling file appender? If not I would expect that your code would create the desired result if you used the normal file appender.

Edit: Maybe it works with the RollingFile Appender if you call ActivateOptions() on the appender.

Julius A

Try this snippet:

XmlConfigurator.Configure();

log4net.Repository.ILoggerRepository repo = LogManager.GetRepository();
foreach (log4net.Appender.IAppender appender in repo.GetAppenders())
{
if (appender.Name.CompareTo("RollingFileAppender") == 0 && appender is log4net.Appender.RollingFileAppender)
{
   var appndr = appender as log4net.Appender.RollingFileAppender;
   string logPath = "MyApplication.log";
   appndr.File = logPath;
   appndr.ActivateOptions();
}

I had posted similar article here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!