简体   繁体   中英

Getting Logger Name into Excel file with NLog

Thanks to Rolf's comment in this question: NLog in C# with severity AND categories

I am able to record to a text file the category of log message (such as "Thermal" or "Database" or "Mechanical". I'm doing this simply by passing a name to the "GetLogger" method.

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");
    }

The text file looks like this:

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

which is pretty good. I might ask in a seperate question how to reformat it.
Now I would like to get these messages into a CSV (Excel) file. I'm using:

<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>

but the output is only:

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,

This isn't surprising. I used "name" as a guess.
What is the field in GetLogger called?
More generally, how do I know all the options I can put in the CSV layout? Finally, is there a good tutorial on using NLog with CSV? I haven't found one.

Thanks,

Dave

What is the field in GetLogger called?

You're looking for ${logger} - see ${logger} docs

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

You could use all layout renderers, see the list with all layout renderers

For options for the CSV formating, see CsvLayout docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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