简体   繁体   English

c#NLog如何只获取一个以longdate为文件名的日志文件

[英]c# NLog how to get only one logfile with the longdate as filename

Hello I am using for my console application NLog for logging.您好,我正在使用我的控制台应用程序 NLog 进行日志记录。 With that being said I would like to log everything that happens in the console using NLog to set that up I am using the NLOG.config:话虽如此,我想使用 NLog 记录控制台中发生的所有事情,以进行设置,我正在使用 NLOG.config:

  <target name="FullCSVFile" xsi:type="File"  fileName="logs\internal_${date:format=yyyyMMdd_HHmmss}.log" keepFileOpen="true">
  <layout xsi:type="CsvLayout">
    <column name="Console" layout="${message}" />
    <column name="Error" layout="${exception:format=ToString}" />
  </layout>
</target>

<logger minlevel="Debug" name="*" writeTo="FullCSVFile" />

But this way the tool makes a new file every second passed in the console, but I want to make one file when the tool starts with that timestamp and write everything in this one.但是通过这种方式,该工具每秒都会在控制台中创建一个新文件,但是我想在该工具以该时间戳启动时创建一个文件并将所有内容写入该文件。 How would I do that?我该怎么做?

The trick is to use the ambient property cached=true like this:诀窍是像这样使用环境属性cached=true

<target name="FullCSVFile" xsi:type="File"  fileName="logs\internal_${date:format=yyyyMMdd_HHmmss:cached=true}.log" keepFileOpen="true">
  <layout xsi:type="CsvLayout">
    <column name="Console" layout="${message}" />
    <column name="Error" layout="${exception:format=ToString}" />
  </layout>
</target>

It will make layout render once, and then just return the same cached value.它将使布局渲染一次,然后只返回相同的缓存值。

See also: https://github.com/nlog/NLog/wiki/Cached-Layout-Renderer另见: https : //github.com/nlog/NLog/wiki/Cached-Layout-Renderer

Something like this could work, but I have not tested it:像这样的东西可以工作,但我还没有测试过:

  <variable name="started" value="${date:format=yyyyMMdd_HHmmss}"/>
  <target name="FullCSVFile" xsi:type="File" fileName="logs\internal_${started}.log" keepFileOpen="true">
    <layout xsi:type="CsvLayout">
      <column name="Console" layout="${message}" />
      <column name="Error" layout="${exception:format=ToString}" />
    </layout>
  </target>

The idea is similar to the example from the documentation: https://github.com/nlog/nlog/wiki/Configuration-file#variables这个想法类似于文档中的示例: https : //github.com/nlog/nlog/wiki/Configuration-file#variables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM