azureazure-application-insightsazure-webjobsazure-webjobssdk

Application Insights not logging from console application


I'm trying to understand how logging to application insights works for simple logging from a console application. I have created a console app as follows, hosted it in Azure as a webjob and it runs. I can see from the logs that it's writing to the console but I can't see where to look in Application Insights to see the console logs. I want to be able to create an alert if there is an error.

Class LoggingEngine:[Logging Engine Class]

enter image description here

Program.cs: enter image description here

In Azure App Services | WebJobs: enter image description here

Clicked on the highlighted logs icon and get this: [Web Job Logs]

enter image description here

But when I go into Monitoring | Logs and run any of the queries they all say No Results.

Firstly, have I got my code correct? Secondly, I'm using the F1 (Free) App Service Plan, does that matter? Thirdly, Can you help me with a query for Application Insights to look at Console Logs please? Fourthly, Do I need to enable Profiler to be able to see console writes?

Any help \ suggestions would be greatly received, Thanks,

I have tried various versions of code that I've found regarding this but nothing seems to be logging. I don't know where I'm going wrong.


Solution

  • I am able to log Traces to Application Insights which are seen in Azure Web Job.

    Web Job Logs: enter image description here

    Application Insights Transaction Search:

    enter image description here

    Log Traces: enter image description here

    I have referred this MSDoc to configure ApplicationInsights in Console App.

    My Program.cs file:

    using System.Threading.Tasks;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    namespace ConsoleApp2
    {
        class Program
        {
            static async Task Main()
            {
                var builder = new HostBuilder();            
                builder.ConfigureLogging((context, b) =>
                {
                    b.AddConsole();   
                    //b.AddFilter("Microsoft.Azure.WebJobs.Hosting", LogLevel.None);
                    //b.SetMinimumLevel(LogLevel.Information);
                    string ConnString = "InstrumentationKey=****;IngestionEndpoint=https://****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/;ApplicationId=****";
                    if (!string.IsNullOrEmpty(ConnString))
                    {
                        b.AddApplicationInsightsWebJobs(o => o.ConnectionString = ConnString);
                    }
                });
                builder.ConfigureWebJobs(b =>
                {
                    b.AddAzureStorageQueues();
                });
                var host = builder.Build();
                using (host)
                {
                    var logger = host.Services.GetRequiredService<ILogger<Program>>();
                    logger.LogInformation("Log Information from Progarm.cs..");
                    logger.LogWarning("Warning Message");
                    logger.LogDebug("Debug Message");
                    logger.LogInformation("Application stopped.");
    
                    await host.RunAsync();
    
                }
            }
        }
    }
    
     b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = instrumentationKey);
    

    enter image description here

    Do I need to enable Profiler to be able to see console writes?

    enter image description here