简体   繁体   English

C# NLog - 如何停止用 NLog 替换换行符?

[英]C# NLog - How to stop replacing newlines by NLog?

I'm trying to figure out how to stop NLog from replacing the Newlines in the strings I'm logging.我试图弄清楚如何阻止 NLog 替换我正在记录的字符串中的换行符。 I want the output to include all the line breaks and not place the entire output onto one line.我希望 output 包含所有换行符,而不是将整个 output 放在一行上。

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

The way the output looks is controlled by the layout . output 的外观由layout控制。 Themessage is formatted. message已格式化。 You can tell it to output raw with a ${message:raw=true} declaration.您可以使用${message:raw=true}声明将其告知 output raw。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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