2016-10-24 2 views

Antwort

0

Ein Ansatz wäre, benutzerdefinierte Middleware zu erstellen, die den Fehler abfangen und die Ausnahme an AppInsights senden würde.

using System; 
using System.Threading.Tasks; 
using Microsoft.ApplicationInsights; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Http; 

namespace Middleware 
{ 
    public static class ApplicationBuilderExtensions 
    { 
     public static IApplicationBuilder UseHttpException(this IApplicationBuilder application) 
     { 
      return application.UseMiddleware<HttpExceptionMiddleware>(); 
     } 
    } 

    public class HttpExceptionMiddleware 
    { 
     private readonly RequestDelegate _next; 

     public HttpExceptionMiddleware(RequestDelegate next) 
     { 
      _next = next; 
     } 

     public async Task Invoke(HttpContext context) 
     { 
      try 
      { 
       await _next.Invoke(context); 
      } 
      catch (Exception ex) 
      { 
       var telemetryClient = new TelemetryClient(); 
       telemetryClient.TrackException(ex); 

       //handle response codes and other operations here 
      } 
     } 
    } 
} 

Dann registrieren, die Middleware im Configure-Methode des Startup:

app.UseHttpException();

+0

u sicher, das funktioniert? Für mich gibt es immer noch keine Ausnahmen. müssen Sie es zuerst in die Pipeline oder nach benutzerdefinierten Fehlern oder? –

Verwandte Themen