Get error with details

You can log error with all details you want. Here is the example. You need to just pass the exception object & it will return you the whole error details which will be very helpful.  



public static string GetErrorInfo(Exception ex)
        {
            StringBuilder errorRows = new StringBuilder();

            StackTrace stackTrace = new StackTrace(ex, true);
            StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            MethodBase methodBase = null;
            try
            {
                methodBase = stackFrame.GetMethod();
            }
            catch { }
            errorRows.AppendLine("<b>Server         : </b>" + System.Environment.MachineName + "<HR/>");
            errorRows.AppendLine("<b>Date Time         : </b>" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "<HR/>");
            try
            {
                errorRows.AppendLine("<b>User              : </b>" + SPContext.Current.Web.CurrentUser.Name + "<HR/>");
                errorRows.AppendLine("<b>Web               : </b>" + SPContext.Current.Web.Url + "<HR/>");
                errorRows.AppendLine("<b>Page              : </b>" + HttpContext.Current.Request.Url.AbsolutePath + "<HR/>");
            }
            catch { }

            try
            {
                errorRows.AppendLine("<b>Class Name        : </b>" + methodBase.DeclaringType.FullName + "<HR/>");
                errorRows.AppendLine("<b>Parent Method Name: </b>" + methodBase.Name + "<HR/>");
            }
            catch { }
            errorRows.AppendLine("<b>Error Method      : </b>" + ex.TargetSite.Name + "<HR/>");
            errorRows.AppendLine("<b>Method Tree       : </b>" + getMathodNames(stackTrace.GetFrames()) + "<HR/>");
            errorRows.AppendLine("<b>Assembly Name     : </b>" + Assembly.GetExecutingAssembly().FullName + "<HR/>");
            errorRows.AppendLine("<b>Error Type        : </b>" + ex.GetType().ToString() + "<HR/>");
            errorRows.AppendLine("<b>Message           : </b>" + ex.Message + "<HR/>");
            errorRows.AppendLine("<b>Source            : </b>" + ex.Source + "<HR/>");
            errorRows.AppendLine("<b>Target            : </b>" + ex.TargetSite.ToString() + "<HR/>");
            errorRows.AppendLine("<b>Stack Trace       : </b>" + ex.StackTrace + "<HR/>");



            // add any inner exceptions
            Exception innerException = ex.InnerException;

            while (innerException != null)
            {
                StackTrace stackTraceInner = new StackTrace(innerException, true);
                StackFrame stackFrameInner = stackTraceInner.GetFrame(stackTraceInner.FrameCount - 1);
                MethodBase methodBaseInner = stackFrameInner.GetMethod();
                errorRows.AppendLine("<b>---------------------Inner Exception---------------------</b><HR/>");
                errorRows.AppendLine("<b>Class Name        : </b>" + methodBaseInner.DeclaringType.FullName + "<HR/>");
                errorRows.AppendLine("<b>Parent Method Name: </b>" + methodBaseInner.Name + "<HR/>");
                errorRows.AppendLine("<b>Error Method      : </b>" + innerException.TargetSite.Name + "<HR/>");
                errorRows.AppendLine("<b>Method Tree       : </b>" + getMathodNames(stackTraceInner.GetFrames()) + "<HR/>");
                errorRows.AppendLine("<b>Error Type        : </b>" + innerException.GetType().ToString() + "<HR/>");
                errorRows.AppendLine("<b>Message           : </b>" + innerException.Message + "<HR/>");
                errorRows.AppendLine("<b>Source            : </b>" + innerException.Source + "<HR/>");
                errorRows.AppendLine("<b>Target            : </b>" + innerException.TargetSite.ToString() + "<HR/>");
                errorRows.AppendLine("<b>Stack Trace       : </b>" + innerException.StackTrace + "<HR/>");

                innerException = innerException.InnerException;
            }

            return errorRows.ToString();
        }

        private static string getMathodNames(StackFrame[] fs)
        {
            string methodNames = "";
            foreach (StackFrame stackFrame in fs)
            {
                MethodBase methodBase = stackFrame.GetMethod();
                methodNames += methodBase.Name + " ,";
            }

            return methodNames;
        }

Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Change Language for current user with JSOM in SharePoint Online

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

SharePoint 2010 CSS Chart