Skip to content

Commit 2a56472

Browse files
committed
adding exception details to FileLogger
1 parent 0653fcc commit 2a56472

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/WebJobs.Script/Diagnostics/FileLogger.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Globalization;
77
using System.Text;
8+
using Microsoft.Azure.WebJobs.Host;
89
using Microsoft.Extensions.Logging;
910

1011
namespace Microsoft.Azure.WebJobs.Script.Diagnostics
@@ -62,6 +63,28 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
6263
return;
6364
}
6465

66+
if (exception != null)
67+
{
68+
if (exception is FunctionInvocationException ||
69+
exception is AggregateException)
70+
{
71+
// we want to minimize the stack traces for function invocation
72+
// failures, so we drill into the very inner exception, which will
73+
// be the script error
74+
Exception actualException = exception;
75+
while (actualException.InnerException != null)
76+
{
77+
actualException = actualException.InnerException;
78+
}
79+
80+
formattedMessage += $"{Environment.NewLine}{actualException.Message}";
81+
}
82+
else
83+
{
84+
formattedMessage += $"{Environment.NewLine}{exception.ToFormattedString()}";
85+
}
86+
}
87+
6588
formattedMessage = FormatLine(stateValues, logLevel, formattedMessage);
6689
_fileWriter.AppendLine(formattedMessage);
6790

test/WebJobs.Script.Tests/Diagnostics/FileLoggerTests.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
8+
using Microsoft.Azure.WebJobs.Host;
89
using Microsoft.Azure.WebJobs.Logging;
910
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1011
using Microsoft.Extensions.Logging;
@@ -157,6 +158,62 @@ public void FileLogger_LogsExpectedLines()
157158
t => Assert.EndsWith("[Information,TestFunction] Test Message With Function", t));
158159
}
159160

161+
[Fact]
162+
public void FileLogger_LogsFunctionInvocationException()
163+
{
164+
var logger = new FileLogger(_category, _fileWriter, () => true, isPrimary: () => true, logType: LogType.Host);
165+
166+
// throw an exception so we have a stack
167+
try
168+
{
169+
var innerEx = new InvalidOperationException("boom!");
170+
var ex = new FunctionInvocationException("Error executing function.", innerEx);
171+
throw ex;
172+
}
173+
catch (Exception ex)
174+
{
175+
logger.LogError(ex, "Test Message");
176+
}
177+
178+
_fileWriter.Flush();
179+
180+
string logFile = Directory.EnumerateFiles(_logFilePath).Single();
181+
string[] lines = File.ReadAllLines(logFile);
182+
183+
// A FunctionInvocationException only logs the message.
184+
Assert.Collection(lines,
185+
t => Assert.EndsWith("[Error] Test Message", t),
186+
t => Assert.Equal("boom!", t));
187+
}
188+
189+
[Fact]
190+
public void FileLogger_LogsException()
191+
{
192+
var logger = new FileLogger(_category, _fileWriter, () => true, isPrimary: () => true, logType: LogType.Host);
193+
194+
// throw an exception so we have a stack
195+
try
196+
{
197+
var ex = new InvalidOperationException("boom!");
198+
throw ex;
199+
}
200+
catch (Exception ex)
201+
{
202+
logger.LogError(ex, "Test Message");
203+
}
204+
205+
_fileWriter.Flush();
206+
207+
string logFile = Directory.EnumerateFiles(_logFilePath).Single();
208+
string[] lines = File.ReadAllLines(logFile);
209+
210+
// This isn't a FunctionInvocationException, which means we'll see the stack.
211+
Assert.Collection(lines,
212+
t => Assert.EndsWith("[Error] Test Message", t),
213+
t => Assert.Equal("System.InvalidOperationException : boom!", t),
214+
t => Assert.Contains("at Microsoft.Azure.WebJobs.Script.Tests.Diagnostics.FileLoggerTests.FileLogger_LogsException()", t));
215+
}
216+
160217
[Fact]
161218
public void GetLogPrefix_ReturnsExpectedValue()
162219
{

0 commit comments

Comments
 (0)