使用 NLog 将记录器名称放入 Excel 文件

Getting Logger Name into Excel file with NLog

感谢 Rolf 在这个问题中的评论: NLog in C# with severity AND categories

我能够将日志消息的类别(例如 "Thermal" 或 "Database" 或 "Mechanical" 记录到文本文件中。我只是通过传递名称来完成此操作"GetLogger" 方法。

public MainWindow()
    {
        InitializeComponent();
        var logger = NLog.LogManager.GetCurrentClassLogger();
        logger.Info("Hello World");

        (NLog.LogManager.GetLogger("Database")).Info("Here is a DB message");
        (NLog.LogManager.GetLogger("Thermal")).Info("Here is a Thermal message");
    }

文本文件如下所示:

2018-05-13 17:40:47.7442|INFO|NLogExperiments.MainWindow|Hello World
2018-05-13 17:40:50.3404|INFO|Database|Here is a DB message
2018-05-13 17:40:50.3404|INFO|Thermal|Here is a Thermal message

这还不错。我可能会在一个单独的问题中问如何重新格式化它。
现在我想将这些消息放入 CSV (Excel) 文件中。我正在使用:

<target name="excelfile" xsi:type="File" fileName="nLog.csv" archiveAboveSize="50000000" archiveNumbering="Sequence" maxArchiveFiles="3">
  <layout xsi:type="CsvLayout">
    <!-- Layout Options -->

    <column name="time" layout="${longdate}" />
    <column name="level" layout="${level}"/>
    <column name="name" layout="${name}"/>
    <column name="message" layout="${message}" />
    <column name="codeLine" layout="${event-context:item=codeLine}" />

  </layout>
</target>

但输出只有:

time,level,name,message,codeLine
2018-05-13 17:40:47.7442,Info,,Hello World,
2018-05-13 17:40:50.3404,Info,,Here is a DB message,
2018-05-13 17:40:50.3404,Info,,Here is a Thermal message,

这并不奇怪。我用 "name" 作为猜测。
GetLogger 中的字段叫什么?
更一般地说,我如何知道可以放入 CSV 布局中的所有选项? 最后,是否有关于将 NLog 与 CSV 一起使用的好教程?我还没找到。

谢谢,

戴夫

What is the field in GetLogger called?

您正在寻找 ${logger} - 请参阅 ${logger} docs

More generally, how do I know all the options I can put in the CSV layout?

您可以使用所有布局渲染器,请参阅 list with all layout renderers

有关 CSV 格式的选项,请参阅CsvLayout docs