【发布时间】:2019-06-05 05:17:00
【问题描述】:
我正在使用 NLog,并且正在通过网络发送一个 LogEventInfo 对象,我不拥有这个对象,因此我不能用 [JsonConverter(typeof(StringEnumConverter))] 装饰它,或者我可以但我不知道如何
我尝试了以下代码,但没有帮助
var strategy = new Newtonsoft.Json.Serialization.CamelCaseNamingStrategy();
var sec = new StringEnumConverter(strategy, false);
//TEMP Hard code serialization and message routing for just logs
LogEventInfo info = Newtonsoft.Json.JsonConvert.DeserializeObject<LogEventInfo>(msg.Body, sec);
有没有人运气好让枚举反序列化并且枚举在你不拥有的类中?
这是消息:
{
"date":"2019-06-04 21:48:24.0753",
"level":"Error",
"message":"{\"ApplicationId\":1390760,\"AppStatus\":\"#PG2\",\"Status\":400,\"ErrorCode\": 1053 }",
"properties":"ResponseBody={\"ApplicationId\":1390760,\"AppStatus\":\"#PG2\",\"Status\":400,\"ErrorCode\": 1053 }|GroupId=392934|ApplicationId=1390760|Status=400",
"callsite":"HFD.Enterprise.Logging.Tests.LogTest.LoadTest",
"logger":"WebApiLog",
"machinename":"BRANDONHOSTVS"
}
这里是个例外:
Newtonsoft.Json.JsonSerializationException: Error converting value "Error" to type 'NLog.LogLevel'. Path 'level', line 1, position 72. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'NLog.LogLevel'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
--- End of inner exception stack trace ---
【问题讨论】:
-
看起来
"message"是一个嵌入的双序列化 JSON 字符串,而"properties"包含嵌入的 JSON,周围有一些附加文本。这可能是问题吗?即使您不能修改该类,您也可以共享其参考源,即minimal reproducible example?