带有私有构造函数的类型的JsonSerializationException [英] JsonSerializationException for type with private constructor
问题描述
我正在将NLog
日志记录语句持久保存到我的RavenDb
数据库中. LogEventInfo
类代表一个日志语句,它具有一个带有私有构造函数的属性LogLevel
. LogLevel
的实例(信息,警告等)是通过调用私有构造函数的静态只读属性创建的.
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.
问题是我希望从数据库中读取消息,并查询它们会引发Json.Net
序列化错误:
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:
找不到用于NLog.LogLevel类型的构造函数.一个类应该具有一个默认的构造函数,一个带有参数的构造函数或一个标有JsonConstructor属性的构造函数.路径"Level.Name".
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'.
如何解决该错误?可以在这里创建某种Raven索引吗?
How can I get around the error? Could creating some kind of Raven index help here?
推荐答案
我认为最简单的方法是创建一个自定义类来保存所有日志信息.
I think the easiest way to do this, is to create a custom class to hold all the log information.
这篇关于带有私有构造函数的类型的JsonSerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!