如果异常传递给 LogError (或其他 Log* 方法),Application Insights 不会记录日志,但如果异常未传递,则记录日志

问题描述 投票:0回答:1

我有一个托管在 Azure 应用服务中的 .NET 8 Web API。

我遇到了一个问题,由于数据库逻辑错误,我收到了 500 响应,但门户的 App Insights 部分的异常日志中没有任何条目。

我在控制器代码中添加了一些测试日志记录行,以显式捕获异常并尝试手动记录日志,结果发现,如果我将异常作为第一个参数传递给 Log* 方法,则不会记录任何内容。如果我不传递异常,而只是传递一条消息,它就会记录。

我正在使用最新版本(2.22.0)上的

Microsoft.ApplicationInsights.AspNetCore
包。

我的配置:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  }
}

在我的启动文件中,我正在调用

services.AddApplicationInsightsTelemetry();

我的控制器代码来测试这个是:

try
{
    var updatedCase = await ...; // DB update logic that may throw ConcurrencyException

    return Ok(...);
}
catch (ConcurrencyException cEx)
{
    logger.LogError(cEx, "DEBUG ERROR: CAUGHT CONCURRENCY EXCEPTION"); // NOT logged
    logger.LogInformation(cEx, "DEBUG INFO: CAUGHT CONCURRENCY EXCEPTION"); // NOT logged
    logger.LogInformation("DEBUG INFO: CAUGHT CONCURRENCY EXCEPTION (NO EX)"); // LOGGED under traces in App Insights
    throw; // The rethrown exception is NOT logged
}

我不确定它是否有帮助,但自定义

ConcurrencyException
看起来像这样 - 我需要用它做其他事情才能使其可记录吗?:

public class ConcurrencyException : Exception
{
    public ConcurrencyException() { }

    public ConcurrencyException(string message)
        : base(message) { }

    public ConcurrencyException(string message, Exception inner)
        : base(message, inner) { }
}

抛出它的库代码如下所示:

catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.Conflict || ex.Headers["x-ms-substatus"] == "409" || ex.SubStatusCode == 409)
{
    throw new ConcurrencyException($"Concurrency conflict when appending to stream {streamId}. Expected revision {firstEventNumber - 1}", ex);
}

如果我不捕获/处理异常,我会收到 500 响应,但 AppInsights 中不会记录任何内容。

奇怪的是,如果我捕获

ConcurrencyException
并尝试记录它,它不会出现在 AppInsights 中。如果我捕获它并抛出一个新的异常,那么该新的异常就会被记录,例如

try
{
    ...
}
catch (ConcurrencyException cEx)
{
    ...
    throw new Exception("DEBUG: A NEW EXCEPTION"); // Note that if I do this, then it DOES get logged!
}

如何让 App Insights 记录这些异常?

c# asp.net-core .net-core azure-application-insights
1个回答
0
投票

无论是否传递 EX,我都可以将错误记录为异常。

 catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.Conflict || ex.Headers["x-ms-substatus"] == "409" || ex.SubStatusCode == 409)
 {
     _logger.LogError("DEBUG: A NEW EXCEPTION without Ex");//Logged as Trace
     _logger.LogError(ex, "DEBUG ERROR: Error EXCEPTION with ex");//Logged as Exception with exception message    
 }
  • 在交易搜索中您只能看到消息。

enter image description here

  • 点击
    Exception
    =>
    End-to-end
    交易详情

您可以看到异常的严重级别。

EX 错误:

enter image description here

EX信息: enter image description here

我的

Program.cs
文件:

builder.Services.AddApplicationInsightsTelemetry(new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
    ConnectionString = builder.Configuration.GetSection("ApplicationInsights")["ConnectionString"]
});
builder.Logging.SetMinimumLevel(LogLevel.Information);

appsettings.json

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
       "ConnectionString": "InstrumentationKey=****;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/"

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.