2016-01-13 93 views
0

使用以下配置即时通讯。这会为每毫秒创建一个日志文件。 我想每次执行只有一个日志文件,它应该是时间戳记我不想让nlog为每个毫秒创建日志文件

<?xml version="1.0" encoding="utf-8"?> 

<configuration> 
<configSections> 
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> 
</configSections> 


<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

<targets> 
     <target name="logfile" xsi:type="File" fileName="C:\log\log-  ${date:format=dd/MM/yyyy HH\:mm\:ss}.txt"></target> 
</targets> 

<rules> 
    <logger name="*" minlevel="Info" writeTo="logfile" /> 
</rules> 

+0

“每次执行一个日志文件”。这是一个可执行程序,您正在运行还是Web服务器?什么是执行的上下文。另外,您是否为每个类创建1个静态记录器对象,并且整个执行过程将从该对象的单个实例运行? –

回答

1

做,这是通过编程设置文件名的唯一方法。

E.g.

var logfileTarget = NLog.LogManager.Configuration.FindTargetByName<FileTarget>("logfile"); 
logfileTarget.FileName = "filename_with_date_and_ext"; //you can use layout renderers here. 

API docs

1

是否已经提交了processinfo布局,渲染器PR,因此它可以输出过程开始时间以期望的格式。但它只支持本地时间

fileName="C:\log\log-${processinfo:property=StartTime:format=yyyy-MM-dd_HHmmss}.log" 
+0

这包含在NLog 4.4中:) – Julian