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???
}
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With