Detailed Error Logging

If detailed error logging is needed you can use the following to get the page name as well as the method name that triggered the error. In my case I log errors to the event log as well as store them in the database using log4net. I like getting back detailed error messages so I can more easily find and fix the related bugs.

using System.Diagnostics;

using System. Reflection:

 catch (Exception ex)

{

//retrieves the name of the page so it does not have to be hardcoded
string currentPageName = System.IO.Path.GetFileName(Request.PhysicalPath);

StackFrame stackFrame = new StackFrame();

//retrieves the name of the current method so it does not have to be hardcoded
MethodBase methodBase = stackFrame.GetMethod();

logger.Error(String.Format("Page Name: {1}: There was an error in the Method: {2}, User ID: {0})",
UserID, currentPageName, methodBase.Name), ex);

}

posted @ Tuesday, August 5, 2008 2:26 PM
Print

Comments on this entry:

No comments posted yet.

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«May»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678