如何在 Nlog.Web.AspNetCore 中省略转义“/”
How to omit escaping "/" in Nlog.Web.AspNetCore
我正在使用 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)" }
这看起来像是一个编码问题。试试下面的方法。
<attribute name="message" layout="${message}" encode="false"/>
我还建议像这样在布局文件中包含实际的异常
<attribute name="${exception}" layout="${exception:format=toString}" />
我正在使用 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)" }
这看起来像是一个编码问题。试试下面的方法。
<attribute name="message" layout="${message}" encode="false"/>
我还建议像这样在布局文件中包含实际的异常
<attribute name="${exception}" layout="${exception:format=toString}" />