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

View File

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

View File

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

View File

@ -0,0 +1,92 @@
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Xunit;
namespace Demystify
{
public class NonThrownException
{
[Fact]
public async Task DoesNotPreventThrowStackTrace()
{
// Arrange
Exception innerException = null;
try
{
await Task.Run(() => throw new Exception()).ConfigureAwait(false);
}
catch(Exception ex)
{
innerException = ex;
}
// Act
Exception demystifiedException = new Exception(innerException.Message, innerException).Demystify();
// Assert
var stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
var trace = stackTrace.Split(Environment.NewLine);
Assert.Equal(
new[] {
"System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.",
" at Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()+()=>{}",
" at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)",
" at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()",
" --- End of inner exception stack trace ---"},
trace);
// Act
try
{
throw demystifiedException;
}
catch (Exception ex)
{
demystifiedException = ex.Demystify();
}
// Assert
stackTrace = demystifiedException.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
trace = stackTrace.Split(Environment.NewLine);
Assert.Equal(
new[] {
"System.Exception: Exception of type 'System.Exception' was thrown. ---> System.Exception: Exception of type 'System.Exception' was thrown.",
" at Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()+()=>{}",
" at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)",
" at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()",
" --- End of inner exception stack trace ---",
" at async Task Demystify.NonThrownException.DoesNotPreventThrowStackTrace()"
},
trace);
}
[Fact]
public async Task Current()
{
// Arrange
EnhancedStackTrace est = null;
// Act
await Task.Run(() => est = EnhancedStackTrace.Current()).ConfigureAwait(false);
// Assert
var stackTrace = est.ToString();
stackTrace = ReplaceLineEndings.Replace(stackTrace, "");
var trace = stackTrace.Split(Environment.NewLine);
Assert.Equal(
new[] {
" at void System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, object state)",
" at bool System.Threading.ThreadPoolWorkQueue.Dispatch()"},
trace);
}
private Regex ReplaceLineEndings = new Regex(" in [^\n\r]+");
}
}