TraceEventCache not serializable

Topics: Logging Application Block
Sep 14, 2012 at 9:10 PM

I'm using the EntLibLoggingProxyTraceListener to log WCF messages.

All is well when I use the text formatters, but I can't use the BinaryLogFormatter because I get this:

Extended Properties: TraceEventCache - System.Diagnostics.TraceEventCache
--> MachineName: W000VSD5106--> TimeStamp: 2012-09-14 20:45:52--> FullName: Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35--> AppDomainName: /LM/W3SVC/1/ROOT/CLS-1-129921291228371148--> WindowsIdentity: NT AUTHORITY\SYSTEM
Exception Information Details:======================================Exception Type: System.Runtime.Serialization.SerializationExceptionMessage: Type 'System.Diagnostics.TraceEventCache' in Assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.Data: System.Collections.ListDictionaryInternalTargetSite: System.Reflection.MemberInfo[] InternalGetSerializableMembers(System.RuntimeType)HelpLink: NULLSource: mscorlib
StackTrace Information Details: ======================================   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)   at Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter.Format(LogEntry log)   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener.FormatEntry(LogEntry entry)   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener.CreateMessage(LogEntry logEntry)   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.MsmqTraceListener.SendMessageToQueue(LogEntry logEntry)   at Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.TraceListenerWrapper.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)   at Microsoft.Practices.EnterpriseLibrary.Logging.LogSource.TraceData(TraceEventType eventType, Int32 id, LogEntry logEntry, TraceListenerFilter traceListenerFilter, TraceEventCache traceEventCache)   at Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterImpl.ProcessLog(LogEntry log, TraceEventCache traceEventCache)

As it turns out, TraceEventCache is not used by EntLib when used directly, but it is added by EntLibLoggingProxyTraceListener. But when logs are sent the the MSMQ Trace Listener, using the BinaryLogFormatter, it obviously fails...

Aside from building a custom EntLibLoggingProxyTraceListener and/or BinaryLogFormatter, is there anyway I can prevent this error?