ASP.NET Core Application Logs Not Written To Blob in Azure App Service
I've enabled application logging to a blob for an app service on Azure.
xxxxx-#####.applicationLog.csv
is being created each hour in the Azure storage account I created, but this file doesn't actually contain the my application logs Configuration details:
Program.cs
via: .ConfigureLogging(log => log.AddAzureWebAppDiagnostics())
(which is apparently necessary when running on .NET Framework instead of .NET Core runtime) In summary: No files containing my application logs are created in Azure Storage when I configure it that way in the Azure portal.
If we want to write Application logs to Azure blob storage ,firstly we need to enable Application log and configurate blob storage for it on the Azure portal.
We could following the blog to set up logging in the Asp.net core app.
setting up logging in an ASP.NET Core app doesn't require much code. ASP.NET Core new project templates already setup some basic logging providers with this code in the Startup.Configure method:
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
I also do demo for it. It works correctly on my side.
1.In the Sartup.cs file add the following code
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics(
new AzureAppServicesDiagnosticsSettings
{
OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}",
}
);
2.add following code in the HomeController.cs
private readonly ILogger _logger;
public HomeController(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HomeController>();
}
public IActionResult Index()
{
_logger.LogInformation("Log information");
_logger.LogError("Logger error");
return View();
}
3.Visit the home/index page and check it from the Azure storage blob. Last section of log blob name. Defaults to " applicationLog.txt ". We also could set it ourseleves.
链接地址: http://www.djcxy.com/p/66116.html上一篇: Java应用程序:无法读取ISO