H2ONaCl
H2ONaCl

Reputation: 11269

How can I view the Azure App Service log files?

The default API example in Visual Studio 2019 instantiates an ILogger<T>. If I invoke it via _logger.Log(LogLevel.Information, "hello") how can I view the log file? This question assumes use of Azure App Service.

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
    }
}

Upvotes: 5

Views: 21805

Answers (3)

Felipe Augusto
Felipe Augusto

Reputation: 1553

Try to use the methods from namespace System.Diagnostics.Trace to register info about diagnostic

eg: System.Diagnostics.Trace.TraceError("If you're seeing this, something bad happened");

By Default, .NET use this provider for logs:

Microsoft.Extensions.Logging.AzureAppServices.

Upvotes: 0

Sumer
Sumer

Reputation: 2867

I am sharing the Azure portal steps in Azure App Service and also the code required for it.

  1. First of all enable logging via App Service Logs Options enter image description here

  2. Then check out the live log via 'Log Stream' Option enter image description here

  3. Also sharing my code change for .netcore 3.1

    using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.EventLog;

     namespace MPRC.Common.SAMPLE
     {
         public class Program
         {
             public static void Main(string[] args)
             {
                 CreateHostBuilder(args).Build().Run();
             }
    
             public static IHostBuilder CreateHostBuilder(string[] args) =>
                 Host.CreateDefaultBuilder(args)
                     .ConfigureLogging((hostingContext, logging) =>
                     {
                         logging.ClearProviders();
                         logging.AddConfiguration(..sample...);                    
                         logging.AddEventLog(new EventLogSettings()
                         {
                         //...........
                         });
                         logging.AddConsole();
                         logging.AddAzureWebAppDiagnostics();
                     })
                     .ConfigureWebHostDefaults(webBuilder =>
                     {
                         webBuilder.UseStartup<Startup>();
                     });
    
  4. Just take a note of logging.AddAzureWebAppDiagnostics(); in above code

Upvotes: 3

Bao To Quoc
Bao To Quoc

Reputation: 382

  1. You can stream logs live in Cloud Shell, use the following command:

    az webapp log tail --name appname --resource-group myResourceGroup

  2. Or you can navigate to your app and select Log stream.

enter image description here

Upvotes: 6

Related Questions