.NET Core 3.0 worker - 无法使用 Application Insights 或 EventLog 进行日志记录

.NET Core 3.0 worker - can't make logging work with Application Insights nor EventLog

我需要 .NET 3.0 辅助角色服务来登录 Azure Application Insights 和 EventLog。 None 个作品(几乎)!

这是我的 CreateHostBuilder:

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    IConfiguration configuration = hostContext.Configuration;

                    WatchdogOptions options = configuration.GetSection("WorkerOptions").Get<WatchdogOptions>();
                    if (options == null) throw new WatchdogException("WorkerOptions settings are not set");

                    services.AddSingleton(options);

                    services.AddHostedService<Worker>()
                    // .AddApplicationInsightsTelemetryWorkerService();
                    ;
                })
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    //logging.AddConsole();
                    logging.AddApplicationInsights("<instr-key>");
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",
                        LogName = "Watchdog"
                    });
                });
        }

1) 无论我做什么 EventLog 都没有来自我的工作人员的任何记录。我确实在应用程序设置中设置了日志记录级别:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "EventLog": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

2) Application Insights 仅在 .AddApplicationInsightsTelemetryWorkerService() 被注释掉时获取记录 并且 检测密钥被硬编码在 logging.AddApplicationInsights("8d3bc77d-1cc3-4c4a-83e4-6d8aaa87f8f7")。应该怎么做才能从应用程序设置中获取密钥?

3) 为什么这么麻烦?

更新

这是完整的app.development.settings

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-what-ever-0000000000"
  }
}

更新 2

ApplicationInsights 添加到 Logging 部分:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    } 
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

更新 3

属性 名称更改为 Logging:ApplicationInsights:LogLevel:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "EventLog": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "ApplicationInsights": {
      "LogLevel": {
        "PushNotificationsWatchdog.Worker": "Information"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "8d3bc77d-1cc5-4c4a-83e4-6d8aaa87f8f7"
  }
}

同样的事情 - App Insights 中没有记录。检测密钥正确。

决议

感谢@peter-bons!

所以我更改了 ConfigureServices()ConfigureLogging() 的顺序,并使用了更新 2 中的 appsettings,现在可以使用了!所以我们开始了:

public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddEventLog(new EventLogSettings
                    {
                        SourceName = "PNWatchdog",
                        LogName = "Watchdog"
                    });
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>()
                        .AddApplicationInsightsTelemetryWorkerService();
                })
                .UseWindowsService(); // windows only feature
        }

AddApplicationInsightsTelemetryWorkerService() 是在 WorkerService 中启用应用程序洞察力的推荐方法。它可以从 appsettings.json 中获取 ikey。这会在内部设置日志记录提供程序 (applicationinsightsloggingprovider)。

ApplicationInsights 日志记录提供程序默认仅捕获 'Warning' 或更高级别 (https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#ilogger-logs) 的日志。所以这是我的第一个怀疑。你能记录一些警告或错误,看看是否被捕获。 或者更改应用程序洞察捕获的默认级别以捕获信息或跟踪级别。 (https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#control-logging-level)

直到有类似 "Application Insights Telemetry WorkerService" 的东西:-)

我使用 AddApplicationInsightsTelemetryWorkerService 让它工作。但前提是您在 ConfigureServices 之前不调用 logging.ClearProviders(); 或调用 ConfigureLogging,否则添加的日志记录提供程序将被清除。

这个有效:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<Worker>();
                    services.AddApplicationInsightsTelemetryWorkerService();
                });

使用此配置:

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

正如您在输出中看到的那样,AI 密钥已正确拾取:

Application Insights Telemetry: {
    "name": "Microsoft.ApplicationInsights.Dev.3b40adb096064da0816e7b8579aa443c.Message",
    "time": "2019-11-13T07:52:11.0027057Z",
    "iKey": "xxx",
    "tags": {
        "ai.application.ver": "1.0.0.0",
        "ai.cloud.roleInstance": "xxx",
        "ai.internal.sdkVersion": "il:2.11.0-21511",
        "ai.internal.nodeName": "xxx"
    },
    "data": {
        "baseType": "MessageData",
        "baseData": {
            "ver": 2,
            "message": "Application started. Press Ctrl+C to shut down.",
            "severityLevel": "Information",
            "properties": {
                "{OriginalFormat}": "Application started. Press Ctrl+C to shut down.",
                "CategoryName": "Microsoft.Hosting.Lifetime",
                "DeveloperMode": "true"
            }
        }
    }
}