Use injected RequestTelemetry object
This commit is contained in:
Родитель
31de55ddf6
Коммит
4d69cd678d
|
@ -34,7 +34,12 @@
|
|||
TelemetryConfiguration.Active.InstrumentationKey = config.Get("ApplicationInsights:InstrumentationKey");
|
||||
|
||||
services.AddInstance<TelemetryClient>(new TelemetryClient());
|
||||
services.AddScoped<RequestTelemetry>();
|
||||
services.AddScoped<RequestTelemetry>((svcs) => {
|
||||
var rt = new RequestTelemetry();
|
||||
// this is workaround to inject proper instrumentation key into javascript:
|
||||
rt.Context.InstrumentationKey = svcs.GetService<TelemetryClient>().Context.InstrumentationKey;
|
||||
return rt;
|
||||
});
|
||||
}
|
||||
|
||||
public static HtmlString ApplicationInsightsJavaScriptSnippet(this IHtmlHelper helper, string instrumentationKey)
|
||||
|
|
|
@ -18,10 +18,12 @@
|
|||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly TelemetryClient telemetryClient;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public ApplicationInsightsRequestMiddleware(RequestDelegate next, TelemetryClient client)
|
||||
public ApplicationInsightsRequestMiddleware(IServiceProvider svcs, RequestDelegate next, TelemetryClient client)
|
||||
{
|
||||
this.telemetryClient = client;
|
||||
this.serviceProvider = svcs;
|
||||
this.telemetryClient = client;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
|
@ -42,12 +44,12 @@
|
|||
{
|
||||
sw.Stop();
|
||||
|
||||
var telemetry = new RequestTelemetry(
|
||||
httpContext.Request.Method + " " + httpContext.Request.Path.Value,
|
||||
now,
|
||||
sw.Elapsed,
|
||||
httpContext.Response.StatusCode.ToString(),
|
||||
httpContext.Response.StatusCode < 400);
|
||||
var telemetry = this.serviceProvider.GetService<RequestTelemetry>();
|
||||
telemetry.Name = httpContext.Request.Method + " " + httpContext.Request.Path.Value;
|
||||
telemetry.Timestamp = now;
|
||||
telemetry.Duration = sw.Elapsed;
|
||||
telemetry.ResponseCode = httpContext.Response.StatusCode.ToString();
|
||||
telemetry.Success = httpContext.Response.StatusCode < 400;
|
||||
|
||||
this.telemetryClient.TrackRequest(telemetry);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
@using Microsoft.ApplicationInsights.AspNet
|
||||
@inject Microsoft.ApplicationInsights.TelemetryClient TelemetryClient
|
||||
@inject Microsoft.ApplicationInsights.DataContracts.RequestTelemetry RequestTelemetry
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
@ -10,7 +10,7 @@
|
|||
<link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css" />
|
||||
<link rel="stylesheet" href="~/css/site.css" />
|
||||
<link rel="stylesheet" href="~/lib/bootstrap-touch-carousel/css/bootstrap-touch-carousel.css" />
|
||||
@Html.ApplicationInsightsJavaScriptSnippet(TelemetryClient.Context.InstrumentationKey);
|
||||
@Html.ApplicationInsightsJavaScriptSnippet(RequestTelemetry.Context.InstrumentationKey);
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
|
|
Загрузка…
Ссылка в новой задаче