【发布时间】:2014-12-29 06:45:33
【问题描述】:
我正在使用 NLog 创建一个日志库,并且需要根据不同的终端设置记录器。我所做的是创建一个 ILoggerFacade 实现,并将文件名作为构造函数传递,并注入用于创建日志文件的文件名。
这是我的代码:
internal static class FileLogFactory
{
#region "Private Static Members"
private static ILog _applicationLog;
private static Dictionary<string, ILog> _terminalLogs;
private static object _syncLock = new object();
#endregion
#region "Constant Fields"
private const string _APPLICATION_LOG = "applicationLog";
private const string _TERMINAL_LOG = "terminalHandlerLog";
private const string _TERMINAL_PREFIX = "filenameprefix";
public const string APPLICATION = "Application";
#endregion
#region "Static Cosntructors"
static FileLogFactory()
{
_terminalLogs = new Dictionary<string, ILog>();
}
#endregion
#region "Static Methods"
internal static ILog CreateLoggerInstance(string filename)
{
lock (_syncLock)
{
if (string.IsNullOrEmpty(filename) || filename == APPLICATION)
{
// Application Log requested.
if (_applicationLog == null)
{
_applicationLog = LogManager.GetLogger(_APPLICATION_LOG);
}
return _applicationLog;
}
// terminal log requested, check if already created else create new.
if (!_terminalLogs.ContainsKey(filename))
{
NLog.GlobalDiagnosticsContext.Set(_TERMINAL_PREFIX, filename);
_terminalLogs.Add(filename, LogManager.GetLogger(_TERMINAL_LOG));
}
return _terminalLogs[filename];
}
}
#endregion
}
以下是我的 NLog 配置
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<variable name="logpath" value="c:/Log"/>
<targets>
<!-- add your targets here -->
<target xsi:type="File"
name="applicationTarget"
layout="${longdate}|${uppercase:${level}}|${threadid}| ${message}"
archiveAboveSize="1024000"
fileName="${logpath}/Application.${shortdate}.log"
/>
<target xsi:type="File"
name="terminalHandlerTarget"
layout="${longdate}|${uppercase:${level}}|${threadid}| ${message}"
archiveAboveSize="1024000"
fileName="${logpath}/${gdc:item=filenameprefix}.${shortdate}.log"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="applicationLog" minlevel="Trace" writeTo="applicationTarget" />
<logger name="terminalHandlerLog" minlevel="Trace" writeTo="terminalHandlerTarget" />
</rules>
</nlog>
这个想法是从代码中传递 gdc:item=filenameprefix 并为每个终端创建日志文件。现在发生的事情是 GetLogger 返回相同的对象 terminalHandlerLog ,因此给定的最后一个文件名采用首选项,然后始终写入该文件。
我相信这是因为 GetLogger 遵循单例模式并为每个记录器创建单个对象。
如何覆盖,是否有任何设置可以用于此?
【问题讨论】: