When I try it (in a slightly "cut down" form as I don't have your JSON data) I get no such error:
var dict = new Dictionary < string, object>();
var logContext = new JObject();
foreach (var kvp in dict)
{
logContext?.Add(kvp.Key, kvp.Value as JToken ?? new JValue(kvp.Value));
}
I even tried "forcing" the conversion:
logContext?.Add(kvp.Key, (kvp.Value as JToken) ?? new JValue(kvp.Value));
but it made no difference.
So ... we can't help you directly.
I'd start by changing all the
var
definitions to explicit types, to be sure of what I was working with (VS will do that for you) then break out your single line of code into multiple statements to try and see what part of it is doesn't like.
Sorry - but if we can't reproduce it, we can't solve it!