Change name of logfile a few times during runtime

大憨熊 提交于 2019-12-01 04:57:27
Koen

The answer of Peter brought me in the right direction, but I ended up doing it in code instead of editing and saving the config-file.

public void WriteStepInfo(string device, int step)
{
    var h = (log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository();
    foreach (IAppender a in h.Root.Appenders)
    {
        if (a.Name == "StepsLogAppender")
        {
            FileAppender fa = (FileAppender)a;
            var logFileLocation = string.Format(".\\Logs\\Device_{0}.log", device);

            fa.File = logFileLocation;
            fa.ActivateOptions();
            break;
        }
    }

    Log.Info(string.Format("Device {0} - step {1}. Different file for each device", device, step));
}

You can open the log4net.config file from your application and change the name. Then save the file (log4net.config) and the logging file will be changed.

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