?
Solved

Error Handling & Custom Exceptions (C#.NET)

Posted on 2007-11-13
4
Medium Priority
?
1,688 Views
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.

Cheers!
0
Comment
Question by:b_poker
[X]
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
4 Comments
 
LVL 3

Accepted Solution

by:
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.
Ex:

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)
        {
            try
            {
                string sEntry = sLogEntry + "       " + DateTime.Now;
                fsLog.WriteLine(sEntry);
                fsLog.Flush();
            }
            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.

CT.
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20294888
??? Isn't that enough?
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20511599
Forced accept.

Computer101
EE Admin
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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