【问题标题】:.Net core log Startup errors to app insights.Net 核心日志启动错误到应用洞察
【发布时间】:2022-10-16 00:55:10
【问题描述】:

我正在尝试记录任何启动错误,但日志没有流入应用程序洞察力。我尝试在 program.cs 中配置应用洞察,但仍然没有看到任何日志。

public static IHostBuilder CreateHostBuilder(string[] args) =>

    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddApplicationInsights();
            logging.SetMinimumLevel(LogLevel.Information);
        });

【问题讨论】:

  • 你期待什么日志你能提供一些例子吗?
  • 我正在尝试查看应用程序启动失败的原因,main 方法或 startup.cs 中的任何异常

标签: asp.net-core .net-core azure-web-app-service asp.net-core-webapi azure-application-insights


【解决方案1】:

要从 program.cs 或 startup.cs 中捕获日志,您需要像这样进行配置。

.ConfigureLogging((context, builder) =>
 {
   // Providing a connection string is required if need to capture logs during application startup, such as
   // in Program.cs or Startup.cs itself.
   builder.AddApplicationInsights(
       configureTelemetryConfiguration: (config) => config.ConnectionString = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
       configureApplicationInsightsLoggerOptions: (options) => { }
      );

   // Capture all log-level entries from Program
   builder.AddFilter<ApplicationInsightsLoggerProvider>(
                        typeof(Program).FullName, LogLevel.Trace);

  // Capture all log-level entries from Startup
  builder.AddFilter<ApplicationInsightsLoggerProvider>(
                        typeof(Startup).FullName, LogLevel.Trace);
 });

【讨论】:

  • 这对我不起作用。我在 Startup 的 Configure 方法中抛出了一个测试异常,但在我的 App Insights 中没有找到任何日志。任何线索它可能是什么?我正在使用.NET 6。
【解决方案2】:

对于那些使用具有最小托管模型的 .NET 6 项目的人来说,这有助于我将启动/关闭消息捕获到 Application Insights 中:

// Program.cs

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApplicationInsightsTelemetry();
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft.Hosting.Lifetime", LogLevel.Information);

...

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多