Reputation: 395
Background: I'm using WCF and MSMQ to execute long running jobs on a server. General error logging is done by Enterprise Library 5 Logging Application Block.
My problem is this: private LogWriter _writer = EnterpriseLibraryContainer.Current.GetInstance() is always null once the ExecutingMethod() has been pickup up from the queue and starts executing, resulting in not logging possible errors.
public class SerializedClass
{
private LogWriter _writer = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
public void ExecutingMethod()
{
try
{
.....
}
catch(Exception ex)
{
_writer.Write(ex, Category.General, Priority.Highest);
}
}
}
I've already checked that the logging config is visible to the executing assembly. My assumption is that there might be a problem with the serialization of the 'SerializedClass'?
Any help or input would be appreciated?
Upvotes: 0
Views: 882
Reputation: 30411
The LogWriter class is explicitly not serializable. It's not obvious what serialization you're doing from the code. Actually, it's not obvious from the code you're serializing at all, so I'm guessing here.
Anyway, I'm not sure what serializer you're using, but whatever it is, LogWriter will NOT be going through it cleanly. The .NET serializers typically don't re-run the constructors for types, so for fields that don't deserialize cleanly you'd probably get null, like you are.
The workaround is easy - don't grab the LogWriter until you actually need it. Grab it in the catch block instead of storing it in the constructor. In this case, it will probably be easier to use the older static facades instead, and just call Logger.Write() instead of going through a specific LogWriter instance.
-Chris
Upvotes: 2