Description
When serializing a PropertiesDictionary (as part of LoggingEvent) having null values --> NullReference exception occurs
Â
This happens because of:
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { foreach (DictionaryEntry dictionaryEntry in this.InnerHashtable.Clone() as IDictionary) { string key = dictionaryEntry.Key as string; object obj = dictionaryEntry.Value; // When dictionaryEntry.Value is null bool isSerializable = obj.GetType().IsSerializable; // NullReferenceException if (key != null && obj != null && isSerializable) info.AddValue(XmlConvert.EncodeLocalName(key), obj); } }
Suggestion (Not tested) :
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { foreach (DictionaryEntry dictionaryEntry in this.InnerHashtable.Clone() as IDictionary) { string key = dictionaryEntry.Key as string; object obj = dictionaryEntry.Value; bool isSerializable = obj == null ? false : obj.GetType().IsSerializable; if (key != null && obj != null && isSerializable) info.AddValue(XmlConvert.EncodeLocalName(key), obj); } }
Attachments
Issue Links
- duplicates
-
LOG4NET-581 NullReferenceException from log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData
- Resolved