Skip to content

Commit ab809da

Browse files
committed
Log exception details from outermost exception (#6680)
1 parent 6479d9f commit ab809da

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/WebJobs.Script.WebHost/Extensions/ExceptionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static (string exceptionType, string exceptionMessage, string exceptionDe
2323

2424
string exceptionType = innerException.GetType().ToString();
2525
string exceptionMessage = Sanitizer.Sanitize(innerException.Message);
26-
string exceptionDetails = Sanitizer.Sanitize(innerException.ToFormattedString());
26+
string exceptionDetails = Sanitizer.Sanitize(exception.ToFormattedString());
2727

2828
return (exceptionType, exceptionMessage, exceptionDetails);
2929
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Xunit;
6+
7+
namespace Microsoft.Azure.WebJobs.Script.Tests.Extensions
8+
{
9+
public class ExceptionExtensionsTests
10+
{
11+
[Fact]
12+
public void GetExceptionDetails_ReturnsExpectedResult()
13+
{
14+
Exception innerException = new InvalidOperationException("Some inner exception");
15+
Exception outerException = new Exception("some outer exception", innerException);
16+
Exception fullException;
17+
18+
try
19+
{
20+
throw outerException;
21+
}
22+
catch (Exception e)
23+
{
24+
fullException = e; // Outer exception will have stack trace whereas the inner exception's stack trace will be null
25+
}
26+
27+
(string exceptionType, string exceptionMessage, string exceptionDetails) = fullException.GetExceptionDetails();
28+
29+
Assert.Equal("System.InvalidOperationException", exceptionType);
30+
Assert.Equal("Some inner exception", exceptionMessage);
31+
Assert.Contains("System.Exception : some outer exception ---> System.InvalidOperationException : Some inner exception \r\n End of inner exception\r\n at Microsoft.Azure.WebJobs.Script.Tests.Extensions.ExceptionExtensionsTests.GetExceptionDetails_ReturnsExpectedResult()", exceptionDetails);
32+
Assert.Contains("Extensions\\ExceptionExtensionsTests.cs : 20", exceptionDetails);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)