Skip to content

Commit d2157ea

Browse files
authored
Logging function name from FunctionException (#9912)
* Extracts function name from FunctionException base exception
1 parent fb62307 commit d2157ea

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/WebJobs.Script.WebHost/Diagnostics/SystemLogger.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Azure.WebJobs.Host;
88
using Microsoft.Azure.WebJobs.Host.Indexers;
9+
using Microsoft.Azure.WebJobs.Host.Listeners;
910
using Microsoft.Azure.WebJobs.Logging;
1011
using Microsoft.Azure.WebJobs.Script.Configuration;
1112
using Microsoft.Azure.WebJobs.Script.Eventing;
@@ -172,7 +173,7 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
172173
if (exception != null)
173174
{
174175
// Populate details from the exception.
175-
if (string.IsNullOrEmpty(functionName) && exception is FunctionInvocationException fex)
176+
if (string.IsNullOrEmpty(functionName) && exception is FunctionException fex)
176177
{
177178
functionName = string.IsNullOrEmpty(fex.MethodName) ? string.Empty : fex.MethodName.Replace("Host.Functions.", string.Empty);
178179
}

test/WebJobs.Script.Tests/Eventing/SystemLoggerTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading.Tasks;
8+
using Microsoft.Azure.WebJobs.Host;
9+
using Microsoft.Azure.WebJobs.Host.Listeners;
810
using Microsoft.Azure.WebJobs.Logging;
911
using Microsoft.Azure.WebJobs.Script.Configuration;
1012
using Microsoft.Azure.WebJobs.Script.WebHost;
@@ -149,6 +151,22 @@ public void Log_Error_EmitsExpectedEvent()
149151
_mockEventGenerator.VerifyAll();
150152
}
151153

154+
[Theory]
155+
[MemberData(nameof(FunctionExceptionDataProvider.TestCases), MemberType = typeof(FunctionExceptionDataProvider))]
156+
public void Log_FunctionException_EmitsExpectedEvent(FunctionException functionException, string functionName)
157+
{
158+
string eventName = string.Empty;
159+
string functionInvocationId = string.Empty;
160+
string activityId = string.Empty;
161+
162+
_mockEventGenerator.Setup(p => p.LogFunctionTraceEvent(LogLevel.Error, _subscriptionId, _websiteName, functionName, eventName, string.Empty, functionException.ToFormattedString(), functionException.Message, functionException.InnerException.GetType().ToString(), functionException.InnerException.Message, functionInvocationId, _hostInstanceId, activityId, _runtimeSiteName, _slotName, It.IsAny<DateTime>()));
163+
164+
ILogger localLogger = new SystemLogger(_hostInstanceId, string.Empty, _mockEventGenerator.Object, _environment, _debugStateProvider.Object, null, new LoggerExternalScopeProvider(), _appServiceOptions);
165+
localLogger.LogError(functionException, functionException.Message);
166+
167+
_mockEventGenerator.VerifyAll();
168+
}
169+
152170
[Fact]
153171
public void Log_Sanitization()
154172
{
@@ -291,5 +309,26 @@ public void AppEnvironment_Reset_OnSpecialization()
291309
Assert.Equal("updatedslot", evt.SlotName);
292310
Assert.Equal("updatedruntimesitename", evt.RuntimeSiteName);
293311
}
312+
313+
public class FunctionExceptionDataProvider
314+
{
315+
public static IEnumerable<object[]> TestCases
316+
{
317+
get
318+
{
319+
yield return new object[] { new FunctionListenerException("Functions.TestFunction", new Exception("Kaboom")), "Functions.TestFunction" };
320+
yield return new object[] { new FunctionInvocationException("Invocation failed", Guid.Empty, "Functions.TestFunction", new Exception("Kaboom")), "Functions.TestFunction" };
321+
yield return new object[] { new FunctionTeapotException("Host.Functions.Teapot", new Exception("Super Kaboom")), "Teapot" };
322+
}
323+
}
324+
}
325+
326+
private class FunctionTeapotException : FunctionException
327+
{
328+
public FunctionTeapotException(string methodName, Exception innerException)
329+
: base($"Error 418. The function '{methodName}' is a teapot", methodName, innerException)
330+
{
331+
}
332+
}
294333
}
295334
}

0 commit comments

Comments
 (0)