0
votes

I am using webjobs v3 with HostBuilder.

How do I have to configure the logger that I can use a telemetryClient to tracktrace data which is visible in the console output AND visible in the azure app service this webjob is running on?

I would like to get a ILogger instance which logs to console AND azure.

this line of code:

 var logger = host.Services.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("I log only in the console but not azure.");

DOES not send its data to azure application insights charts...

I do NOT want to use the ILogger AND the telemetryclient to log stuff that would be stupid.

var builder = new HostBuilder()
                .UseEnvironment("Development")
                .ConfigureWebJobs(b =>
                {
                    // Add extensions and other WebJobs services
                })
                .ConfigureAppConfiguration(b =>
                {

                    // Add configuration sources          
                })
                .ConfigureLogging((context, b) =>
                {
                    // Add Logging Providers
                    b.AddConsole(); 


                    // If this key exists in any config, use it to enable App Insights
                    string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]; // taken from the appservice environmentvariable

                    appInsightsKey = "xxxxxxxx";

                    if (!string.IsNullOrEmpty(appInsightsKey))
                    {
                        // This uses the options callback to explicitly set the instrumentation key.
                        b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                    }
                })
                .UseConsoleLifetime();

            var host = builder.Build();

            // my code like telemetryClient.TrackTrace("show this text in console output AND azure app service where this webjob belongs to")

            using (host)
            {
                host.Run();
            }
1
what do you mean "I do NOT want to use the ILogger AND the telemetryclient to log stuff"?Ivan Yang
I do not want to use TWO api`s to log parallel in 2 places when running my code locally for DEV environment.HelloWorld
ok, you want to use ILogger to log the message to console and application insights in azure portal, right?Ivan Yang

1 Answers

0
votes

Please correct me if I misunderstand you.

For "I would like to get a ILogger instance which logs to console AND azure", I'm using the code below:

        static void Main(string[] args)
        {
            var builder = new HostBuilder()
                .ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageCoreServices();
                    b.AddAzureStorage();
                })
                .ConfigureAppConfiguration(b =>
                {

                }
                )
                .ConfigureLogging((context, b) =>
                {

                    b.AddConsole();
                    string instrumentationKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                    if (!string.IsNullOrEmpty(instrumentationKey))
                    {
                        b.AddApplicationInsights(o => o.InstrumentationKey = instrumentationKey);
                    }
                })                
                .UseConsoleLifetime();

            var host = builder.Build();

            var logger = host.Services.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("hello 111111111111111");

            using (host)
            {
                host.Run();
            }
        }

After running the code, the message is logged in console and app insights in azure portal:

enter image description here