private void PrintError(string where, Exception what) { ScriptHost.SubmitBoundaryEnd(null, 0); var stackTrace = new StackTrace(what, true); var frames = stackTrace.GetFrames() .Select(a => new { Frame = a, Method = a.GetMethod(), Type = a.GetMethod()?.DeclaringType }) .Where(a => a.Method != null && (!a.Type.Assembly.GetName().Name.Contains("mscorlib") && !a.Type.Assembly.GetName().Name.Contains("CitizenFX.Core") && !a.Type.Assembly.GetName().Name.StartsWith("System"))) .Select(a => new { name = EnhancedStackTrace.GetMethodDisplayString(a.Method).ToString(), sourcefile = a.Frame.GetFileName() ?? "", line = a.Frame.GetFileLineNumber(), file = $"@{m_resourceName}/{a.Type?.Assembly.GetName().Name ?? "UNK"}.dll" }); var serializedFrames = MsgPackSerializer.Serialize(frames); var formattedStackTrace = FormatStackTrace(serializedFrames); if (formattedStackTrace != null) { Debug.WriteLine($"^1SCRIPT ERROR in {where}: {what.GetType().FullName}: {what.Message}^7"); Debug.WriteLine("{0}", formattedStackTrace); } }
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { var skip = 1; while (true) { var stack = new StackFrame(skip); if (!stack.HasMethod()) { logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue("<unknown method>"))); return; } var method = EnhancedStackTrace.GetMethodDisplayString(stack.GetMethod()); var caller = method.DeclaringTypeName; if (!caller.StartsWith("Serilog") && !caller.StartsWith("Microsoft") && !caller.StartsWith("NStore.Tutorial.Support.ConsoleLoggerAdapter") && !caller.StartsWith("System.Runtime.CompilerServices.AsyncMethodBuilderCore") && !caller.StartsWith("NStore.Core.Persistence.PersistenceExtensions") ) { var dump = method.ToString(); logEvent.AddPropertyIfAbsent(new LogEventProperty("Caller", new ScalarValue(dump))); } skip++; } }
public void AppendWithFullNameFalseTest() { var resolvedMethod = EnhancedStackTrace.GetMethodDisplayString(GetType().GetMethods().First(m => m.Name == nameof(AppendWithFullNameFalseTest))); var sb = new StringBuilder(); Assert.Equal($"void {GetType().Name}.{nameof(AppendWithFullNameFalseTest)}()", resolvedMethod.Append(sb, false).ToString()); }
private static IEnumerable <StackFrameInfo> getCurrentStackFrames(Predicate <Type> skipFrame) { var enhancedStackTrace = EnhancedStackTrace.Current(); var stackFrames = enhancedStackTrace.GetFrames(); if (stackFrames == null) { yield break; } foreach (var stackFrame in stackFrames) { if (!stackFrame.HasMethod()) { continue; } var methodBase = stackFrame.GetMethod(); if (methodBase == null) { continue; } var resolvedMethod = EnhancedStackTrace.GetMethodDisplayString(methodBase); if (resolvedMethod == null) { continue; } var declaringType = methodBase.DeclaringType; if (declaringType == null) { continue; } if (declaringType == typeof(EnhancedStackTraceService)) { continue; } if (skipFrame(declaringType)) { continue; } yield return(new StackFrameInfo { Method = resolvedMethod.ToString(), File = stackFrame.GetFileName(), Line = stackFrame.GetFileLineNumber(), Column = stackFrame.GetFileColumnNumber() }); } }
public void DiagnosesGenericMethodDisplayString() { var sf = Example <Type> .StackFrame; try { var s = EnhancedStackTrace.GetMethodDisplayString(sf.GetMethod()); Assert.True(true, "Does not throw exception when diagnosing generic method display string."); } catch (Exception ioe) { Assert.True(false, "Must not throw an exception when diagnosing generic method display string."); } }
private static void EnrichFastDemystify(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) { ResolvedMethod callerMethod = null !; Type? callerType = null; { StackFrame[] frames = new StackTrace().GetFrames(); bool gotToSerilogYet = false; for (int i = 0; i < frames.Length; i++) { MethodBase?tempCallerMethod = frames[i].GetMethod(); if (tempCallerMethod is null) { continue; } callerMethod = EnhancedStackTrace.GetMethodDisplayString(tempCallerMethod); callerType = callerMethod.DeclaringType; if (callerType is null) { continue; } bool isSerilog = callerType.Namespace?.StartsWith("Serilog") ?? false; switch (gotToSerilogYet) { case false when !isSerilog: continue; case false: gotToSerilogYet = true; continue; } if (isSerilog) { continue; } //Finally found the right number //https://youtu.be/Vy7RaQUmOzE?t=203 break; } } //Now do the actual enriching, with nicer names string callingMethodStr = callerMethod.Name !; string callingTypeStr = callerType is null ? "<Module>" : StringBuilderPool.BorrowInline(static (sb, callerType) => sb.AppendTypeDisplayName(callerType, false), callerType);