如果我们想将应用程序日志写入Azure blob存储,首先需要在Azure门户上启用应用程序日志并为其配置blob存储。
我们可以遵循
blog
在Asp中设置日志记录。net core应用程序。
在ASP中设置日志记录。NET Core应用程序不需要太多代码。ASP。NET核心新项目模板已经在启动时使用此代码设置了一些基本日志提供程序。配置方法:
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
我也为它做了演示。对我来说,它工作正常。
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.在HomeController中添加以下代码。反恐精英
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.访问主页/索引页并从Azure存储blob中进行检查。日志blob名称的最后一部分。默认为“
应用程序日志。txt文件
“。我们也可以自己设置。