logging.AddAzureWebAppDiagnostics() does not work in .net core 2.2
Asked Answered
B

1

7

I've updated my project from .net core 2.1 to 2.2 and then logging.AddAzureWebAppDiagnostics() in Program.cs no longer works.

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddAzureWebAppDiagnostics();
            })

            .UseStartup<Startup>()
            .Build();
}

'ILoggingBuilder' does not contain a definition for 'AddAzureWebAppDiagnostics' and no accessible extension method 'AddAzureWebAppDiagnostics' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?

Referring to this document,

If targeting .NET Framework or referencing the Microsoft.AspNetCore.App metapackage, add the provider package to the project. Invoke AddAzureWebAppDiagnostics on an ILoggerFactory instance:

So the way might be slightly different from the previous one. How do I fix this issue?

Burnett answered 28/2, 2019 at 17:0 Comment(8)
might be a silly question.. have you added the using Microsoft.Extensions.Logging; statement?Couscous
Yes I have the statement. It was working with .net 2.0 and 2.1.Burnett
also in the linked document it says, If targeting .NET Core, note the following points: Don't explicitly call AddAzureWebAppDiagnostics.Couscous
I use Microsoft.AspNetCore.App as well, that's why I referred to the statement in the document.Burnett
that talks abt invoking AddAzureWebAppDiagnostics on an ILoggerFactory instance. here you have ILoggingBuilder. And as you can see here, that method is set as Obsolete with a comment will be removed in a future version.The alternative is AddAzureWebAppDiagnostics(this ILoggingBuilder builder)Couscous
I understand. So can I just comment out the function call and use Microsoft.Extensions.Logging.AzureAppServices instead?Burnett
could you check if logs are coming without the call (comment AddAzureWebAppDiagnostics)?Couscous
Umm oddly the logs still come without the function call... How does the app output them? Anyway I think now I can just comment it out and get to 2.2. Thank you for your suggestion.Burnett
O
23

The documentation is a bit tricky but if read carefully it become clear that following steps should be undertaken (for NET Core):

  1. Microsoft.Extensions.Logging.AzureAppServices should be installed

  2. There is NO need to call logging.AddAzureWebAppDiagnostics();

  3. Logging can be configured using following code

    // file startup.cs
    using Microsoft.Extensions.Logging.AzureAppServices;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //...
            services.Configure<AzureFileLoggerOptions>(Configuration.GetSection("AzureLogging"));
        } 
    }
    

    File appsettings.json should contain

    "AzureLogging": {
         "FileName" : "azure-diagnostics-",
         "FileSizeLimit": 50024,
         "RetainedFileCountLimit": 5
    }
    
  4. Logging should be turned on on Azure Portal. After enabling, Azure Portal may ask for installing addon. Message requiring to install addon will appear on logging config page.

enter image description here

  1. Call logger.LogWarning ("message"); in your code to write to log file. If you use LogWarning be sure to set Level to Warning or more detailed (Info or Debug)
Overcome answered 18/4, 2019 at 7:1 Comment(7)
Thank you for your suggestion. Let me take a look at my code when I have a time.Burnett
Is there any way to test it locally? for the logs to be saved locally?Teyde
I couldn't find anything that stated you didn't need to call logging.AddAzureWebAppDiagnostics();. Is this based on something you've read or just from experience? The docs code sample you've linked to do include this code, so I think it's probably safer to make the call.Overdevelop
@ajbeaven, Azure gives you the option to install the extension automatically if you don't want to make code changes. If you want to be explicit then you can install the NuGet manually and call AddAzureWebAppDiagnostics(). See: mderriey.com/2020/08/08/…Storz
@Storz awesome, good find!Overdevelop
Yeah that's some strange secret magic that you do have to hook up file logging, and using that particular object, and you don't actually have to call AddAzureWebAppDiagnostics. There's an example that shows the file logging being hooked up but no clear implication that that's necessary (on learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/…). I would've guessed it just had trace output and that was essentially "live" by calling the extension method.Circumsolar
AddAzureWebAppDiagnostics() call is needed if you call ClearProviders()Mattison

© 2022 - 2024 — McMap. All rights reserved.