使用 NLog 记录对象时的额外引号
Extra quotes when logging objects with NLog
我有一个代码可以使用 NLog 将内插字符串输出到日志。
这是一个例子
_logger.LogDebug($"REQUEST {webRequest.RequestUri}", Id)
WebResponse webResponse = await _httpService.SendRequestAsync(webRequest);
var response = ParseResponse(webResponse);
_logger.LogDebug($"RESPONSE {(int)response.StatusCode} {JsonConvert.SerializeObject(response.Body)}", Id);
在此示例中,在调用函数 _logger.LogDebug 的第一种情况下,我得到了预期的结果:
2019-04-15 09:27:24.5027 DEBUG e1701b07-d228-4543-a320-3cb1b7f2e4b0 REQUEST http://url/
但在第二种情况下,预期结果包含在额外的引号中。
2019-04-15 09:27:57.2907 DEBUG "e1701b07-d228-4543-a320-3cb1b7f2e4b0 RESPONSE 200 [{...},{...}]"
这里是_logger.LogDebug方法
using NLog;
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
public void LogDebug(string message, Guid id)
{
Logger.Debug($"{id.ToString()} {message}");
}
JsonConvert.SerializeObject(response.Body)的结果是json数组的字符串表示,例如:[{"key":"value","key":"value"},{"key":"value","key":"value"}]
这是我的一部分Nlog.config
<targets>
<target name="csv" xsi:type="File" fileName="${shortdate}-${level}-services.log">
<layout xsi:type="CSVLayout" delimiter="Tab" withHeader="false">
<column name="time" layout="${longdate}" />
<column name="level" layout="${uppercase:${level}}"/>
<column name="message" layout="${message}" />
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="csv" />
</rules>
为什么在第二种情况下我会得到额外的报价,我该如何避免?
根据the docs,
CSV Options quoting - Default Quoting mode for columns.
Default: Auto
Possible values:
Auto - Quote only whose values contain the quote symbol, the separator or newlines (Slow)
All - Quote all column. Useful for data known to be multiline such as Exception-ToString
(Fast) Nothing - Quote nothing (Very Fast)
默认值为 Auto
- 这意味着它引用您的字符串,因为它包含引号、制表符(分隔符)或新行。
这样做很重要,这样文件才能成为有效的 CSV 文件(否则 Excel 等人不知道列数据的开始和结束位置。
我有一个代码可以使用 NLog 将内插字符串输出到日志。
这是一个例子
_logger.LogDebug($"REQUEST {webRequest.RequestUri}", Id)
WebResponse webResponse = await _httpService.SendRequestAsync(webRequest);
var response = ParseResponse(webResponse);
_logger.LogDebug($"RESPONSE {(int)response.StatusCode} {JsonConvert.SerializeObject(response.Body)}", Id);
在此示例中,在调用函数 _logger.LogDebug 的第一种情况下,我得到了预期的结果:
2019-04-15 09:27:24.5027 DEBUG e1701b07-d228-4543-a320-3cb1b7f2e4b0 REQUEST http://url/
但在第二种情况下,预期结果包含在额外的引号中。
2019-04-15 09:27:57.2907 DEBUG "e1701b07-d228-4543-a320-3cb1b7f2e4b0 RESPONSE 200 [{...},{...}]"
这里是_logger.LogDebug方法
using NLog;
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
public void LogDebug(string message, Guid id)
{
Logger.Debug($"{id.ToString()} {message}");
}
JsonConvert.SerializeObject(response.Body)的结果是json数组的字符串表示,例如:[{"key":"value","key":"value"},{"key":"value","key":"value"}]
这是我的一部分Nlog.config
<targets>
<target name="csv" xsi:type="File" fileName="${shortdate}-${level}-services.log">
<layout xsi:type="CSVLayout" delimiter="Tab" withHeader="false">
<column name="time" layout="${longdate}" />
<column name="level" layout="${uppercase:${level}}"/>
<column name="message" layout="${message}" />
</layout>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="csv" />
</rules>
为什么在第二种情况下我会得到额外的报价,我该如何避免?
根据the docs,
CSV Options quoting - Default Quoting mode for columns.
Default: Auto
Possible values:
Auto - Quote only whose values contain the quote symbol, the separator or newlines (Slow)
All - Quote all column. Useful for data known to be multiline such as Exception-ToString
(Fast) Nothing - Quote nothing (Very Fast)
默认值为 Auto
- 这意味着它引用您的字符串,因为它包含引号、制表符(分隔符)或新行。
这样做很重要,这样文件才能成为有效的 CSV 文件(否则 Excel 等人不知道列数据的开始和结束位置。