3/27/2017
I’ve written a very helpfull ErrorLog Event class. When used correctly, it raises an event that you can use to process errors however you please. The main benefit to using this class is if you include it in a Code Library project, it provides a means of returning error messages to the application so the application can handle it. It does not however return the Exception itself, but you can modify it easily to do that if you need to. What it does return is a string containing all error messages and stacktrace’s for the error, including any and all inner exceptions. This tutorial is also a very good, and basic example of using Delegates to setup event handling between a DLL and an application. (Remember, DLLs normally only communicate with applications via return values from functions. This allows extra communications outside of return values.) This tutorial is no longer offered as a viewable Compilr project, but the source code is available below. The code has been modified to use function overloads so that strings and exceptions are passed using the same method name. NOTE: This code will not do anything by itself. It must be included in a project. NOTE: While this code does work to perform some simple error logging. I would highly advise using something like log4net instead. DEBUGGING NOTE: Normally we want error handlers in every function, however since this is the error handler, if we have errors here we really have some serious issues, but will never know about them as they won’t be reported back to the application anyways. For this reason, we’ll trap the errors here, but not handle them. This way at least the application won’t crash. USAGE: To use this, simply include it in your project, then write your catch blocks like this: Then in your application, in it’s main method, before anything else, put this: Then simply add the following function, and modify it to handle the error messages as you please. Here’s the class:catch(Exception ex)
{
ErrorLog.Add(ex);
}
ErrorLog.Added += newErrorLogAdded(ErrorLog_Added);
void ErrorLogAdded(stringMessage)
{
Console.WriteLine(Message);
}
using System;
namespace Utilities
{
// We need a delegate for the event, to be used in the application for
// detecting when the event was fired, and declaring what method to handle
// it with
public delegate void ErrorLogAdded(stringMessage);
public static class ErrorLog
{
// Create the event
public static event ErrorLogAdded Added;
// Method to add a string to the error handler, and fire the event
public static void Add(stringResponse)
{
try
{
// There's no processing to be done here, so just fire the event off
Added(Response);
}
catch { }
}
// Method to add an exception to the error handler, and fire the event
public static void Add(Exception ex)
{
try
{
// This is usefull for avoiding error messages in a threaded environment
// where the thread is shutting down.
if(ex.Message != "Thread was being aborted.")
{
// Start the message with the top error message and stacktrace
string Message = string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace);
// Create a local copy of the exception
Exception ce = ex;
while(ce.InnerException!=null)
{
// Add the inner exception message and stacktrace to the string
Message = string.Format("{0}\r\n{1}\r\n{2}", Message, ce.InnerException.Message, ce.StackTrace);
// Move to the inner exception for the next iteration
ce = ce.InnerException;
}
// We've built the message string, fire the event off.
Added(Message);
}
}
catch { }
}
}
}