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?