3

The following code work on my machine locally. That means all log statements are in app insights after some minutes...

When I deploy the application with Publish as Webjob all gets deployed BUT there are no log statement of the triggered webjob although I can see it runs every 5 seconds...

I also tried Thread.Sleep(30000); // 30 seconds...

Why does app insights work locally but not hosted on azure?

Console application .NET 4.7.2:

  // Create the DI container.
                IServiceCollection services = new ServiceCollection();
    
                var appInsightsKey = ConfigurationManager.AppSettings.Get("APPINSIGHTS_INSTRUMENTATIONKEY");
                Console.WriteLine($"appInsightsKey: {appInsightsKey}");
    
                // Being a regular console app, there is no appsettings.json or configuration providers enabled by default.
                // Hence instrumentation key and any changes to default logging level must be specified here.
                services.AddLogging(loggingBuilder =>
                {
                    loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
                    loggingBuilder.AddApplicationInsights(appInsightsKey);
                }
                );
    
                services.AddApplicationInsightsTelemetryWorkerService(appInsightsKey);
                IServiceProvider serviceProvider = services.BuildServiceProvider();
                // Obtain logger instance from DI.
                ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
    
                logger.LogWarning("nogger 2: warning");
                logger.LogInformation("nogger2: information");
                logger.LogError("nogger2: error");
    
                Console.WriteLine("This is the end...");
    
                Task.Delay(5000).Wait();

UPDATE

I am using Microsoft.ApplicationInsights.WorkerService nuget:

https://learn.microsoft.com/de-de/azure/azure-monitor/app/worker-service

scroll down for .NET framework console apps!

I have noticed something interesting (the different display of the references...) but maybe it does not make any difference in the end because with both projects the ILogging in azure does not work:

4.7.2 project:

xx

4.6.1 project:

enter image description here

I have also just deployed as Azure Webjob vom Visual Studio with debug mode and still it does not work and I even configured appinsights extra in that deploy dialog but no difference!

HelloWorld
  • 4,671
  • 12
  • 46
  • 78
  • Hi, did you check the configuration on your App Service? Is AppInsights enabled and the connection string properly set? – Facundo La Rocca Jul 16 '20 at 17:22
  • As stated in the code I read the appInsightsKey successfully its in the Console log... App Insights must be enabled because it worked from local place... but indeed it is enabled! – HelloWorld Jul 16 '20 at 17:25
  • Could it be because locally is no ApplicationInsights.config created? Maybe azure code needs it... I do not remember which process is creating that file maybe when I added any appinsights nuget... – HelloWorld Jul 17 '20 at 08:34
  • @HelloWorld which sdk are you using? – Ivan Glasenberg Jul 17 '20 at 08:55
  • 1
    see my UPDATE answer! :-) – HelloWorld Jul 17 '20 at 09:35
  • at the moment i use the workaround to use the: var telemetryClient = serviceProvider.GetRequiredService(); that instance works on azure logging to appInsights. – HelloWorld Jul 17 '20 at 10:09
  • Same issue for me. Did you figure out the solution and where the issue is? – D0mm Dec 02 '21 at 09:06

1 Answers1

0

Have you tried converting it into a continuous web job...? I had similar issues that got resolved upon switching from triggered to continuous web job...

Gabba
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '22 at 00:09
  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch Aug 31 '22 at 10:47