Exemple #1
0
        public async Task LogFunctionCompletedAsync_CallsTraceWriter()
        {
            FunctionDescriptor descriptor = new FunctionDescriptor
            {
                ShortName = "TestJob"
            };
            FunctionCompletedMessage successMessage = new FunctionCompletedMessage
            {
                Function = descriptor
            };

            Exception ex = new Exception("Kaboom!");
            FunctionCompletedMessage failureMessage = new FunctionCompletedMessage
            {
                Function = descriptor,
                Failure  = new FunctionFailure {
                    Exception = ex
                },
                FunctionInstanceId = new Guid("8d71c9e3-e809-4cfb-bb78-48ae25c7d26d")
            };

            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Info, Host.TraceSource.Execution, "Executed: 'TestJob' (Succeeded)", null));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Error, Host.TraceSource.Execution, "Executed: 'TestJob' (Failed)", ex));
            _mockTraceWriter.Setup(p => p.Trace(TraceLevel.Error, Host.TraceSource.Host, "  Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '8d71c9e3-e809-4cfb-bb78-48ae25c7d26d'", null));

            await _logger.LogFunctionCompletedAsync(successMessage, CancellationToken.None);

            await _logger.LogFunctionCompletedAsync(failureMessage, CancellationToken.None);

            _mockTraceWriter.VerifyAll();
        }
        public async Task LogFunctionCompletedAsync_CallsTraceWriter()
        {
            FunctionDescriptor descriptor = new FunctionDescriptor
            {
                ShortName = "TestJob",
                FullName  = "TestNamespace.TestJob"
            };
            FunctionCompletedMessage successMessage = new FunctionCompletedMessage
            {
                Function           = descriptor,
                FunctionInstanceId = Guid.NewGuid(),
                HostInstanceId     = Guid.NewGuid()
            };

            Exception ex = new Exception("Kaboom!");
            FunctionCompletedMessage failureMessage = new FunctionCompletedMessage
            {
                Function = descriptor,
                Failure  = new FunctionFailure {
                    Exception = ex
                },
                FunctionInstanceId = new Guid("8d71c9e3-e809-4cfb-bb78-48ae25c7d26d"),
                HostInstanceId     = Guid.NewGuid()
            };

            await _logger.LogFunctionCompletedAsync(successMessage, CancellationToken.None);

            await _logger.LogFunctionCompletedAsync(failureMessage, CancellationToken.None);

            Assert.Equal(3, _traceWriter.Traces.Count);

            TraceEvent traceEvent = _traceWriter.Traces[0];

            Assert.Equal(TraceLevel.Info, traceEvent.Level);
            Assert.Equal(Host.TraceSource.Execution, traceEvent.Source);
            Assert.Equal(string.Format("Executed 'TestJob' (Succeeded, Id={0})", successMessage.FunctionInstanceId), traceEvent.Message);
            Assert.Equal(successMessage.HostInstanceId, traceEvent.Properties["MS_HostInstanceId"]);
            Assert.Equal(successMessage.FunctionInstanceId, traceEvent.Properties["MS_FunctionInvocationId"]);
            Assert.Same(successMessage.Function, traceEvent.Properties["MS_FunctionDescriptor"]);

            traceEvent = _traceWriter.Traces[1];
            Assert.Equal(TraceLevel.Error, traceEvent.Level);
            Assert.Equal(Host.TraceSource.Execution, traceEvent.Source);
            Assert.Equal(string.Format("Executed 'TestJob' (Failed, Id={0})", failureMessage.FunctionInstanceId), traceEvent.Message);
            Assert.Same(ex, traceEvent.Exception);
            Assert.Equal(failureMessage.HostInstanceId, traceEvent.Properties["MS_HostInstanceId"]);
            Assert.Equal(failureMessage.FunctionInstanceId, traceEvent.Properties["MS_FunctionInvocationId"]);
            Assert.Same(failureMessage.Function, traceEvent.Properties["MS_FunctionDescriptor"]);

            traceEvent = _traceWriter.Traces[2];
            Assert.Equal(TraceLevel.Error, traceEvent.Level);
            Assert.Equal(Host.TraceSource.Host, traceEvent.Source);
            Assert.Equal("  Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is '8d71c9e3-e809-4cfb-bb78-48ae25c7d26d'", traceEvent.Message);
            Assert.Same(ex, traceEvent.Exception);
            Assert.Equal(failureMessage.HostInstanceId, traceEvent.Properties["MS_HostInstanceId"]);
            Assert.Equal(failureMessage.FunctionInstanceId, traceEvent.Properties["MS_FunctionInvocationId"]);
            Assert.Same(failureMessage.Function, traceEvent.Properties["MS_FunctionDescriptor"]);
        }