May 10, 2013 01:00 AM
(This post was last modified: May 10, 2013 04:15 AM by Exussit.)
Ok, well I was bored and just tweaking with one of my applications, and thought it'd be a fun idea to create a little Error Reporting System for when shit goes wrong haha. Now this obviously isn't going to be the best example you could find on the internet I'm sure, but I hope it'll at least benefit some of you :)
Ok, so basically, the error reporting magic will generically begin in the catch region of your code if you're implementing try/catch/finally into it. (Which you really should be).
And it's as simple as this:
Define an integer in your class outside of a method (so you don't have to re-define it in every method of your class).
Like so:
Feel free to rename the integer whatever the hell you want, but for simplicity's sake, keep the value to 1.
Now here's the main method for it inclusive of the catch (obviously you'll need to have a try beforehand).
Ok, I'll explain what this does bit by bit if you don't already know..
The GetFolderPath function basically retrieves (in this instance) the appdata folder because obviously, it's a unique directory on everyone's PC..
I've then used a while loop to say that whilst there is a file located in (Appdata Directory)/Exussit/ named Error Report No. "+k+".txt (i.e. the current value of the integer that we declared, so in this instance it's still "Error Report No. 1"), it will add +1 value to the integer that has been defined, and will keep on doing so until there is a file with a non-matching name, then proceed to create a new file name with the unallocated int.
The MessageBox line is rather self explanatory and provides information as to where the user can locate the created error report for handling.
For File.WriteAllText, this is the main part where it actually writes the error itself that has been caught (ex). It converts it to a readable string and then writes it to the designated text file.
The last line merely is for personal use in DebugTime and I've just used that to tell me the method has executed. (I prefer that to using breakpoints in some cases).
And well, that about wraps it up.
Now naturally, it's quite easy to add a little system on top of that which allows you to automatically send the report to a designated webhost or database; whatever you like if you're creative enough. I just thought I'd begin contributing a little more to the c# section considering it is my favourite programming language...
So anyway, hope this helped at least some of you haha. Enjoy :)
Alternative method: (Credit to Bausshacker)
If you prefer a more... uniformed approach to the file naming, you can remove the need for the int all together and go by an error type naming system instead.
Simply modify this line:
To this:
And update the MessageBox function etc.. Correlating to the new 'e' value instead of the integer.
Happy programming!
Ok, so basically, the error reporting magic will generically begin in the catch region of your code if you're implementing try/catch/finally into it. (Which you really should be).
And it's as simple as this:
Define an integer in your class outside of a method (so you don't have to re-define it in every method of your class).
Like so:
Code
int k = 1;Now here's the main method for it inclusive of the catch (obviously you'll need to have a try beforehand).
Code
catch (Exception ex)
{
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
while (File.Exists(AppData + "/Exussit/Error Report No. " + k + ".txt"))
{
k++;
}
MessageBox.Show("An Error has occured!" + Environment.NewLine + "Please Read:" + Environment.NewLine + AppData + "/Exussit/Error Report No. " + k + ".txt" + Environment.NewLine + "for more details.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
File.WriteAllText(AppData + "/Exussit/Error Report No. " + k + ".txt", ex.ToString());
Console.WriteLine("Created an Error Report in AppData.");
}The GetFolderPath function basically retrieves (in this instance) the appdata folder because obviously, it's a unique directory on everyone's PC..
I've then used a while loop to say that whilst there is a file located in (Appdata Directory)/Exussit/ named Error Report No. "+k+".txt (i.e. the current value of the integer that we declared, so in this instance it's still "Error Report No. 1"), it will add +1 value to the integer that has been defined, and will keep on doing so until there is a file with a non-matching name, then proceed to create a new file name with the unallocated int.
The MessageBox line is rather self explanatory and provides information as to where the user can locate the created error report for handling.
For File.WriteAllText, this is the main part where it actually writes the error itself that has been caught (ex). It converts it to a readable string and then writes it to the designated text file.
The last line merely is for personal use in DebugTime and I've just used that to tell me the method has executed. (I prefer that to using breakpoints in some cases).
And well, that about wraps it up.
Now naturally, it's quite easy to add a little system on top of that which allows you to automatically send the report to a designated webhost or database; whatever you like if you're creative enough. I just thought I'd begin contributing a little more to the c# section considering it is my favourite programming language...
So anyway, hope this helped at least some of you haha. Enjoy :)
Alternative method: (Credit to Bausshacker)
If you prefer a more... uniformed approach to the file naming, you can remove the need for the int all together and go by an error type naming system instead.
Simply modify this line:
Code
File.WriteAllText(AppData + "/Exussit/Error Report No. " + k + ".txt", ex.ToString());Code
File.AppendAllText(AppData + "/Exussit/Error Report. " + e + ".txt", ex.ToString());Happy programming!

