Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Handling 6

Status
Not open for further replies.

kmfna

MIS
Sep 26, 2003
306
US
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

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.
 
This is a good example and it could be extended to:
- write to event log
- use StackFrame object to get the FileName, Method, & LineNumber
- use StackTrace object to access also the caller method information.
-obislavu-
 
Nice!
 
Veep -
Thanks for the compliment!

obi -
good suggestions, but I'm curious about stackframe...I've never seen that. I already have it writing the stack trace out, which should have the line number, form, and function. Either way, about the frame, how do I use it?
And the event log...that is a great idea...I hadn't even thought about it until you said something....

So, do you guys think that this might actually be useful to people?? I know I use it, but, wasn't sure of the wider implications.

Again, thanks to both of you!

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
the hints on how to capture the screen are worth a star in themselves.



mr s. <;)

 
Can you generalize it a little more, and put it in the FAQ area?

Good work.

Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Nice class,

I was thinking of making something like this but never even thought of doing a screenshot, what a great idea.
 
I have been looking for a "print screen" function, and this is come in really handy.

Thanks
MJ
 
Wow. That was easy to implement and works 100% as promised.

Thanks for sharing.




----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top