Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Error Handling & Custom Exceptions (C#.NET)

Posted on 2007-11-13
Medium Priority
Last Modified: 2013-12-17
I have a fairly large .NET 1.1 Web App.

In this application I have a class (SQL.cs) that handles all calls to the DB (SQL Server 2000).  There are aproximately 150+ different static functions with calls to the DB.  

I would like to build something that would throw an exception every time one of these functions crashes, that will (1) let me write some information about the error to a error log in my DB and (2) give me information or at least the name of the Stored procedure that was called during the error.

Any help would be appreciated.

Question by:b_poker
  • 2

Accepted Solution

Aurora27 earned 500 total points
ID: 20277226
You can have a log function that can be called from the exception handler. So, you have a log pointer and use it throughout your app. everywhere, passing it wherever required.

To get the method where the exception occured, use

System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() in the exception handler and write this to the log, with datetime stamp, so it wud help.

You can of course, add tags like M001..M019 for functions, so when logged u can search where the error occured.

catch (Exception ex)
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() + ex.ToString());
                MessageBox.Show("M001 - Setting home directory failed. Error message saved in log.", "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

and WriteLog() wud be ...

private void WriteLog(string sLogEntry)
                string sEntry = sLogEntry + "       " + DateTime.Now;
            catch (Exception ex)
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString() +  ex.ToString());
                MessageBox.Show("M0018 - Wrting to error log failed. Log Path - " + m_sLogPath, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

fsLog is:

private StreamWriter fsLog;

This should help.


Expert Comment

ID: 20294888
??? Isn't that enough?

Expert Comment

ID: 20511599
Forced accept.

EE Admin

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month11 days, 6 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question