public async void GivenRequest_WhenRequestIdPresented_ThenRequestTracedWithRequestId() { // Arrange var telemetryChannel = new TelemetryChannelMock(); var builder = new WebHostBuilder() .Configure(app => { app.Use(async(context, next) => { var telemetryClient = app.ApplicationServices.GetService <TelemetryClient>(); using (telemetryClient.StartOperation <RequestTelemetry>(Activity.Current)) { await next.Invoke(); } }); app.UseWebApiAppInsightsLogging(options => { options.OperationIdHeaderName = DefaultHeaderName; }); }) .ConfigureServices(services => { services.AddSingleton <ITelemetryChannel>(telemetryChannel); services.AddWebApiApplicationInsights(options => options.InstrumentationKey = DefaultInstrumentationKey); }); var server = new TestServer(builder); var operationId = Guid.NewGuid().ToString(); var request = new HttpRequestMessage(); request.Headers.Add(DefaultHeaderName, operationId); // Act await server.CreateClient().SendAsync(request); // Assert var requests = telemetryChannel.RequestTelemetries.ToList(); // some of requests are default request for server, use only last one var telemetry = requests.LastOrDefault(); Assert.NotNull(telemetry); Assert.Equal(operationId, telemetry.Context.Operation.Id); Assert.Equal(operationId, telemetry.Context.Operation.ParentId); }
public async Task InvokeLogHandler_Test() { var fakeChannel = new TelemetryChannelMock(); var config = new TelemetryConfiguration { TelemetryChannel = fakeChannel, InstrumentationKey = string.Empty, }; var client = new TelemetryClient(config); var httpContext = new DefaultHttpContext().Request.HttpContext; httpContext.Request.Method = HttpMethods.Post; var logMiddleware = new LogMiddleware(async(innerHttpContext) => { await innerHttpContext.Response.WriteAsync("Response body mock"); }, client); await logMiddleware.Invoke(httpContext, _identityServiceMock.Object); Assert.NotNull(logMiddleware); }
public async void GivenRequest_WhenTelemetryNotEnabled_ThenTelemetryNotFound() { // Arrange var telemetryChannel = new TelemetryChannelMock(); var builder = new WebHostBuilder() .Configure(app => app.UseWebApiAppInsightsLogging(options => { options.OperationIdHeaderName = DefaultHeaderName; })) .ConfigureServices(services => { services.AddSingleton <ITelemetryChannel>(telemetryChannel); }); var server = new TestServer(builder); // Act await server.CreateClient().GetAsync(string.Empty); // Assert Assert.Empty(telemetryChannel.RequestTelemetries); }