I am using NLog, and am sending a LogEventInfo
object across the wire, I do not own this object and therefore I can't decorate it with [JsonConverter(typeof(StringEnumConverter))]
, or maybe I can and I just don't know how
I've tried the following code and it doesn't help
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);
Does anyone have any luck getting enums to deserialize and the enum is in a class you don't own?
Here is the message:
{
"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"
}
And here is the exception:
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 ---