我正在使用Nlog.Web.AspNetCore 4.8登录到文件。在Nlog.config中,我已将布局配置为JsonLayout。问题是最终结果包含一个反斜杠,以转义原始错误消息中出现的“ /”。除非明确告知,否则是否可以将NLog配置为省略转义某些字符?
这是我的JsonLayout配置:
<target xsi:type="File" name="ownFile-web" fileName="${logDirectory}/logs.txt" >
<layout type='JsonLayout' includeAllProperties="true" maxRecursionLimit="20" >
<attribute name="time" layout="${longdate}" />
<attribute name="level" layout="${level}" />
<attribute name="logger" layout="${logger}" />
<attribute name="message" layout="${message}" />
</layout >
</target>
这是结果:
{ "time": "2019-03-01 01:12:07.2239", "level": "Error", "logger": "Scheduler.Api.Startup", "message": "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server\/Instance Specified)" }
我希望结果看起来像这样:
{ "time": "2019-03-01 01:12:07.2239", "level": "Error", "logger": "Scheduler.Api.Startup", "message": "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" }
答案 0 :(得分:1)
这看起来像是编码问题。请尝试以下。
<attribute name="message" layout="${message}" encode="false"/>
我还建议像这样在您的布局文件中包含实际的异常
<attribute name="${exception}" layout="${exception:format=toString}" />