Application Insights to azure webjob .Net Core 2.0

余生颓废 提交于 2019-12-07 06:10:40

问题


How to add application insights telemetry (Application Insights) to azure webjob ?


回答1:


With recently released WebJob SDK 3.0, you can add ApplicationInsights in the ConfigureLogging method

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();
     }
}



回答2:


You can add AI to Web Jobs during the development time as a Nuget Package.

AI .NET Core Nuget is here. Package name is a bit misleading (Microsoft.ApplicationInsights.AspNetCore) but it's supposed to work with all .Net core apps.

AI .NET Core GitHub page is here (with some customization options explained in Wiki).

Getting started guide is on GitHub and docs.microsoft.com as well. It's a bit lengthy guide, so I hope links are OK (although not in full accordance with SO guidelines) and I won't need to post it as part of the reply.



来源:https://stackoverflow.com/questions/52424215/application-insights-to-azure-webjob-net-core-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!