Error Handling & Custom Exceptions (C#.NET)

Posted on 2007-11-13
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Aurora27 earned 125 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

728 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