55using System . Collections . Generic ;
66using System . Linq ;
77using System . Threading . Tasks ;
8+ using Microsoft . Azure . WebJobs . Host ;
9+ using Microsoft . Azure . WebJobs . Host . Listeners ;
810using Microsoft . Azure . WebJobs . Logging ;
911using Microsoft . Azure . WebJobs . Script . Configuration ;
1012using 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