我有一个接受 Web 请求以触发长时间运行的异步任务的 Windows 服务。我希望每个任务都将日志输出到不同的目录(其名称由请求 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;
}
有没有办法只为特定的记录器设置日志?或者我可以以某种方式避免 Reset() 吗?此外,这段特定的代码是我唯一提到 log4net 的地方。因此,如果我可以为每个日志集创建多个文件夹,我可以迁移到 NLog。
编辑:我在 NLog 中找到了这个功能 - https://github.com/NLog/NLog/wiki/Configure-component-logging
但它看起来不像 Common.Logging 支持它。