问题
I am persisting NLog logging statements to my RavenDb database. The LogEventInfo class, which represents a log statement, has a property, LogLevel, with a private constructor. Instances of LogLevel (Info, Warn, etc.) are created via static, readonly properties that call the private constructor. 
The problem is that I wish to read the messages out of the database and querying for them is throwing a Json.Net serialization error:
Unable to find a constructor to use for type NLog.LogLevel. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Level.Name'.
How can I get around the error? Could creating some kind of Raven index help here?
回答1:
I think the easiest way to do this, is to create a custom class to hold all the log information.
回答2:
It worked with me like this
create a CustomCreationConverter and use it with your object's deserialization
public class EventInfoConverter : CustomCreationConverter<LogEventInfo>
    {
        private string _level { get; set; }
        public EventInfoConverter(string s)
        {
            JToken eventinfo = JObject.Parse(s);
            var childs = eventinfo.Children();
            foreach (var item in childs)
            {
                if (((Newtonsoft.Json.Linq.JProperty)item).Name == "Level")
                {
                    var m = ((Newtonsoft.Json.Linq.JProperty)item).Value.Children();
                    foreach (var item1 in m)
                    {
                        _level = ((Newtonsoft.Json.Linq.JProperty)item1).Value.ToString();
                        break;
                    }
                    break;
                }
            }
        }
        public override LogEventInfo Create(Type objectType)
        {
            LogEventInfo eventInfo = new LogEventInfo();
            switch (_level)
            {
                case "Info":
                         eventInfo = new LogEventInfo(LogLevel.Info, "", "");
                    break;
                case "Debug":
                    eventInfo = new LogEventInfo(LogLevel.Debug, "", "");
                    break;
                case "Error":
                    eventInfo = new LogEventInfo(LogLevel.Error, "", "");
                    break;
                case "Warn":
                    eventInfo = new LogEventInfo(LogLevel.Warn, "", "");
                    break;
                default:
                    break;
            }
            return eventInfo;
        }
    }
- In your deserialization: - NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger(); _logger.Log(Newtonsoft.Json.JsonConvert.DeserializeObject<LogEventInfo>(eventInfo, new EventInfoConverter(eventInfo)));
来源:https://stackoverflow.com/questions/15327589/jsonserializationexception-for-type-with-private-constructor