how to log exception to flat file when the database exception logging fails

Topics: Enterprise Library Core, Exception Handling Application Block, Logging Application Block
May 19, 2014 at 12:42 PM
Hi All,

Below is the scenario..

Currently we are developing WCF Service and we are using Enterprise libarary for exception logging...

We want all the exceptions to be logged in database at first...

If the database exception log fails then we need to log the exception to flat file in IIS where we publish the web service....

if the flat file log fails due to any reason we want to log the exception to System event log of the IIS...

Could you please guide me how to do this using Enterprise library 5.0
May 20, 2014 at 6:02 AM
Enterprise Library has the concept of error sources. This is a list of trace listeners that will execute in the event that a trace listener fails. You can configure it using XML:
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <add name="Flat File Trace Listener" />

Or using the configuration tool.

One thing which might be an issue for you (or it might not) is that the errors special source will write out the exception information of the failure along with the original LogEntry information. The LogEntry will be formatted using the default format and will not use the original formatter.

Unfortunately (for your requirements), If the trace listener in the errors special source fails the exception is swallowed. One way around that is to create your own custom trace listener that logs to a flat file. Inside the trace listener you would catch all Exceptions and then call Response.AppendToLog to log to the IIS Log. You can use the Enterprise Library FlatFileTraceListener source code as the base for the trace listener since you don't need to change much. The other way to achieve what you want is to modify the Enterprise Library source code to have the exact behavior you want.

Randy Levy
Enterprise Library support engineer
Support How-to