简体   繁体   English

如何使用NLog写入多个“数据”文件

[英]How to use NLog to write multiple “data” files

I would like my application to repeatedly write a large amount of data to separate "log" files during the application lifetime. 我希望我的应用程序在应用程序生命周期内重复写入大量数据以分隔“日志”文件。 (Call each instance of such a file a "data file".) The data is written to this file in a single write operation (as it's a string). (将此类文件的每个实例称为“数据文件”。)数据通过一次写入操作(因为它是字符串)被写入该文件。 In the main application log file, the application would write the path name of the data file that it creates, so that I can identify that data file that was created at that point in its execution. 在主应用程序日志文件中,应用程序将写入其创建的数据文件的路径名,以便我可以识别在执行该点时创建的数据文件。

I could create the data file using the .NET file API, but I thought it would be useful to use NLog, as I could enable the writing of the file via configuration. 我可以使用.NET文件API创建数据文件,但是我认为使用NLog会很有用,因为我可以通过配置启用文件写入。 However, I cannot see how to define a Target whose name is unique for each write operation. 但是,我看不到如何为每个写操作定义名称唯一的目标。 As an alternative, I could programmatically create a Target and add it to the LogManager's Configuration object each time I write a data file, and then delete the Target afterwards. 作为替代方案,我可以每次编写数据文件时以编程方式创建Target并将其添加到LogManager的Configuration对象中,然后再删除Target。

This seems plausible, but before implementing it I'd like to know if others had a similar requirement and how they implemented it. 这似乎是合理的,但是在实施之前,我想知道其他人是否有类似的要求以及他们是如何实施的。

You can configure NLog so that the name of the output file (in the case of using a file target) is "dynamic". 您可以配置NLog,以使输出文件的名称(在使用文件目标的情况下)为“动态”。 You can use LayoutRenderers in the file name configuration. 您可以在文件名配置中使用LayoutRenderers。 So, you could configure the target something like this: 因此,您可以配置目标,如下所示:

  <targets>
    <target name="file" xsi:type="File" layout="${verbose}" fileName="${basedir}/${gdc:DataFile}.log" />
  </targets>

In your code you would do something like this: 在您的代码中,您将执行以下操作:

NLog.GlobalDiagnosticsContext.Set("DataFile", "SomeName");

When NLog writes, it will resolve the file name. NLog写入时,它将解析文件名。 If the file exists, then the log will be written to that file. 如果该文件存在,则日志将被写入该文件。 If the file does not exist, then NLog will create a new file. 如果该文件不存在,则NLog将创建一个新文件。 So, in your case, if you set a new value into the GlobalDiagnosticsContext dictionary before each write, you will get a new file. 因此,在您的情况下,如果在每次写入之前在GlobalDiagnosticsContext字典中设置新值,则将获得一个新文件。 You can experiment with other LayoutRenders and see if you can get the behavior that you want for free. 您可以试验其他LayoutRender,看看是否可以免费获得想要的行为。 For example, if you used the CounterLayoutRenderer you could probably get a different file for every single write (I haven't tried it, so I can't say for sure). 例如,如果您使用CounterLayoutRenderer,则每次写入可能会获得一个不同的文件(我没有尝试过,所以不能肯定地说)。

You could configure it something like this: 您可以像这样配置它:

  <targets>
    <target name="file" xsi:type="File" layout="${verbose}"fileName="${basedir}/${counter}.log" />
  </targets>

UPDATE - You can also use the CounterLayoutRenderer as the basis for a filename, rather than accept it as the entire filename. 更新-您还可以将CounterLayoutRenderer用作文件名的基础,而不是将其作为整个文件名。

  <targets>
    <target name="file" xsi:type="File" layout="${verbose}"fileName="${basedir}/MyDataStuff_${counter}.log" />
  </targets>

This will yield filenames like: MyDataStuff_0.log MyDataStuff_1.log MyDataStuff_2.log etc... 这将产生如下文件名:MyDataStuff_0.log MyDataStuff_1.log MyDataStuff_2.log等...

Using LayoutRenderers to build the output filename is pretty flexible, so you should be able to find a solution that meets your needs. 使用LayoutRenderers生成输出文件名非常灵活,因此您应该能够找到满足您需求的解决方案。

Maybe this will give you some ideas 也许这会给你一些想法

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

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