private static ErrReport GetErrReport(int code, string msg) { try { ErrReport err; List <string> stackFrames; //------------------------------------------------------ // TODO - See the Standard version which uses the interface IStackTools to process the stack frame //------------------------------------------------------ err = new ErrReport(code, StackFrameTools.FirstNonWrappedMethod2(typeof(WrapErr)), msg); stackFrames = StackFrameTools.FirstNonWrappedTraceStack(typeof(WrapErr), 2); //stackFrames = StackFrameTools.FirstNonWrappedTraceStack(typeof(WrapErr), new StackTrace(0,true)); StringBuilder sb = new StringBuilder(100); stackFrames.ForEach((item) => sb.AppendLine(item)); err.StackTrace = sb.ToString(); return(WrapErr.LogErr(err)); } catch (Exception ex) { Debug.WriteLine("{0} on call to WrapErr.GetErrReport:{1} - {2}", ex.GetType().Name, ex.Message, ex.StackTrace); return(WrapErr.LogErr(new ErrReport(code, "UnknownClass", "UnknownMethod", msg))); } }
// Method like the Net standard interface for eventual replacement public static List <string> FirstNonWrappedTraceStack(Type typeToIgnore, int fromLevel) { bool firstNonWrapErr = false; string name = typeToIgnore.Name; StackTrace trace = new StackTrace(fromLevel, true); List <string> stackFrames = new List <string>(); for (int i = 0; i < trace.FrameCount; i++) { StackFrame sf = trace.GetFrame(i); // Skip over all entries until you hit the first not to ignore if (!firstNonWrapErr) { if (StackFrameTools.ClassName(sf) != name) { firstNonWrapErr = true; } else { continue; } } stackFrames.Add( String.Format("\t{0} : Line:{1} - {2}.{3}", StackFrameTools.FileName(sf), StackFrameTools.Line(sf), StackFrameTools.ClassName(sf), StackFrameTools.MethodName(sf))); } return(stackFrames); }
/// <summary> /// Helper to create an error report object with the class and method names of calling method that /// had an exeption occur. Names are found by relection /// </summary> /// <param name="code">Unique error code</param> /// <param name="msg">Error message</param> /// <param name="e">Exception of origine</param> /// <returns>And error report object</returns> private static ErrReport GetErrReport(int code, string msg, Exception e) { try { //------------------------------------------------------ // TODO - See the Standard version which uses the interface IStackTools to process the stack frame //------------------------------------------------------ if (msg == REPLACE_WITH_EXCEPTION_MSG) { msg = e.Message; } // Always needs to be called directly from one of the wrap objects to get the right number of the stack frame // This will give us the class and method names in which the wrap method is used return(WrapErr.LogErr(new ErrReport(code, StackFrameTools.FirstNonWrappedMethod2(typeof(WrapErr)), msg, e))); } catch (Exception ex) { Debug.WriteLine("{0} on call to WrapErr.GetErrReport:{1} - {2}", ex.GetType().Name, ex.Message, ex.StackTrace); return(WrapErr.LogErr(new ErrReport(code, "UnknownClass", "UnknownMethod", msg, e))); } }