make TrackRequest and TrackException output some debug data
This commit is contained in:
Родитель
49a5c752f9
Коммит
9a021344b2
|
@ -7,13 +7,13 @@
|
|||
{
|
||||
public static IApplicationBuilder UseApplicationInsightsRequestTelemetry(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseMiddleware<ApplicationInsightsMiddleware>();
|
||||
app.UseMiddleware<ApplicationInsightsRequestMiddleware>();
|
||||
return app;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder UseApplicationInsightsExceptionTelemetry(this IApplicationBuilder app)
|
||||
{
|
||||
app.UseMiddleware<ApplicationInsightsMiddleware>();
|
||||
app.UseMiddleware<ApplicationInsightsExceptionMiddleware>();
|
||||
return app;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,12 +7,23 @@ namespace Microsoft.ApplicationInsights.AspNet
|
|||
using System.Collections.Generic;
|
||||
|
||||
public static class ApplicationInsightsJavaScriptSnippetExtensions
|
||||
{
|
||||
{
|
||||
public static HtmlString ApplicationInsightsJavaScriptSnippet(this IHtmlHelper helper, string instrumentationKey, Dictionary<string, string> parameters = null)
|
||||
{
|
||||
//see: https://github.com/aspnet/Mvc/issues/2056
|
||||
//var client = (TelemetryClient)helper.ViewContext.HttpContext.ApplicationServices.GetService(typeof(TelemetryClient));
|
||||
return new HtmlString("<script language='javascript'>alert('Key: ' + '" + instrumentationKey + "');</script>");
|
||||
return new HtmlString(@"<script language='javascript'>
|
||||
var appInsights = window.appInsights || function(config){
|
||||
function s(config){t[config]=function(){var i=arguments; t.queue.push(function(){ t[config].apply(t, i)})}
|
||||
}
|
||||
var t = { config:config }, r = document, f = window, e = ""script"", o = r.createElement(e), i, u;for(o.src=config.url||""//az416426.vo.msecnd.net/scripts/a/ai.0.js"",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=[""Event"",""Exception"",""Metric"",""PageView"",""Trace""];i.length;)s(""track""+i.pop());return config.disableExceptionTracking||(i=""onerror"",s(""_""+i),u=f[i],f[i]=function(config, r, f, e, o) { var s = u && u(config, r, f, e, o); return s !== !0 && t[""_"" + i](config, r, f, e, o),s}),t
|
||||
}({
|
||||
instrumentationKey:""" + instrumentationKey + @"""
|
||||
});
|
||||
|
||||
window.appInsights=appInsights;
|
||||
appInsights.trackPageView();
|
||||
</script>");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,12 +5,12 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class ApplicationInsightsMiddleware
|
||||
public class ApplicationInsightsRequestMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly TelemetryClient client;
|
||||
|
||||
public ApplicationInsightsMiddleware(RequestDelegate next, TelemetryClient client)
|
||||
public ApplicationInsightsRequestMiddleware(RequestDelegate next, TelemetryClient client)
|
||||
{
|
||||
this.next = next;
|
||||
this.client = client;
|
||||
|
@ -18,9 +18,33 @@
|
|||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
this.client.TrackRequest();
|
||||
this.client.TrackRequest(context.Request.Path.ToString());
|
||||
await this.next(context);
|
||||
}
|
||||
}
|
||||
public class ApplicationInsightsExceptionMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly TelemetryClient client;
|
||||
|
||||
public ApplicationInsightsExceptionMiddleware(RequestDelegate next, TelemetryClient client)
|
||||
{
|
||||
this.next = next;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await this.next(context);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.client.TrackException(ex.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,9 +22,14 @@
|
|||
{
|
||||
}
|
||||
|
||||
public void TrackRequest()
|
||||
public void TrackRequest(string path)
|
||||
{
|
||||
Debug.WriteLine("Track Request " + this.config.InstrumentationKey);
|
||||
Debug.WriteLine("Track Request (instrumentation key = " + (this.config.InstrumentationKey ?? "") + "): " + (path ?? ""));
|
||||
}
|
||||
|
||||
public void TrackException(string ex)
|
||||
{
|
||||
Debug.WriteLine("Track Exception (instrumentation key = " + (this.config.InstrumentationKey ?? "") + "):" + (ex ?? ""));
|
||||
}
|
||||
|
||||
private readonly TelemetryContext context;
|
||||
|
|
Загрузка…
Ссылка в новой задаче