记录到多个文件夹

时间:2016-08-12 03:38:01

标签: log4net nlog common.logging

我有一个Windows服务,它接受Web请求以触发长时间运行的异步任务。我希望每个任务都将日志输出到不同的目录(其名称由请求ID和触发作业的用户确定)。在每个目录中,我有多个日志文件。

我正在使用Common.Logging和log4net。但是,我不得不重置log4net配置以更改目录(如下所示),如果在另一个仍在运行时触发任务,这将无法正常工作。这两个任务的日志都将更改为最新创建的目录。

 protected ILog CreateLoggerInstance(string loggerName, string logFolder)
    {
        logFolder += "/";
        // This is a hack to avoid creation of a folder called (null), because of limitation in log4net. 
        // More details: https://github.com/net-commons/common-logging/issues/81
        log4net.GlobalContext.Properties["LogsDirectory"] = logFolder;

        this.LogInstance = LogManager.GetLogger(loggerName);
        this.LogInstance.GlobalVariablesContext.Set("LogsDirectory", logFolder);
        LogManager.Reset();
        this.LogInstance = LogManager.GetLogger(loggerName);

        this.LogFolder = logFolder;
        return this.LogInstance;
    }

有没有办法只为特定记录器设置日志?或者我可以以某种方式避免重置()?此外,这段特殊的代码是我所指的log4net的唯一地方。如果我可以为每个日志集创建多个文件夹,那么我可以移动到NLog。

编辑:我在NLog中找到了这项功能 - https://github.com/NLog/NLog/wiki/Configure-component-logging

但它看起来不像Common.Logging支持它。

0 个答案:

没有答案