Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# NLog - How to stop replacing newlines by NLog?

Tags:

c#

nlog

I'm trying to figure out how to stop NLog from replacing the Newlines in the strings I'm logging. I want the output to include all the line breaks and not place the entire output onto one line.

Can anybody help?

Config:

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="udp" xsi:type="NLogViewer" address="udp4://192.168.0.101:7071" />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="udp" />
  </rules>
</nlog>

Code:

var logger = LogManager.GetLogger($"Test");
var dumpStr = builder.ToString();
logger.Info(dumpStr);
like image 861
Trevor Avatar asked Dec 21 '25 20:12

Trevor


1 Answers

The way the output looks is controlled by the layout. The message is formatted. You can tell it to output raw with a ${message:raw=true} declaration.

The default layout is:

${longdate}|${level:uppercase=true}|${logger}|${message}

So we would change that to:

${longdate}|${level:uppercase=true}|${logger}|${message:raw=true}

You would add this option to your target like so:

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="udp" 
            xsi:type="NLogViewer" 
            address="udp4://192.168.0.101:7071" 
            layout="${longdate}|${level:uppercase=true}|${logger}|${message:raw=true}"
    />
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="udp" />
  </rules>
</nlog>
like image 125
HackSlash Avatar answered Dec 24 '25 11:12

HackSlash