public void CreateTraceMonitor_AllErrors()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("AllErrors").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            TraceFilter.AnonymousTraceFilter traceFilter = (TraceFilter.AnonymousTraceFilter)traceMonitor.Filters.Single();
            Assert.Equal("One or more WebJob errors have occurred.", traceFilter.Message);

            int notification = 0;

            traceMonitor.Subscribe(p => notification++);
            Assert.Equal(0, traceFilter.GetEvents().Count());
            traceMonitor.Trace(new TraceEvent(TraceLevel.Error, "Error1"));
            IEnumerable <TraceEvent> traceEvents = traceFilter.GetEvents();

            Assert.Equal(1, traceEvents.Count());
            Assert.Equal("Error1", traceEvents.Single().Message);

            traceMonitor.Trace(new TraceEvent(TraceLevel.Error, "Error2"));
            traceEvents = traceFilter.GetEvents();
            Assert.Equal(1, traceEvents.Count());
            Assert.Equal("Error2", traceEvents.Single().Message);
            Assert.Equal(2, notification);
        }
        public void CreateTraceMonitor_AllErrors_Customized()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("AllErrors_Customized").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            Assert.Equal(TimeSpan.Parse("00:30:00"), traceMonitor.NotificationThrottle);

            TraceFilter.AnonymousTraceFilter traceFilter = (TraceFilter.AnonymousTraceFilter)traceMonitor.Filters.Single();
            Assert.Equal("Custom Message", traceFilter.Message);

            int notification = 0;

            traceMonitor.Subscribe(p => notification++);

            Assert.Equal(0, traceFilter.GetEvents().Count());
            traceMonitor.Trace(new TraceEvent(TraceLevel.Error, "Error1"));
            Assert.Equal(1, traceFilter.GetEvents().Count());
            Assert.Equal("Error1", traceFilter.GetEvents().Single().Message);

            traceMonitor.Trace(new TraceEvent(TraceLevel.Error, "Error2"));
            Assert.Equal(1, traceFilter.GetEvents().Count());
            Assert.Equal("Error2", traceFilter.GetEvents().Single().Message);

            // expect second notification to be ignored due to throttle
            Assert.Equal(1, notification);
        }
        public void CreateTraceMonitor_FunctionErrorHandler_CustomFilterType()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("Test2ErrorHandler").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            ErrorTriggerListener.CompositeTraceFilter traceFilter = (ErrorTriggerListener.CompositeTraceFilter)traceMonitor.Filters.Single();
            Assert.NotNull(traceFilter);
            Assert.Equal(typeof(Functions.CustomTraceFilter), traceFilter.InnerTraceFilter.GetType());

            // first log a function exception for a *different* function
            // don't expect it to pass filter
            FunctionInvocationException functionException = new FunctionInvocationException("Function failed", Guid.Empty,
                                                                                            "Microsoft.Azure.WebJobs.Extensions.Tests.Core.ErrorTriggerListenerTests+Functions.Foo", new Exception("Kaboom!"));

            TraceEvent traceEvent = new TraceEvent(TraceLevel.Error, "Kaboom!", null, functionException);

            traceMonitor.Trace(traceEvent);
            Assert.Equal(0, traceFilter.GetEvents().Count());

            functionException = new FunctionInvocationException("Function failed", Guid.Empty,
                                                                "Microsoft.Azure.WebJobs.Extensions.Tests.Core.ErrorTriggerListenerTests+Functions.Test2", new Exception("Kaboom!"));

            traceEvent = new TraceEvent(TraceLevel.Error, "Kaboom!", null, functionException);
            traceMonitor.Trace(traceEvent);
            Assert.Equal(1, traceFilter.GetEvents().Count());
            Assert.Same(functionException, traceFilter.GetEvents().Single().Exception);
        }
        public void CreateTraceMonitor_FunctionErrorHandler()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("TestErrorHandler").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            TraceFilter.AnonymousTraceFilter traceFilter = (TraceFilter.AnonymousTraceFilter)traceMonitor.Filters.Single();
            Assert.Equal("Function 'Functions.Test' failed.", traceFilter.Message);

            // first log a function exception for a *different* function
            // don't expect it to pass filter
            FunctionInvocationException functionException = new FunctionInvocationException("Function failed", new Exception("Kaboom!"))
            {
                MethodName = "Microsoft.Azure.WebJobs.Extensions.Tests.Core.ErrorTriggerListenerTests+Functions.Foo"
            };
            TraceEvent traceEvent = new TraceEvent(TraceLevel.Error, "Kaboom!", null, functionException);

            traceMonitor.Trace(traceEvent);
            Assert.Equal(0, traceFilter.GetEvents().Count());

            functionException = new FunctionInvocationException("Function failed", new Exception("Kaboom!"))
            {
                MethodName = "Microsoft.Azure.WebJobs.Extensions.Tests.Core.ErrorTriggerListenerTests+Functions.Test"
            };
            traceEvent = new TraceEvent(TraceLevel.Error, "Kaboom!", null, functionException);
            traceMonitor.Trace(traceEvent);
            Assert.Equal(1, traceFilter.GetEvents().Count());
            Assert.Same(functionException, traceFilter.GetEvents().Single().Exception);
        }
        public void CreateTraceMonitor_SlidingWindow_Customized()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("SlidingWindowErrorHandler_Customized").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            SlidingWindowTraceFilter traceFilter = (SlidingWindowTraceFilter)traceMonitor.Filters.Single();

            Assert.Equal(5, traceFilter.Threshold);
            Assert.Equal("Custom Message", traceFilter.Message);
        }
        public void CreateTraceMonitor_SlidingWindow()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("SlidingWindowErrorHandler").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            SlidingWindowTraceFilter traceFilter = (SlidingWindowTraceFilter)traceMonitor.Filters.Single();

            Assert.Equal(5, traceFilter.Threshold);
            Assert.Equal("5 events at level 'Error' or lower have occurred within time window 00:05:00.", traceFilter.Message);
        }
        public void CreateTraceMonitor_CustomFilterType()
        {
            ParameterInfo parameter = typeof(Functions).GetMethod("CustomFilterType").GetParameters()[0];

            TraceMonitor traceMonitor = ErrorTriggerListener.CreateTraceMonitor(parameter, _mockExecutor.Object);

            Assert.Equal(TimeSpan.Parse("00:30:00"), traceMonitor.NotificationThrottle);

            Functions.CustomTraceFilter traceFilter = (Functions.CustomTraceFilter)traceMonitor.Filters.Single();
            Assert.NotNull(traceFilter);
        }