对 azure webjob .Net Core 2.0 的应用洞察

Application Insights to azure webjob .Net Core 2.0

如何将应用洞察遥测(Application Insights)添加到azure webjob?

您可以在开发期间将 AI 作为 Nuget 包添加到 Web 作业中。

AI .NET Core Nuget 是 here。包名称有点误导 (Microsoft.ApplicationInsights.AspNetCore) 但它应该适用于所有 .Net 核心应用程序。

AI .NET Core GitHub 页面是 here(在 Wiki 中解释了一些自定义选项)。

入门指南也是 on GitHub and docs.microsoft.com。这是一个有点冗长的指南,所以我希望链接没问题(虽然不完全符合 SO 指南)并且我不需要 post 它作为回复的一部分。

最近发布的 WebJob SDK 3.0,您可以在 ConfigureLogging 方法中添加 ApplicationInsights

public static async Task Main(string[] args)
{
     var builder = new HostBuilder()
        .ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices().AddAzureStorage();
        })
        .ConfigureAppConfiguration(b =>
        {
            // Adding command line as a configuration source
            b.AddCommandLine(args);
        })
        .ConfigureLogging((context, b) =>
        {
            b.SetMinimumLevel(LogLevel.Debug);
            b.AddConsole();

            // If this key exists in any config, use it to enable App Insights
            string appInsightsKey = context.Configuration["ApplicationInsights:InstrumentationKey"];
            if (!string.IsNullOrEmpty(appInsightsKey))
            {
                b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
            }
        });

     var host = builder.Build();
     using (host)
     {
         await host.RunAsync();
     }
}