Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 839
kemakino Avatar asked Feb 28 '19 17:02

kemakino


People also ask

Which .NET function would you use to get a logger?

ILogger : This interface provides the Log() method, which can be used to write a log message. ILoggerProvider : Logging providers implement this interface to write the logs to a specific destination. For example, the Console ILoggerProvider writes the logs to the console.

How do I view .NET Core logs?

The . NET Core agent logs information to the logs folder within C:\ProgramData\Contrast\dotnet-core\ on Windows or /var/tmp/contrast/dotnet-core/ on Linux, by default. Notes: Depending on the setup of the Windows profile and folder view settings, the ProgramData folder may be hidden.

What is logging in .NET Core?

In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).


1 Answers

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)
like image 198
Fyodor Avatar answered Oct 05 '22 23:10

Fyodor