3

I am trying to serialize and store Exception objects to a file and then retrieve it back by deserializing.

File.Delete(_FilePath);
using (Stream stream = File.Open(_FilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    new BinaryFormatter().Serialize(stream, originalException);
}

using (Stream stream = File.Open(_FilePath, FileMode.Open, FileAccess.Read))
{
     Exception deserializedException = (Exception)(new BinaryFormatter().Deserialize(stream));
   // deserializedException.TargetSite is null!!!
}

However, the deserialized Exception object's TargetSite property is null. i.e., it doesn't get serialized as a result of which it isn't getting deserialized as well. The TargetSite property is of type MethodBase. (Serializing MethodBase doesn't make sense IMO)

Is there any other way of storing and retrieving full Exception object without any data loss?

1 Answer 1

1

Exception implements Iserializable and controls how it get serialized.

You can subclass from exception and override GetObjectData. Do not forget to add Serializable attribute to derived class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.