wiem
wiem

Reputation: 47

ASP.NET Core 3.1 Can't view my logs on Azure Service Log Stream

Below is how I configured it:

1.Install nuget packagae Microsoft.Extensions.Logging.AzureAppServices, Version 3.1.2 and Microsoft.Extensions.Logging.Console, version 3.1.2

I enabled Application Logging in azure

in program.cs

 public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    // We have to be precise on the logging levels
                    logging.AddConsole();
                    logging.AddDebug();
                    logging.AddAzureWebAppDiagnostics();
                })
                 .ConfigureWebHostDefaults(builder =>
                 {
                     builder.ConfigureAppConfiguration((builderContext, config) =>
                     {
                         var env = builderContext.HostingEnvironment;
                      
                     builder.UseStartup<Startup>()
                       
                         .UseSerilog(); ;
                 }).ConfigureServices(services =>
                 {
                  
                     services.Configure<AzureFileLoggerOptions>(options =>
                     {
                         options.FileName = "my-azure-diagnostics-";
                         options.FileSizeLimit = 50 * 1024;
                         options.RetainedFileCountLimit = 5;
                     });
                 }); ;
        }

In startup.cs in ConfigureServices method

 // Logging
            services.AddLogging(builder => builder
                    .AddConsole()
                    .AddDebug()
                    .AddAzureWebAppDiagnostics()
                    .AddApplicationInsights())

                   // .AddConfiguration(Configuration.GetSection("Logging")))
                ;

AND I ADDED LOG MESSAGE

    _logger.LogDebug("DEBUG message. GET enpoint was called.");

but I can't see my log what i'mm missing?

Upvotes: 1

Views: 615

Answers (1)

akb
akb

Reputation: 128

I am showing the code here which works for me. Please deploy and if that is not working then the App Service Logs is not configured correctly

Reference Microsoft.Extensions.Logging (5.0.0), Microsoft.Extensions.Logging.AzureAppServices(5.0.3)

in Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddConsole();
            logging.AddDebug();
            logging.AddAzureWebAppDiagnostics();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

in Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();

}

in appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
  }

in index.cshtml.cs

public void OnGet()
{
    _logger.LogInformation($"Logging {DateTime.Now.ToLongTimeString()}");
}

Configuration in Portal

enter image description here

Upvotes: 3

Related Questions