Пример #1
0
        private HostingDiagnosticListener CreateHostingListener(bool aspNetCore2, TelemetryConfiguration config = null)
        {
            HostingDiagnosticListener hostingListener;

            if (config != null)
            {
                hostingListener = new HostingDiagnosticListener(
                    config,
                    CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                    CommonMocks.GetMockApplicationIdProvider(),
                    injectResponseHeaders: true,
                    trackExceptions: true,
                    enableW3CHeaders: false,
                    enableNewDiagnosticEvents: aspNetCore2);
            }
            else
            {
                hostingListener = new HostingDiagnosticListener(
                    CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                    CommonMocks.GetMockApplicationIdProvider(),
                    injectResponseHeaders: true,
                    trackExceptions: true,
                    enableW3CHeaders: false,
                    enableNewDiagnosticEvents: aspNetCore2);
            }

            hostingListener.OnSubscribe();
            return(hostingListener);
        }
        public void OnBeginRequestWithNoW3CHeadersAndRequestIdIsTrackedCorrectly()
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            this.middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true);

            var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

            context.Request.Headers[RequestResponseHeaders.RequestIdHeader]          = "|abc.1.2.3.";
            context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";

            middleware.OnBeginRequest(context, Stopwatch.GetTimestamp());
            var activityInitializedByW3CHeader = Activity.Current;

            Assert.Equal("|abc.1.2.3.", activityInitializedByW3CHeader.ParentId);
            middleware.OnEndRequest(context, Stopwatch.GetTimestamp());

            Assert.Single(sentTelemetry);
            var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

            Assert.Equal($"|{activityInitializedByW3CHeader.GetTraceId()}.{activityInitializedByW3CHeader.GetSpanId()}.", requestTelemetry.Id);
            Assert.Equal(activityInitializedByW3CHeader.GetTraceId(), requestTelemetry.Context.Operation.Id);
            Assert.Equal("|abc.1.2.3.", requestTelemetry.Context.Operation.ParentId);

            Assert.Equal("abc", requestTelemetry.Properties["ai_legacyRootId"]);
            Assert.StartsWith("|abc.1.2.3.", requestTelemetry.Properties["ai_legacyRequestId"]);
        }
