How do I extract the parameters individually?

56 Views Asked by At

How do I set up NLog to extract the parameters individually?
Expected parameters in the logging log:

  • Date Time;
  • Project name;
  • Class name;
  • Name of the method;
  • Line number where the calling method of the logger is located;
  • Lger's message.

My ultimate goal: I want to write the logger data to a csv file so that each parameter is recorded in a separate field.

NLog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <!-- Log to a file -->
        <target name="console" xsi:type="Console"
                layout="${longdate}|${callsite}|${callsite-linenumber}|${message}"/>
    </targets>

    <rules>
        <!-- All messages with a minimum log level of Debug or higher are written to the Console -->
        <logger name="*" minlevel="Debug" writeTo="console" />
    </rules>
</nlog>

Logger1

namespace LoggingWriteJsonWinFrmQ1
{
    internal class Logger1
    {
        public void CreateLogger()
        {
            Logger log = LogManager.GetCurrentClassLogger();

            log.Info("info message. Step-1");
            log.Info("info message. Step-2");
        }
    }
}

I'm getting the result now:

2024-02-28 22:21:36.6406|LoggingWriteJsonWinFrmQ1.Logger1.CreateLogger|17|info message. Step-1
2024-02-28 22:21:36.7066|LoggingWriteJsonWinFrmQ1.Logger1.CreateLogger|18|info message. Step-2

I expect the result:

2024-02-28 22:21:36.6406|LoggingWriteJsonWinFrmQ1|Logger1|CreateLogger|17|info message. Step-1
2024-02-28 22:21:36.7066|LoggingWriteJsonWinFrmQ1|Logger1|CreateLogger|18|info message. Step-2

Update-1

NLog.config

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets async="true">
        <target name="run_log"
                xsi:type="File"         
                fileName="${basedir}/Application.csv"
                keepFileOpen="true">
            <layout xsi:type="CSVLayout">
                <column name="DateTime" layout="${longdate}" />                 
                <column name="Project" layout="${callsite:className=true:includeSourcePath=false}" />
                <column name="Class" layout="${callsite:className=false:includeSourcePath=true}" />
                <column name="Method" layout="${callsite-linenumber}" />
                <column name="Message" layout="${message}" />               
            </layout>
        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="run_log" />
    </rules>
</nlog>

I get the result Application.csv (The fields are separated by dots)

DateTime,Project,Class,Method,Message
2024-02-29 21:17:39.8456,LogggingMainProjectTetst.Logger1.CreateLogger,CreateLogger,18,info message. Step-1
2024-02-29 21:17:41.4306,LogggingMainProjectTetst.Logger1.CreateLogger,CreateLogger,22,info message. Step-2

I expect the result Application.csv (The fields are separated by commas)

DateTime,Project,Class,Method,Message
2024-02-29 21:17:39.8456,LogggingMainProjectTetst,Logger1,CreateLogger,18,info message. Step-1
2024-02-29 21:17:41.4306,LogggingMainProjectTetst,Logger1,CreateLogger,22,info message. Step-2
0

There are 0 best solutions below