Logging Exceptions In-Depth

Exceptions are safeguards to deal with any unexpected or exceptional situations and logging them helps finding the root cause of issues during runtime. Significance of logging becomes even more critical on production where resources to debug are very scarce. So, to have in-depth information on what gone wrong and where play a very important role and helps troubleshooting/resolving the issues effectively.

Below is the C# implementation to log the in-depth information about the exception raised during runtime:

void Application_Error(object sender, EventArgs e) 
	// Code that runs when an unhandled error occurs
	StringBuilder message = new StringBuilder();
	if (Server != null)
		Exception ex;
		for (ex = Server.GetLastError(); ex != null; ex = ex.InnerException)
			message.AppendFormat("{0}: {1}{2}", ex.GetType().FullName, ex.Message, ex.StackTrace);
	//Log the exception and inner exception information.
	Logger.Write(message); //Reference "Microsoft.Practices.EnterpriseLibrary.Logging"

Application_Error() would be the method in Global.asax file of your web application.


(Visited 15 times, 1 visits today)