Пример #3
0
        public void OnBeginRequestWithW3CHeadersAndRequestIdIsTrackedCorrectly(bool isAspNetCore2)
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: true,
                       enableNewDiagnosticEvents: isAspNetCore2))
            {
                hostingListener.OnSubscribe();
                var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

                context.Request.Headers[RequestResponseHeaders.RequestIdHeader] = "|abc.1.2.3.";
                context.Request.Headers[W3C.W3CConstants.TraceParentHeader]     =
                    "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
                context.Request.Headers[W3C.W3CConstants.TraceStateHeader] = "state=some";
                context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";
                context.Request.Headers[RequestResponseHeaders.RequestContextHeader]     = "appId=something";

                HandleRequestBegin(hostingListener, context, 0, isAspNetCore2);

                var activityInitializedByW3CHeader = Activity.Current;

                if (isAspNetCore2)
                {
                    Assert.Equal("|abc.1.2.3.", activityInitializedByW3CHeader.ParentId);
                }

                Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", activityInitializedByW3CHeader.GetTraceId());
                Assert.Equal("00f067aa0ba902b7", activityInitializedByW3CHeader.GetParentSpanId());
                Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
                Assert.Equal("state=some", activityInitializedByW3CHeader.GetTracestate());
                Assert.Equal("v", activityInitializedByW3CHeader.Baggage.Single(t => t.Key == "k").Value);

                HandleRequestEnd(hostingListener, context, 0, isAspNetCore2);

                Assert.Single(sentTelemetry);
                var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

                Assert.Equal($"|4bf92f3577b34da6a3ce929d0e0e4736.{activityInitializedByW3CHeader.GetSpanId()}.",
                             requestTelemetry.Id);
                Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
                Assert.Equal("|4bf92f3577b34da6a3ce929d0e0e4736.00f067aa0ba902b7.",
                             requestTelemetry.Context.Operation.ParentId);

                Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader,
                                                                 out var appId));
                Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);

                if (isAspNetCore2)
                {
                    Assert.Equal("abc", requestTelemetry.Properties["ai_legacyRootId"]);
                    Assert.StartsWith("|abc.1.2.3.", requestTelemetry.Properties["ai_legacyRequestId"]);
                }
            }
        }
        public void OnBeginRequestWithW3CHeadersIsTrackedCorrectly()
        {
            var configuration = TelemetryConfiguration.CreateDefault();

            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            this.middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true);

            var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

            context.Request.Headers[W3CConstants.TraceParentHeader] = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
            context.Request.Headers[W3CConstants.TraceStateHeader]  = "state=some";
            context.Request.Headers[RequestResponseHeaders.CorrelationContextHeader] = "k=v";

            if (HostingDiagnosticListener.IsAspNetCore20)
            {
                var activity = new Activity("operation");
                activity.Start();

                middleware.OnHttpRequestInStart(context);

                Assert.NotEqual(Activity.Current, activity);
            }
            else
            {
                middleware.OnBeginRequest(context, Stopwatch.GetTimestamp());
            }

            var activityInitializedByW3CHeader = Activity.Current;

            Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", activityInitializedByW3CHeader.GetTraceId());
            Assert.Equal("00f067aa0ba902b7", activityInitializedByW3CHeader.GetParentSpanId());
            Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
            Assert.Equal("state=some", activityInitializedByW3CHeader.GetTracestate());
            Assert.Equal("v", activityInitializedByW3CHeader.Baggage.Single(t => t.Key == "k").Value);

            if (HostingDiagnosticListener.IsAspNetCore20)
            {
                middleware.OnHttpRequestInStop(context);
            }
            else
            {
                middleware.OnEndRequest(context, Stopwatch.GetTimestamp());
            }

            Assert.Single(sentTelemetry);
            var requestTelemetry = (RequestTelemetry)this.sentTelemetry.Single();

            Assert.Equal($"|4bf92f3577b34da6a3ce929d0e0e4736.{activityInitializedByW3CHeader.GetSpanId()}.", requestTelemetry.Id);
            Assert.Equal("4bf92f3577b34da6a3ce929d0e0e4736", requestTelemetry.Context.Operation.Id);
            Assert.Equal("|4bf92f3577b34da6a3ce929d0e0e4736.00f067aa0ba902b7.", requestTelemetry.Context.Operation.ParentId);

            Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader, out var appId));
            Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);
        }
Пример #5
0
        public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
        {
            var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry));

            middleware.OnHostingException(null, null);

            Assert.Equal(ExceptionHandledAt.Platform, ((ExceptionTelemetry)sentTelemetry).HandledAt);
        }
 public RequestTrackingMiddlewareTest()
 {
     this.middleware = new HostingDiagnosticListener(
         CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
         CommonMocks.GetMockApplicationIdProvider(),
         injectResponseHeaders: true,
         trackExceptions: true);
 }
Пример #7
0
        public void SdkVersionIsPopulatedByMiddleware()
        {
            var middleware = new HostingDiagnosticListener(CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry));

            middleware.OnHostingException(null, null);

            Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
            Assert.Contains(SdkVersionTestUtils.VersionPrefix, sentTelemetry.Context.GetInternalContext().SdkVersion);
        }
Пример #8
0
        public void TestLoggerWithNegativeFilterDoesNotCreateTraceTelemetryOnLoggedInfo()
        {
            bool            trackedTelemetry = false;
            TelemetryClient client           = CommonMocks.MockTelemetryClient((t) => trackedTelemetry = true);

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(false); }, defaultOptions);

            logger.LogInformation(0, "This is an information");

            Assert.False(trackedTelemetry);
        }
Пример #9
0
        public void TestLoggerWithNegativeFilterDoesNotCreateExceptionTelemetryOnLoggedError()
        {
            bool            trackedTelemetry = false;
            TelemetryClient client           = CommonMocks.MockTelemetryClient((t) => trackedTelemetry = true);

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(false); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);

            Assert.False(trackedTelemetry);
        }
