JSON logging in NLog

78 Views Asked by At
  1. How do I set up NLog so that structured text is saved to the json file?

Project: https://github.com/jhon65496/LoggingWriteJsonWinFrmQ1/tree/Logging

The ultimate goal:

  1. Get the fields from the NLog:
  • DateTime;
  • ProjectName where logging is triggered;
  • ClassName where logging is triggered;
  • Methodname where logging is triggered;
  • LineNumber where the logging is triggered;
  • Duration of execution between steps.
  1. Save to JSON
  2. Analyze JSON in Excel.

I'm using this code now.

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>
        <target name="file" xsi:type="File" fileName="log.json">
            <!-- Добавляем layout для вывода необходимых параметров -->
            <layout xsi:type="JsonLayout">
                <attribute name="time" layout="${longdate}" />
                <attribute name="message" layout="${message}"/>
            </layout>
        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
    </rules>
</nlog>

Logger1

internal class Logger1
{

    public void CreateLogger()
    {
        Logger log = LogManager.GetCurrentClassLogger();
                    
        log.Info("info message. Step-1");
        log.Info("info message. Step-2");
    }
}

Form1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        CreateLogger();
    }

    public void CreateLogger()
    {
        Logger1 lg = new Logger1();
        lg.CreateLogger();
    }
}

Now I get the following contents in the log.json file:

{ "time": "2024-02-28 12:54:23.1273", "message": "info message. Step-1" }
{ "time": "2024-02-28 12:54:23.2433", "message": "info message. Step-2" }

I expect the following content in the "log.json" file:

[
    { 
        "time": "2024-02-28 12:54:23.1273", 
        "message": "info message. Step-1" },
    { 
        "time": "2024-02-28 12:54:23.2433", 
        "message": "info message. Step-2" }
]

Update-1

Added indentJson="true"

<layout xsi:type="JsonLayout" indentJson="true">

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>
        <target name="file" xsi:type="File" fileName="log.json">
            <!-- Добавляем layout для вывода необходимых параметров -->
            <layout xsi:type="JsonLayout" indentJson="true">
                <attribute name="time" layout="${longdate}" />
                <attribute name="message" layout="${message}"/>
            </layout>
        </target>
    </targets>
 
    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
    </rules>
</nlog>

The result

{
  "time": "2024-02-28 15:46:11.0583",
  "message": "info message. Step-1"
}
{
  "time": "2024-02-28 15:46:11.1183",
  "message": "info message. Step-2"
}
1

There are 1 best solutions below

2
Qiang Fu On

You could use <layout ...indentJson="true"> for prettier json.