Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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???
        }
    }
}
like image 268
John B Avatar asked Mar 01 '23 21:03

John B


2 Answers

My apps usually do two things on an error. The first is to write it to a local log file (even if it uses some type of database logging as well). Then it sends an e-mail of the error to a distribution list set up for support.

So if the database log write fails, the error is still in the local log file and also sent via e-mail.

If the e-mail fails, the error is still logged so you can troubleshoot the problem later.

Getting more fault tolerant than that would only be worth the effort for extremely mission critical applications, IMHO.

like image 112
Ron Savage Avatar answered Mar 03 '23 11:03

Ron Savage


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.

like image 31
Treb Avatar answered Mar 03 '23 10:03

Treb