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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 36
Why use this lambda? 12 63
Need help with a query 14 39
Using sample c# simple example 1 26
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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