Solved

Error Handling & Custom Exceptions (C#.NET)

Posted on 2007-11-13
4
1,670 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
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
Aurora27 earned 125 total points
Comment Utility
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
Comment Utility
??? Isn't that enough?
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Forced accept.

Computer101
EE Admin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now