Search code examples
c#asp.neterror-handlingenterprise-libraryerror-logging

What do you do if your error logging fails, and how do you test that its working in production?


  1. What do you do if you're error logging code fails?
  2. How do you make sure that its currently working?
  3. How do you know if its not working?
  4. How do you test that its working in a production environment?
  5. Should I throw an exception if all else fails?

The code below uses Microsoft's Enterprise Library Logging Application Block. How do you make it "better"?

using Microsoft.Practices.EnterpriseLibrary.Logging;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Trying to write some data to the DB
            ...
        }
        catch (Exception ex)
        {
            LogHelper.LogException(ex, "Trying to write to the DB");
        }
    }
}

public class LogHelper
{
    public static void LogException(Exception ex, string exceptionType)
    {
        try
        {
            // Simplified version, only logging the message
            Logger.Write(exceptionType);
        }
        catch
        {
            // What do you do here???
        }
    }
}

Solution

  • See the answers in my related question:

    If everything else fails, have a 'last resort logging' in your catch block. Log the exception to a text file in a location where this is highly unlikely to fail. If this last resort logging fails by throwing another exception, you can either swallow that exception, or terminate your app and display the error as a message box.

    In this specific (exceptional) case, swallowing the exception is the only way to not terminate the app.