Custom MsmqLogDistributor

Topics: Logging Application Block
Aug 5, 2013 at 9:58 AM
Edited Aug 5, 2013 at 9:59 AM
Hi all,
i have a question for you.

I have the following scenario:

I have a CustomLogEntry class that extends LogEntry provided from Enterprise Library 6 .

I've also written a CustomBinaryLogFormatter:

[ConfigurationElementType(typeof(CustomFormatterData))]
public class CustomBinaryLogFormatter : LogFormatter
{

    public CustomBinaryLogFormatter(object o)
    { }


    public override string Format(LogEntry log)
    {
        CustomLogEntry l = log as CustomLogEntry;
        using (MemoryStream binaryStream = new MemoryStream())
        {
            GetFormatter().Serialize(binaryStream, log);
            return Convert.ToBase64String(binaryStream.ToArray());
        }
    }

    public static  LogEntry Deserialize(string serializedLogEntry)
    {
        using (MemoryStream binaryStream = new  
          MemoryStream(Convert.FromBase64String(serializedLogEntry)))
        {
            return (CustomLogEntry)GetFormatter().Deserialize(binaryStream);
        }
    }

    private static BinaryFormatter GetFormatter()
    {
        return new BinaryFormatter();
    }
}
My configured queue (msmq) receives logged messages, but when it tries to deserialize them, it seems to be unable to do that and throws the following error:

Unable to deserialize message with id 301d01cb-8f75-48c9-b3ab-6c16ac24f85b\200724 from queue .\private$\PEF.Logging.Queue. Please make sure only LogEntries are traced and a BinaryFormatter is used to format the message. The offending message has been removed from the queue.

I think that i have to redefine ReceiveQueuedMessages method in MsmqLogDistributor class of Enterprise Library 6.

Now the method acts as following:

string serializedEntry = message.Body.ToString();
LogEntry logEntry = null;
                try
                {
                    logEntry = BinaryLogFormatter.Deserialize(serializedEntry);
                }

I would like to change it realizing something like that:

string serializedEntry = message.Body.ToString();
CustomLogEntry logEntry = null;
                try
                {
                    logEntry = CustomBinaryLogFormatter.Deserialize(serializedEntry);
                }
How can i configure the MsmqDistributor.exe.config in order to let me use the MsmqDistributor provided by the framework, with my little customization?

(I would like to avoid the implementation of an entire new custom DistributorService, because i only need to change the behaviour of the ReceiveQueuedMessages and let the MsmqDistributor.exe.config uses the right version of the MsmqLogDistributor class i.e. a CustomMsmqLogDistributor class)

Thanks for your patience
Aug 6, 2013 at 4:21 AM
There is no way to change the formatter used by the MSMQ Distributor Service using configuration. The way to do it would be to apply your code changes to the source code and compile a new version of the service.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to