c#和MongoDB.不能完全保存Exception对象
本文关键字:保存 Exception 对象 MongoDB 不能 | 更新日期: 2023-09-27 17:49:00
我有一个类的一些字符串属性和一个"Exception"属性。当对象被保存时,所有字符串属性在MongoDB中都是OK的,但Exception是部分的。只有属性"_t"answers"HResult"在那里。
"Message","InnerException","StackTrace"等发生了什么?
我可以保存对象这样的MongoDB?
my class:
public class TraceLogEntity: MongoRepository.Entity {
public string ClassName {
get;
set;
}
public Exception Exception {
get;
set;
}
public string MethodName {
get;
set;
}
}
MongoDB上的对象片段:
{
"_id": ObjectId("55560138c57b9957202cae5a"),
"CreatedAt": ISODate("2015-05-05T03:00:00Z"),
"UserLogin": "UserLogin 3",
"ClassName": "ClassName 3",
"Exception": {
"_t": "LogException",
"HelpLink": null,
"Source": null,
"HResult": -2146233088
},
"MethodName": "MethodName 3"
}
我使用的是MongoRepository: https://github.com/RobThree/MongoRepository
提前感谢!!
. net Exception类没有被标记为Serializable。这可能会影响您在MongoDB中获取Message, InnerException等的能力。MongoRepository使用的JSON库无法从类中获取这些属性。
我要做的是将这些字段添加到TraceLogEntity类中,而不是使用Exception对象。所以它看起来像这样:
public class TraceLogEntity : MongoRepository.Entity
{
public string ClassName { get; set; }
public string Message { get; set; }
public string StackTrace { get; set; }
public TraceLogEntity InnerException { get; set; }
public string MethodName { get; set; }
}