Reputation: 11269
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
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
Reputation: 2867
I am sharing the Azure portal steps in Azure App Service and also the code required for it.
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>();
});
Just take a note of logging.AddAzureWebAppDiagnostics(); in above code
Upvotes: 3
Reputation: 382
You can stream logs live in Cloud Shell, use the following command:
az webapp log tail --name appname --resource-group myResourceGroup
Or you can navigate to your app and select Log stream.
Upvotes: 6