azure - Application Insights and failed request response codes -
when exception unhandled in our web api, response code 500 (internal server error) return client.
although application insights doesn't record 500, rather 200. successful request
false
, response code still wrong.
how can corrrect response codes in telemetry?
startup's configure
:
public void configure(iapplicationbuilder app, ihostingenvironment environment) { if (!telemetryconfiguration.active.disabletelemetry) { // add application insights beginning of request pipeline track http request telemetry. app.useapplicationinsightsrequesttelemetry(); // add application insights exceptions handling request pipeline. should // configured after error handling middleware in request pipeline. app.useapplicationinsightsexceptiontelemetry(); } app.userequirehttps(environment.islocal()); app.usemiddleware<nocachemiddleware>(); app.usejwtbearerauthentication(...); app.usecors("corspolicy"); app.usestaticfiles(); app.usecompression(); app.usemvc(); }
although application insights doesn't record 500, rather 200. successful request false, response code still wrong.
as far know, if application has no error handling middleware application insights report response status code 200 when unhandled exception thrown. find detailed info this article. using following code in method configure , correct response status code.
public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) { if (!microsoft.applicationinsights.extensibility.telemetryconfiguration.active.disabletelemetry) { app.useapplicationinsightsrequesttelemetry(); app.useapplicationinsightsexceptiontelemetry(); } loggerfactory.addconsole(configuration.getsection("logging")); loggerfactory.adddebug(); app.useiisplatformhandler(); app.useexceptionhandler(options => { options.run( async context => { context.response.statuscode = (int)httpstatuscode.internalservererror; context.response.contenttype = "text/html"; var ex = context.features.get<iexceptionhandlerfeature>(); if (ex != null) { var err = $"<h1>error: {ex.error.message}</h1>{ex.error.stacktrace }"; await context.response.writeasync(err).configureawait(false); } }); }); app.usestaticfiles(); app.usemvc(); }
Comments
Post a Comment