mef*_*rdm 10 c# logging filenames nlog
我正在使用Nlog从我的c#app登录.以下是<targets>我的Nlog.config中的部分:
<targets>
<target name="logfile" xsi:type="File" fileName="..\logs\${date:format=yyyyMMdd_HHmmss}_trg.log"
layout="${counter} | ${date:format=yyyy-MM-dd HH\:mm\:ss.ffff} | ${machinename} | ${level:uppercase=true} | ${logger:shortName=true} | ${stacktrace} | ${message:exceptionSeparator=EXCEPTION:withException=true}"
keepFileOpen="true"/>
</targets>
Run Code Online (Sandbox Code Playgroud)
对于filename我使用${date:format=yyyyMMdd_HHmmss}_trg.log基于创建时就来命名日志.但是,当我的应用程序运行时,记录器每秒都会创建一个新的日志文件.如何强制Nlog修复文件名,每个会话只创建一个日志?
wag*_*ghe 10
我不确定,但我的猜测是NLog根据filename属性检查是否存在日志文件(由于您使用的是日期布局渲染器,因此该属性是动态的).因此,由于文件名正在改变(即每次检索文件名值时它都不同(或可能不同)),NLog会创建一个新文件.
尝试使用这样的shortdate布局渲染器:
<targets>
<target name="logfile" xsi:type="File"
fileName="..\logs\${shortdate}_trg.log"
layout="${counter} | ${date:format=yyyy-MM-dd HH\:mm\:ss.ffff} | ${machinename} | ${level:uppercase=true} | ${logger:shortName=true} | ${stacktrace} | ${message:exceptionSeparator=EXCEPTION:withException=true}"
keepFileOpen="true"/>
</targets>
Run Code Online (Sandbox Code Playgroud)
我想你会看到你的文件名不会改变(直到午夜).
关键是NLog将始终检查文件是否存在(根据写入日志消息时的文件名值),如果文件尚不存在,则会创建该文件.
或者,如果要使用更精确的文件名命名日志文件(即它在某个时间某个时间创建),则可以将该时间存储在GlobalDiagnosticContext中,并使用gdc布局渲染器来帮助命名文件.像这样的东西:
//Early in your program do something like this:
NLog.GlobalDiagnosticContext["StartTime"] = DateTime.Now.ToString("yyyyMMdd_HHmmss");
Run Code Online (Sandbox Code Playgroud)
在NLog.config文件中,执行以下操作:
<targets>
<target name="logfile" xsi:type="File"
fileName="..\logs\${gdc:item=StartTime}_trg.log"
layout="${counter} | ${date:format=yyyy-MM-dd HH\:mm\:ss.ffff} | ${machinename} | ${level:uppercase=true} | ${logger:shortName=true} | ${stacktrace} | ${message:exceptionSeparator=EXCEPTION:withException=true}"
keepFileOpen="true"/>
</targets>
Run Code Online (Sandbox Code Playgroud)
最后,您可以编写自定义LayoutRenderer来填充日期/时间.它会得到一次的时间,然后每次都返回相同的值.
它看起来像这样:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Globalization;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
namespace MyNLogExtensions
{
[LayoutRenderer("StartTime")]
class StartTimeLayoutRenderer : LayoutRenderer
{
private DateTime start = DateTime.Now;
public StartTimeLayoutRenderer()
{
this.Format = "G";
this.Culture = CultureInfo.InvariantCulture;
}
//
// In NLog 1.x, LayoutRenderer defines a Culture property.
// In NLog 2.0, LayoutRenderer does not define a Culture property.
//
public CultureInfo Culture { get; set; }
[DefaultParameter]
public string Format { get; set; }
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(start.ToString(this.Format, this.Culture));
}
protected override int GetEstimatedBufferSize(LogEventInfo logEvent)
{
return 10;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的NLog.config文件中,你需要这样的东西来告诉你扩展的位置:
<extensions>
<add assembly="MyAssembly.dll"
</extensions>
Run Code Online (Sandbox Code Playgroud)
然后你的目标配置看起来像这样:
<targets>
<target name="logfile" xsi:type="File"
fileName="..\logs\${StartTime:format=yyyyMMdd_HHmmss}_trg.log"
layout="${counter} | ${date:format=yyyy-MM-dd HH\:mm\:ss.ffff} | ${machinename} | ${level:uppercase=true} | ${logger:shortName=true} | ${stacktrace} | ${message:exceptionSeparator=EXCEPTION:withException=true}"
keepFileOpen="true"/>
</targets>
Run Code Online (Sandbox Code Playgroud)
显然,有一个${cached}布局渲染器将渲染一次布局并重用它. https://github.com/nlog/nlog/wiki/Cached-Layout-Renderer
但是,感谢@wageoghe的输入.使用的解决方案GlobalDiagnosticContext让我考虑将其他值传递给NLog.config.