Hello all, I have just finished with a simple error handling class that I thought might make things a bit simpler for people, if you'd like to use it. As always I would be happy to hear any recommendations, and see any changes you all make to this. It also uses a screenshot class that a co-worker made, and I added a little to, which I will also include on here. Enjoy, and Good luck!
Kevin
- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
Kevin
Code:
//Error Handler
using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data;
using Utils; //Screenshot class
namespace Errors
{
public class ErrorHandler
{
//These locations may need to be changed, based on the computer
private static string ErrorFolder = "c:\\Program_Errors\\";
private static string ScreenShotFolder = ErrorFolder + "Screen_Shots\\";
private static string defaultErrLoc = "";
private static string defaultDestinationFile = ErrorFolder + "MyProgramErrors.txt";
private static bool defaultTakeSnapShot = true;
private static string defaultSnapShotDestination = ScreenShotFolder +
System.DateTime.Now.ToString().Replace("/", ".").Replace(":", ".");
private static bool defaultShowErrorMessage = true;
private static string defaultErrorMessage = "The Program has Encountered an Error. " +
"Please notify your Program's Administrator";
//use Header of Error Location
public static void ErrorWriter(string ErrLoc, Exception ex)
{
ErrorWriter(ErrLoc, ex, defaultDestinationFile, defaultTakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile)
{
ErrorWriter(ErrLoc, ex, DestinationFile, defaultTakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot)
{
ErrorWriter(ErrLoc, ex, DestinationFile, TakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot, string SnapShotDestination)
{
ErrorWriter(ErrLoc, ex, DestinationFile, TakeSnapShot,
SnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage)
{
ErrorWriter(ErrLoc, ex, defaultDestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, bool TakeSnapShot, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(ErrLoc, ex, defaultDestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, ErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot, bool ShowErrorMessage)
{
ErrorWriter(ErrLoc, ex, DestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(ErrLoc, ex, defaultDestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, ErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage)
{
ErrorWriter(ErrLoc, ex, DestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(ErrLoc, ex, DestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, ErrorMessage);
}
//This is the function that is called by all other functions.
public static void ErrorWriter(string ErrLoc, Exception ex, string DestinationFile, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage, string ErrorMessage)
{//Main Function
#region SnapShot
if (TakeSnapShot)
{ //Takes a screen shot and saves it to the specified location
string SnapShotDir = "";
string [] splSnapShotDir = SnapShotDestination.Split('\\');
for (int c=0; c<splSnapShotDir.GetUpperBound(0); c++)
{
if (c == 0)
SnapShotDir += splSnapShotDir[c].ToString();
else
SnapShotDir += "\\" + splSnapShotDir[c].ToString();
}
if (!Directory.Exists(SnapShotDir))
{
Directory.CreateDirectory(SnapShotDir);
}
int Counter=0;
while (File.Exists(SnapShotDestination + ".Jpeg"))
{
SnapShotDestination += Counter.ToString();
Counter++;
}
try
{
Utils.Screenshot.SaveScreen(SnapShotDestination);
}
catch (Exception ehEx)
{
string ehErrLoc = "Error Handler Module";
bool ehTakeSnapShot = false;
bool ehShowErrorMessage = false;
ErrorWriter(ehErrLoc, ehEx, DestinationFile, ehTakeSnapShot, ehShowErrorMessage);
}
}
#endregion
#region Error File
string ErrorFileDir = "";
string [] splDestinationFile = DestinationFile.Split('\\');
for (int c=0; c<splDestinationFile.GetUpperBound(0); c++)
{
if (c==0)
ErrorFileDir += splDestinationFile[c].ToString();
else
ErrorFileDir += "\\" + splDestinationFile[c].ToString();
}
if (!Directory.Exists(ErrorFileDir))
{
Directory.CreateDirectory(ErrorFileDir);
}
StreamWriter ErrSW = new StreamWriter(DestinationFile, true);
string Err = "";
if (ErrLoc != "")
{
Err = "Error Location: " + ErrLoc + "\r\n";
}
Err += "Error Target: " + ex.TargetSite + "\r\n";
if (ex.InnerException != null)
Err+= "Inner Exception:\r\n" + ex.InnerException + "\r\n";
Err += "Exception Message:\r\n" +
ex.Message + "\r\n" +
"Stack Trace:\r\n" +
ex.StackTrace + "\r\n\r\n";
ErrSW.Write(DateTime.Now.ToString() + "\r\n");
ErrSW.WriteLine(Err);
ErrSW.Close();
#endregion
#region Error Message
if (ShowErrorMessage)
{
MessageBox.Show(ErrorMessage, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
//Use No Header of Error Location
public static void ErrorWriter(Exception ex)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, defaultTakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, string DestinationFile)
{
ErrorWriter(defaultErrLoc, ex, DestinationFile, defaultTakeSnapShot,
defaultSnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot, string SnapShotDestination)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
SnapShotDestination, defaultShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot, bool ShowErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, ErrorMessage);
}
public static void ErrorWriter(Exception ex, string DestinationFile, bool TakeSnapShot, bool ShowErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, DestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, defaultDestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, ErrorMessage);
}
public static void ErrorWriter(Exception ex, string DestinationFile, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, DestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, defaultErrorMessage);
}
public static void ErrorWriter(Exception ex, string DestinationFile, bool TakeSnapShot, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, DestinationFile, TakeSnapShot,
defaultSnapShotDestination, ShowErrorMessage, ErrorMessage);
}
public static void ErrorWriter(Exception ex, string DestinationFile, bool TakeSnapShot, string SnapShotDestination, bool ShowErrorMessage, string ErrorMessage)
{
ErrorWriter(defaultErrLoc, ex, DestinationFile, TakeSnapShot,
SnapShotDestination, ShowErrorMessage, ErrorMessage);
}
}
}
Code:
//Screen Shot
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;
namespace Utils
{
public class Screenshot
{
internal class NativeMethods
{
[DllImport("user32.dll")]
public extern static IntPtr GetDesktopWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern UInt64 BitBlt
(IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
System.Int32 dwRop);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
public static void SaveScreen(string filename)
{
Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics gr1 = Graphics.FromImage(myImage);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
gr1.ReleaseHdc(dc1);
myImage.Save(filename, ImageFormat.Jpeg);
}
public static void SaveScreen(string filename, ImageFormat imf)
{
Image myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
Graphics gr1 = Graphics.FromImage(myImage);
IntPtr dc1 = gr1.GetHdc();
IntPtr dc2 = NativeMethods.GetWindowDC(NativeMethods.GetDesktopWindow());
NativeMethods.BitBlt(dc1, 0, 0, Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height, dc2, 0, 0, 13369376);
gr1.ReleaseHdc(dc1);
myImage.Save(filename, imf);
}
}
}
- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.