Пример #10
0
        public void TestLoggerSetsSdkVersionOnLoggedTelemetry()
        {
            bool            isCorrectVersion = false;
            TelemetryClient client           = CommonMocks.MockTelemetryClient((t) =>
            {
                isCorrectVersion = t.Context.GetInternalContext().SdkVersion.StartsWith(SdkVersionUtils.VersionPrefix);
            });

            ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return(true); }, null);

            logger.LogTrace("This is a test.", new object[] { });
            Assert.True(isCorrectVersion);
        }
Пример #11
0
        public void SdkVersionIsPopulatedByMiddleware()
        {
            var middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true);

            middleware.OnHostingException(null, null);

            Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
            Assert.Contains(SdkVersionTestUtils.VersionPrefix, sentTelemetry.Context.GetInternalContext().SdkVersion);
        }
        private HostingDiagnosticListener CreateHostingListener(bool aspNetCore2)
        {
            var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)), 
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: false,
                enableNewDiagnosticEvents: aspNetCore2);
            hostingListener.OnSubscribe();

            return hostingListener;
        }
Пример #13
0
        private HostingDiagnosticListener CreateHostingListener(AspNetCoreMajorVersion aspNetCoreMajorVersion)
        {
            var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => {}),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: false,
                aspNetCoreMajorVersion: aspNetCoreMajorVersion);

            hostingListener.OnSubscribe();

            return(hostingListener);
        }
Пример #14
0
        public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
        {
            var middleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true);

            middleware.OnHostingException(null, null);

            Assert.NotNull(sentTelemetry);
            Assert.IsType <ExceptionTelemetry>(sentTelemetry);
            Assert.Equal(ExceptionHandledAt.Platform, ((ExceptionTelemetry)sentTelemetry).HandledAt);
        }
        public void OnBeginRequestWithW3CSupportAndNoHeadersIsTrackedCorrectly(bool isAspNetCore2)
        {
            var configuration = TelemetryConfiguration.CreateDefault();
            configuration.TelemetryInitializers.Add(new W3COperationCorrelationTelemetryInitializer());
            using (var hostingListener = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry), configuration),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: true,
                enableW3CHeaders: true,
                enableNewDiagnosticEvents: isAspNetCore2))
            {
                hostingListener.OnSubscribe();

                var context = CreateContext(HttpRequestScheme, HttpRequestHost, "/Test", method: "POST");

                HandleRequestBegin(hostingListener, context, 0, isAspNetCore2);

                var activityInitializedByW3CHeader = Activity.Current;

                if (!isAspNetCore2)
                { 
                Assert.Null(activityInitializedByW3CHeader.ParentId);
                    }
                Assert.NotNull(activityInitializedByW3CHeader.GetTraceId());
                Assert.Equal(32, activityInitializedByW3CHeader.GetTraceId().Length);
                Assert.Equal(16, activityInitializedByW3CHeader.GetSpanId().Length);
                Assert.Equal(
                    $"00-{activityInitializedByW3CHeader.GetTraceId()}-{activityInitializedByW3CHeader.GetSpanId()}-02",
                    activityInitializedByW3CHeader.GetTraceparent());
                Assert.Null(activityInitializedByW3CHeader.GetTracestate());
                Assert.Empty(activityInitializedByW3CHeader.Baggage);

                HandleRequestEnd(hostingListener, context, 0, isAspNetCore2);

                Assert.Single(sentTelemetry);
                var requestTelemetry = (RequestTelemetry) this.sentTelemetry.Single();

                Assert.Equal(
                    $"|{activityInitializedByW3CHeader.GetTraceId()}.{activityInitializedByW3CHeader.GetSpanId()}.",
                    requestTelemetry.Id);
                Assert.Equal(activityInitializedByW3CHeader.GetTraceId(), requestTelemetry.Context.Operation.Id);
                Assert.Null(requestTelemetry.Context.Operation.ParentId);

                Assert.True(context.Response.Headers.TryGetValue(RequestResponseHeaders.RequestContextHeader,
                    out var appId));
                Assert.Equal($"appId={CommonMocks.TestApplicationId}", appId);
            }
        }
