简体   繁体   中英

C# and MongoDB. Can't save completely Exception objects

I have a class with some string attributes and one "Exception" attribute. When the object is saved, all string attributes are OK in MongoDB but the Exception one is partially. Only the attributes "_t" and "HResult" are there.

What happened to "Message", "InnerException", "StackTrace" and so on?

Can I save objects like that to MongoDB?

Piece of my class:

public class TraceLogEntity: MongoRepository.Entity {
public string ClassName {
    get;
    set;
}
public Exception Exception {
    get;
    set;
}
public string MethodName {
    get;
    set;
}
}

Piece of the object on 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"
}

I'm using MongoRepository: https://github.com/RobThree/MongoRepository

Thanks in advance!!

The .NET Exception class is not marked as Serializable. This is probably affecting your ability to get Message, InnerException, etc in your MongoDB. The JSON library used by MongoRepository is unable to obtain those attributes from the class.

What I would do is add those fields to your TraceLogEntity class instead of using the Exception object. So it would look something like:

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM