Application Insights 和失败的请求响应代码
Application Insights and failed request response codes
当我们的网络 API 中未处理异常时,响应代码 500(内部服务器错误)将 return 发送给客户端。
虽然 Application Insights 没有将此记录为 500,而是记录为 200。Successful request
是 false
,但响应代码仍然错误。
如何在我的遥测中获得正确的响应代码?
初创公司的 Configure
:
public void Configure(IApplicationBuilder app, IHostingEnvironment environment)
{
if (!TelemetryConfiguration.Active.DisableTelemetry)
{
// Add Application Insights to the beginning of the request pipeline to track HTTP request telemetry.
app.UseApplicationInsightsRequestTelemetry();
// Add Application Insights exceptions handling to the request pipeline. Should be
// configured after all error handling middleware in the 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 this as a 500, but rather a 200. Successful request is false, but the response code is still wrong.
据我所知,如果应用程序没有错误处理中间件,Application Insights 将在抛出未处理的异常时报告响应状态代码 200。我们可以从 this article 中找到详细信息。我在 Configure 方法中使用了以下代码,我可以获得正确的响应状态代码。
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();
}
ApplicationInsights 跟踪交互但不会报告错误,除非客户端将错误记录为自定义事件或服务器添加了中间件代码(根据@fei-han)
如果您需要 Analytics 警报,那么我使用的 Azure Data Lake 查询是:
let sitename = "localhost:26047/api";
requests
| where timestamp > ago(1d)
| where url contains sitename
| where resultCode contains "40"
| order by timestamp desc
对于警报,您可以从 Analytics blade 创建一个新的 'Rule',每月费用约为 1.50 美元。
当我们的网络 API 中未处理异常时,响应代码 500(内部服务器错误)将 return 发送给客户端。
虽然 Application Insights 没有将此记录为 500,而是记录为 200。Successful request
是 false
,但响应代码仍然错误。
如何在我的遥测中获得正确的响应代码?
初创公司的 Configure
:
public void Configure(IApplicationBuilder app, IHostingEnvironment environment)
{
if (!TelemetryConfiguration.Active.DisableTelemetry)
{
// Add Application Insights to the beginning of the request pipeline to track HTTP request telemetry.
app.UseApplicationInsightsRequestTelemetry();
// Add Application Insights exceptions handling to the request pipeline. Should be
// configured after all error handling middleware in the 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 this as a 500, but rather a 200. Successful request is false, but the response code is still wrong.
据我所知,如果应用程序没有错误处理中间件,Application Insights 将在抛出未处理的异常时报告响应状态代码 200。我们可以从 this article 中找到详细信息。我在 Configure 方法中使用了以下代码,我可以获得正确的响应状态代码。
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();
}
ApplicationInsights 跟踪交互但不会报告错误,除非客户端将错误记录为自定义事件或服务器添加了中间件代码(根据@fei-han)
如果您需要 Analytics 警报,那么我使用的 Azure Data Lake 查询是:
let sitename = "localhost:26047/api";
requests
| where timestamp > ago(1d)
| where url contains sitename
| where resultCode contains "40"
| order by timestamp desc
对于警报,您可以从 Analytics blade 创建一个新的 'Rule',每月费用约为 1.50 美元。