为什么使用NLog获得FormatException?

问题描述 投票:1回答:1

我在内部NLog日志中看到了以下内容:

2020-05-10 16:38:09.9251 Warn Error when formatting a message. Exception: System.FormatException: The input string had an incorrect format.
   vid System.Text.StringBuilder.FormatError()
   vid System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   vid System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   vid System.String.Format(IFormatProvider provider, String format, Object[] args)
   vid NLog.LogEventInfo.CalcFormattedMessage()

如果这样填充LogEventInfo:

var logEvent = new LogEventInfo();
                    logEvent.Level = logData.LogLevel.ToLogLevel();
                    logEvent.Message = JsonConvert.SerializeObject(logData, Formatting.Indented);
                    logEvent.Properties.Add("logdata", logData);
                    logEvent.Parameters = new object[1] { logData };
                    _genLogger.Log(logEvent);

警告似乎来自于将消息设置为此:

{
  "ComputerName": null,
  "LogLevel": 5,
  "LogId": null,
  "PersonId": null,
  "Text": "Communication Message",
  "TimeStamp": "2020-05-10T16:42:55.9456429+02:00",
  "ExceptionTyp": 7,
  "UserMessage": null,
  "RelatedObjectJsonString": "[\r\n  {\r\n    \"Address\": \"https://192.168.130.29:44476/MyApp5Service/Client\",\r\n    \"IsEmpty\": false,\r\n    \"IsFaulted\": false,\r\n    \"Action\": \"GetUserConfigurations\",\r\n    \"CallDirection\": 0,\r\n    \"EventTime\": \"2020-05-10T16:42:55.9406583+02:00\",\r\n    \"IsCallback\": false\r\n  }\r\n]",
  "SystemInformation": ""
}

我在做什么错?

问候

c# nlog formatexception nlog-configuration
1个回答
0
投票

消息是字符串格式或结构格式的消息。这些参数用于格式化消息。

示例

字符串格式

[当所有模板持有人均为数字时(在{}之间),该消息用作字符串格式的消息。

logger.Info("Logon by user:{0} from ip_address:{1}", "Kenny", "127.0.0.1");

第一个参数是message,其他参数是parameters >>

结构格式:
logger.Info("Logon by {user} from {ip_address}", "Kenny", "127.0.0.1"); // Logon by "Kenny" from "127.0.0.1"

当前问题

所以我们在这里做类似的事情:

string.Format("{
  "ComputerName": null,
  "LogLevel": 5,
  "LogId": null,
  "PersonId": null,
  "Text": "Communication Message",
  "TimeStamp": "2020-05-10T16:42:55.9456429+02:00",
  "ExceptionTyp": 7,
  "UserMessage": null,
  "RelatedObjectJsonString": "[\r\n  {\r\n    \"Address\": \"https://192.168.130.29:44476/MyApp5Service/Client\",\r\n    \"IsEmpty\": false,\r\n    \"IsFaulted\": false,\r\n    \"Action\": \"GetUserConfigurations\",\r\n    \"CallDirection\": 0,\r\n    \"EventTime\": \"2020-05-10T16:42:55.9406583+02:00\",\r\n    \"IsCallback\": false\r\n  }\r\n]",
  "SystemInformation": ""
}", logData);

不要以为那是您需要的;)

解决方案

我不确定您是否需要消息中的数据。因此列出了这两个选项。

不需要您自己进行JSON转换数据。这可以在配置中完成。

消息中的数据

我建议在这里使用结构化日志。

logger.Log(loglevel, "Message with {LogData}", logData);

您可以在配置中将logData呈现为JSON,如下所示:

${event-properties:item=LogData:jsonEncode=true}

请参见${json-encode}${event-properties}

注意:您可以控制如何呈现消息(${message}),请参见How to use structured logging

数据不在消息中

使用.WithProperty,它是properties的语法糖。

logger.WithProperty("LogData", logData).Log(loglevel, "Message without LogData");

此外,您还可以使用${event-properties:item=LogData:jsonEncode=true}将对象呈现为JSON。>

© www.soinside.com 2019 - 2024. All rights reserved.