NonThrownExceptions

This commit is contained in:
Ben Adams
2017-11-11 21:59:14 +00:00
parent ba17506b3e
commit bab0cf679d
4 changed files with 140 additions and 15 deletions

View File

@@ -12,29 +12,38 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
namespace System.Diagnostics
{
public partial class EnhancedStackTrace
{
private static List<EnhancedStackFrame> GetFrames(Exception exception)
{
var frames = new List<EnhancedStackFrame>();
if (exception == null)
{
return new List<EnhancedStackFrame>();
}
var needFileInfo = true;
var stackTrace = new StackTrace(exception, needFileInfo);
return GetFrames(stackTrace);
}
private static List<EnhancedStackFrame> GetFrames(StackTrace stackTrace)
{
var frames = new List<EnhancedStackFrame>();
var stackFrames = stackTrace.GetFrames();
if (stackFrames == null)
{
return frames;
}
using (var portablePdbReader = new PortablePdbReader())
{
var needFileInfo = true;
var stackTrace = new StackTrace(exception, needFileInfo);
var stackFrames = stackTrace.GetFrames();
if (stackFrames == null)
{
return default;
}
for (var i = 0; i < stackFrames.Length; i++)
{
@@ -557,6 +566,16 @@ namespace System.Diagnostics
Debug.Assert(method != null);
try
{
var type = method.DeclaringType;
if (type == typeof(Task<>) && method.Name == "InnerInvoke")
{
return false;
}
if (type == typeof(Task) && method.Name == "ExecuteWithThreadLocal")
{
return false;
}
// Don't show any methods marked with the StackTraceHiddenAttribute
// https://github.com/dotnet/coreclr/pull/14652
foreach (var attibute in EnumerableIList.Create(method.GetCustomAttributesData()))
@@ -568,7 +587,6 @@ namespace System.Diagnostics
}
}
var type = method.DeclaringType;
if (type == null)
{
return true;

View File

@@ -1,17 +1,17 @@
// Copyright (c) Ben A Adams. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic.Enumerable;
using System.Diagnostics;
using System.Text;
namespace System.Diagnostics
{
public partial class EnhancedStackTrace : StackTrace, IEnumerable<EnhancedStackFrame>
{
public static EnhancedStackTrace Current() => new EnhancedStackTrace(new StackTrace(1 /* skip this one frame */, true));
private readonly List<EnhancedStackFrame> _frames;
// Summary:
@@ -35,6 +35,17 @@ namespace System.Diagnostics
_frames = GetFrames(e);
}
public EnhancedStackTrace(StackTrace stackTrace)
{
if (stackTrace == null)
{
throw new ArgumentNullException(nameof(stackTrace));
}
_frames = GetFrames(stackTrace);
}
/// <summary>
/// Gets the number of frames in the stack trace.
/// </summary>
@@ -63,7 +74,7 @@ namespace System.Diagnostics
/// <returns>A readable representation of the stack trace.</returns>
public override string ToString()
{
if (_frames == null) return "";
if (_frames == null || _frames.Count == 0) return "";
var sb = new StringBuilder();
@@ -82,7 +93,7 @@ namespace System.Diagnostics
{
if (i > 0)
{
sb.AppendLine();
sb.Append(Environment.NewLine);
}
var frame = frames[i];

View File

@@ -15,7 +15,11 @@ namespace System.Diagnostics
{
var stackTrace = new EnhancedStackTrace(exception);
stackTraceString.SetValue(exception, stackTrace.ToString());
if (stackTrace.FrameCount > 0)
{
stackTraceString.SetValue(exception, stackTrace.ToString());
}
exception.InnerException?.Demystify();
}
catch