我正在使用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>
&#13;
想法是从代码传递gdc:item = filenameprefix并为每个终端创建日志文件。现在发生的事情是GetLogger返回相同的对象terminalHandlerLog,因此给出的最后一个文件名采用首选项,并从那里始终写入该文件。
我相信这是因为GetLogger遵循单例模式并为每个记录器创建单个对象。
如何覆盖,是否有可用于此的设置?
答案 0 :(得分:1)
在NLog上使用GetLogger方法时,您将获得在配置中配置的实例。如果您需要多个配置,例如写入运行时已知的不同文件,则需要在代码中创建日志实例:
if (!_terminalLogs.ContainsKey(filename))
{
var config = LogManager.Configuration;
var fileTarget = new FileTarget();
fileTarget.FileName = filename;
fileTarget.Layout = "${longdate}|${uppercase:${level}}|${threadid}| ${message}";
fileTarget.ArchiveAboveSize = 1024000;
然后将记录器添加到您的收藏夹中:
_terminalLogs.Add(filename, _fileTarget);
config.AddTarget(filename, fileTarget);
}