Пример #16
0
        public void TestLoggerCreatesTraceTelemetryOnLoggedInfo()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is an information", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Information, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);
            });

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, null);

            logger.LogInformation(0, "This is an information");
        }
        public async Task RequestWillBeMarkedAsFailedForRunawayException()
        {
            var context = new DefaultHttpContext();

            context.Request.Scheme = HttpRequestScheme;
            context.Request.Host   = this.httpRequestHost;

            var requestMiddleware = new RequestTrackingMiddleware(
                httpContext => { throw new InvalidOperationException(); },
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry));

            await Assert.ThrowsAnyAsync <InvalidOperationException>(async() => { await requestMiddleware.Invoke(context, new RequestTelemetry()); });

            Assert.False(((RequestTelemetry)this.sentTelemetry).Success);
        }
        public void ExceptionsAreNotTrackedInjectedWhenDisabled()
        {
            HttpContext context = CreateContext(HttpRequestScheme, HttpRequestHost);

            var noExceptionsMiddleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: true,
                trackExceptions: false);

            noExceptionsMiddleware.OnHostingException(context, new Exception("HostingException"));
            noExceptionsMiddleware.OnDiagnosticsHandledException(context, new Exception("DiagnosticsHandledException"));
            noExceptionsMiddleware.OnDiagnosticsUnhandledException(context, new Exception("UnhandledException"));

            Assert.Empty(sentTelemetry);
        }
        public void TestLoggerCreatesExceptionTelemetryOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
            });

            ILogger logger    = new ApplicationInsightsLogger("test", client, (s, l) => { return(true); });
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);
        }
Пример #20
0
        public void TestLoggerCreatesTraceTelemetryWithoutEventIdOnLoggedDebug()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is an information", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Verbose, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);

                Assert.False(traceTelemetry.Properties.ContainsKey("EventId"));
                Assert.False(traceTelemetry.Properties.ContainsKey("EventName"));
            });

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, null);

            logger.LogDebug(new EventId(22, "TestEvent"), "This is an information");
        }
Пример #21
0
        public void InvokeTracksExceptionThrownByNextMiddlewareAsHandledByPlatform()
        {
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: false,
                       enableNewDiagnosticEvents: true))
            {
                hostingListener.OnSubscribe();
                hostingListener.OnHostingException(null, null);

                Assert.NotNull(sentTelemetry);
                Assert.IsType <ExceptionTelemetry>(sentTelemetry);
                Assert.Equal(ExceptionHandledAt.Platform, ((ExceptionTelemetry)sentTelemetry).HandledAt);
            }
        }
        public void ResponseHeadersAreNotInjectedWhenDisabled()
        {
            HttpContext context = CreateContext(HttpRequestScheme, HttpRequestHost);

            var noHeadersMiddleware = new HostingDiagnosticListener(
                CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry.Enqueue(telemetry)),
                CommonMocks.GetMockApplicationIdProvider(),
                injectResponseHeaders: false,
                trackExceptions: true);

            noHeadersMiddleware.OnBeginRequest(context, 0);
            Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));
            noHeadersMiddleware.OnEndRequest(context, 0);
            Assert.False(context.Response.Headers.ContainsKey(RequestResponseHeaders.RequestContextHeader));

            Assert.Single(sentTelemetry);
            Assert.IsType <RequestTelemetry>(this.sentTelemetry.First());
        }
Пример #23
0
        public void TestLoggerCreatesTraceTelemetryOnLoggedWarningWithParameters()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is 123 value: Hello, World!", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Warning, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);

                Assert.Equal("123", traceTelemetry.Properties["testParam"]);
                Assert.Equal("Hello, World!", traceTelemetry.Properties["param2"]);
            });

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, null);

            logger.LogWarning(0, "This is {testParam} value: {param2}", 123, "Hello, World!");
        }
Пример #24
0
        public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWhenTrackExceptionsAsExceptionTelemetryIsSetToTrue()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;

                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
            });

            ILogger logger = new ApplicationInsightsLogger("test", client, (s, l) => { return(true); }, new ApplicationInsightsLoggerOptions {
                TrackExceptionsAsExceptionTelemetry = true
            });
            var exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: " + exception.Message);
        }
