Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Nlog and writing to file as json

Tags:

json

nlog

I think I'm missing something as I can't seem to figure out how to have it write to a log file in json format using NLog setup in configuration file. The straight rolling file works fine, but not the json. The json target only outputs the message (not in json).

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        
    <targets async="true">
      <target xsi:type="File" name="rollingFile" fileName="${basedir}/logs/${shortdate}.log" archiveFileName="${basedir}/logs/{shortdate}_Archive{###}.log" archiveAboveSize="1000000" archiveNumbering="Sequence" layout="${longdate} ${uppercase:${level}} ${callsite} ${message}" />
      <target xsi:type="File" 
              name="rollingFileJson" 
              fileName="${basedir}/logs/${shortdate}.json" 
              archiveFileName="${basedir}/logs/{shortdate}_Archive{###}.json" 
              archiveAboveSize="1000000" 
              archiveNumbering="Sequence" 
              layout="${json-encode} ${message}">
      </target>

    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="rollingFile" />
      <logger name="*" minlevel="Trace" writeTo="rollingFileJson" />
    </rules>
  </nlog>
like image 990
Chris Holwerda Avatar asked Nov 11 '14 20:11

Chris Holwerda


People also ask

What is NLog writing?

NLog is “a free logging platform for . NET, Silverlight and Windows Phone with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.”

Where does NLog write to by default?

By default, the web. nlog file can be found at: C:\Program Files (x86)\Pleasant Solutions\Pleasant Password Server\www\web.

What is NLog DLL?

NLog is a flexible and free logging platform for various . NET platforms, including . NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.


1 Answers

As of the release of NLog 4.0.0 it is possible to use a layout that renders events as structured JSON documents.

Example taken from NLog project site:

<target name="jsonFile" xsi:type="File" fileName="${logFileNamePrefix}.json">
    <layout xsi:type="JsonLayout">
        <attribute name="time" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="message" layout="${message}" />
    </layout>
</target>

Edit: You can also create it in code.

Here is the link to the specific part of documentation.

And here the copied example:

var jsonLayout = new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("type", "${exception:format=Type}"),
        new JsonAttribute("message", "${exception:format=Message}"),
        new JsonAttribute("innerException", new JsonLayout
        {

            Attributes =
            {
                new JsonAttribute("type", "${exception:format=:innerFormat=Type:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
                new JsonAttribute("message", "${exception:format=:innerFormat=Message:MaxInnerExceptionLevel=1:InnerExceptionSeparator=}"),
            }
        },
        //don't escape layout
        false)
    }
};

For more info read the docs.

like image 183
Chrono Avatar answered Oct 12 '22 10:10

Chrono