Is there a way to deserialize the exception logs using EL. And show those in a GUI application.

Topics: Enterprise Library Core, Exception Handling Application Block
May 9, 2013 at 5:47 AM
We are having alot of exception logs written in mutiple places for different application. We need to monitor the logs files.

So we are planning to do a small GUI application which can list those excpetion logs. For that exception logs ahs to be desirialzied to a to exception objects.

Is there a option for doing so? Please help..
May 12, 2013 at 6:19 AM
Edited May 12, 2013 at 7:11 AM
Do you mean directly serialize application exceptions into a log and then deserialize the exceptions later?

If so, you could probably use the BinaryLogFormatter and then add the exception to the ExtendedProperties. The configuration could look something like this:
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <loggingConfiguration name="" tracingEnabled="false" defaultCategory="General"
            <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="trace.log" header="" footer="" formatter="Binary Log Message Formatter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Binary Log Message Formatter" />
            <add switchValue="All" name="General">
                    <add name="Flat File Trace Listener" />
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                    <add name="Flat File Trace Listener" />

And then to log you could use something similar to:
Logger.SetLogWriter(new LogWriterFactory().Create());

var ex = new InvalidCastException("BAD CAST");
// add some extra info to the exception as a test
ex.Data.Add("variable", "i");

LogEntry le = new LogEntry();
le.ExtendedProperties["Exception"] = ex;


Then you could open the file for reading, and Base64 decode each line and deserialize. A naive example (does not read through file locks, reads entire file into memory, does not check for file existence, etc.) would look something like this:
foreach (var line in File.ReadAllLines("trace.log"))
    byte[] bytes = Convert.FromBase64String(line);
    BinaryFormatter formatter = new BinaryFormatter();
    LogEntry logEntry = null;
    using (var ms = new MemoryStream(bytes))
        logEntry = formatter.Deserialize(ms) as LogEntry;

    Exception e = logEntry.ExtendedProperties["Exception"] as Exception;


    foreach (var key in e.Data.Keys)
        Console.WriteLine("\t" + key + "=" + e.Data[key]);

If you wanted a human readable format that still preserves .NET type fidelity then you could create a custom formatter that uses NetDataContractSerializer.

The example above is just using the Logging Application Block -- if you want to integrate through the Exception Handling Block then you will probably need to create a custom ExceptionFormatter to serialize the exception into a string. The string would then be set as the Message property of the LogEntry.

You could also look at LogParser from the EntLibContrib project if you wanted to parse log files based on the Template. I'm not sure what the status of that tool is, though.

Randy Levy
Enterprise Library support engineer
Support How-to
May 13, 2013 at 11:48 AM
Randy - Thanks alot.