C # and MongoDB. Cannot fully save Exception objects

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

What happened to "Message", "InnerException", "StackTrace", etc.?

Can I save objects like this to MongoDB?

Part of my class:

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

      

Part of an 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 am using MongoRepository: https://github.com/RobThree/MongoRepository

Thanks in advance!

+3


source to share


1 answer


The .NET Exception class is not marked Serializable. It probably affects your ability to receive messages, InnerException, etc. In your MongoDB. The JSON library used by MongoRepository cannot get these attributes from the class.

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



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

      

+3


source







All Articles