Пример #25
0
        public void SdkVersionIsPopulatedByMiddleware()
        {
            using (var hostingListener = new HostingDiagnosticListener(
                       CommonMocks.MockTelemetryClient(telemetry => this.sentTelemetry = telemetry),
                       CommonMocks.GetMockApplicationIdProvider(),
                       injectResponseHeaders: true,
                       trackExceptions: true,
                       enableW3CHeaders: false,
                       enableNewDiagnosticEvents: true))
            {
                hostingListener.OnSubscribe();
                hostingListener.OnHostingException(null, null);

                Assert.NotEmpty(sentTelemetry.Context.GetInternalContext().SdkVersion);
                Assert.Contains(SdkVersionTestUtils.VersionPrefix,
                                sentTelemetry.Context.GetInternalContext().SdkVersion);
            }
        }
Пример #26
0
        public void TestLoggerCreatesExceptionWithoutEventIdTelemetryOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error", exceptionTelemetry.Message);

                Assert.False(exceptionTelemetry.Properties.ContainsKey("EventId"));
                Assert.False(exceptionTelemetry.Properties.ContainsKey("EventName"));
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(new EventId(22, "TestEvent"), exception, "Error: " + exception.Message);
        }
Пример #27
0
        public void TestLoggerCreatesExceptionTelemetryOnLoggedErrorWithParameters()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error, Value: 123", exceptionTelemetry.Message);

                Assert.Equal("This is an error", exceptionTelemetry.Properties["ex"]);
                Assert.Equal("123", exceptionTelemetry.Properties["value"]);
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            logger.LogError(0, exception, "Error: {ex}, Value: {value}", exception.Message, 123);
        }
Пример #28
0
        public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithEventIdOnLoggedError()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is an error", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Error, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);

                Assert.Equal("22", traceTelemetry.Properties["EventId"]);
                Assert.Equal("TestEvent", traceTelemetry.Properties["EventName"]);
            });
            var options = new ApplicationInsightsLoggerOptions {
                IncludeEventId = true
            };

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, options);

            logger.LogError(new EventId(22, "TestEvent"), "This is an error");
        }
Пример #29
0
        public void TestLoggerIncludingEventIdCreatesTraceTelemetryWithoutEventNameOnLoggedInfo()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <TraceTelemetry>(t);
                var traceTelemetry = (TraceTelemetry)t;
                Assert.Equal("This is an information", traceTelemetry.Message);
                Assert.Equal(SeverityLevel.Information, traceTelemetry.SeverityLevel);
                Assert.Equal("test-category", traceTelemetry.Properties["CategoryName"]);

                Assert.Equal("100", traceTelemetry.Properties["EventId"]);
                Assert.False(traceTelemetry.Properties.ContainsKey("EventName"));
            });
            var options = new ApplicationInsightsLoggerOptions {
                IncludeEventId = true
            };

            ILogger logger = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, options);

            logger.LogInformation(new EventId(100, string.Empty), "This is an information");
        }
Пример #30
0
        public void TestLoggerAdditionalExceptionDataIsAddedToTelemetry()
        {
            TelemetryClient client = CommonMocks.MockTelemetryClient((t) =>
            {
                Assert.IsType <ExceptionTelemetry>(t);
                var exceptionTelemetry = (ExceptionTelemetry)t;
                Assert.Equal(SeverityLevel.Error, exceptionTelemetry.SeverityLevel);
                Assert.Equal("test-category", exceptionTelemetry.Properties["CategoryName"]);
                Assert.Equal("System.Exception: This is an error", exceptionTelemetry.Properties["Exception"]);
                Assert.Equal("Error: This is an error, Value: 123", exceptionTelemetry.Message);

                Assert.Equal("test", exceptionTelemetry.Properties["AdditionalProperty1"]);
                Assert.Equal("test2", exceptionTelemetry.Properties["AdditionalProperty2"]);
            });

            ILogger logger    = new ApplicationInsightsLogger("test-category", client, (s, l) => { return(true); }, defaultOptions);
            var     exception = new Exception("This is an error");

            exception.Data.Add("AdditionalProperty1", "test");
            exception.Data.Add("AdditionalProperty2", "test2");
            logger.LogError(0, exception, "Error: {ex}, Value: {value}", exception.Message, 123);
        }