public void ProjectRootStrippingTests(string[] projectRoots, string fileName, string expectedFileName) { var configuration = new Configuration("123456") { ProjectRoots = projectRoots }; var report = new Report(configuration, new System.Exception(), HandledState.ForHandledException(), new Breadcrumb[0], new Session()); foreach (var exception in report.Event.Exceptions) { var stacktrace = new StackTraceLine[] { new StackTraceLine(fileName, 1, string.Empty, false, null) }; exception["stacktrace"] = stacktrace; } InternalMiddleware.RemoveProjectRoots(report); foreach (var exception in report.Event.Exceptions) { foreach (var stacktraceline in exception.StackTrace) { Assert.Equal(expectedFileName, stacktraceline.FileName); } } }
private static StackTraceLines GetEventTraceLines(Exception exception, out MethodBase catchingMethod) { Assembly assembly = Assembly.GetExecutingAssembly(); if (assembly.EntryPoint == null) { assembly = Assembly.GetCallingAssembly(); } if (assembly.EntryPoint == null) { assembly = Assembly.GetEntryAssembly(); } catchingMethod = assembly == null ? null : assembly.EntryPoint; StackTraceLines lines = new StackTraceLines(); var stackTrace = new StackTrace(exception, true); StackFrame[] frames = stackTrace.GetFrames(); if (frames == null || frames.Length == 0) { StackTraceLine line = new StackTraceLine(); lines.Add(line); line.File = "unknown"; line.Line = 0; line.Function = "unknown"; return(lines); } foreach (StackFrame frame in frames) { MethodBase method = frame.GetMethod(); catchingMethod = method; int lineNumber = frame.GetFileLineNumber(); if (lineNumber == 0) { lineNumber = frame.GetILOffset(); } string file = frame.GetFileName(); if (String.IsNullOrEmpty(file)) { // disable ConditionIsAlwaysTrueOrFalse file = method.ReflectedType != null ? method.ReflectedType.FullName : "(unknown)"; // restore ConditionIsAlwaysTrueOrFalse } StackTraceLine line = new StackTraceLine(); line.File = file; line.Line = lineNumber; line.Function = method.Name; lines.Add(line); } return(lines); }