Mic*_*ski 6 c# azure azure-web-app-service asp.net-core
我正在尝试在 ASP.NET Core MVC 应用程序中配置自定义日志记录。该应用托管在 Linux 上的 Azure 应用服务(免费套餐)上。日志文件没有出现,我做错了什么?
我的配置:
在 ASP.NET Core 日志记录文档(https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#azure-app-service-provider)之后,我添加了我的应用程序的 Azure 应用服务日志记录提供程序:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddAzureWebAppDiagnostics();
})
.ConfigureServices(serviceCollection => serviceCollection
.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 10 * 1024;
options.RetainedFileCountLimit = 5;
})
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://*:8080");
webBuilder.UseStartup<Startup>();
});
Run Code Online (Sandbox Code Playgroud)
app.settings 中的日志配置:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)
我正在控制器中记录信息、警告和错误。
按照Azure文档(https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer),我已在Azure上启用了对文件系统的日志记录应用程序 -> 应用程序服务日志:
我的应用程序部署良好并且工作正常。我期望在应用程序的文件系统中的某个位置找到一个名为“azure-diagnostics”的日志文件。我通过转到 Azure App -> SSH 并运行以下命令来检查文件系统:
find . -name '*azure-diagnostics*'
Run Code Online (Sandbox Code Playgroud)
它不返回任何内容。我还使用 Kudu 和 VS Cloud Explorer 检查了文件系统,文件不存在。如果我添加控制台日志记录提供程序,日志就会很好地显示在标准 Azure 日志文件中。我在这里缺少什么?
我无法使用 AzureWebAppDiagnostics 日志记录提供程序使其工作,但我已设法通过使用具有文件接收器的 Serilog 提供程序解决了我的问题。
在程序.cs中:
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.File(@"../../LogFiles/azure-diagnostics-", fileSizeLimitBytes: 50 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 5)
.CreateLogger();
try
{
CreateHostBuilder(args).Build().Run();
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls("http://*:8080");
webBuilder.UseStartup<Startup>();
});
}
Run Code Online (Sandbox Code Playgroud)
UseSeriLog() 调用替换了默认的 MS 日志工厂,因此我还可以从 appsettings 中删除日志记录配置。
现在我可以看到我的自定义日志文件按预期出现在 /home/LogFiles 目录中。
归档时间: |
|
查看次数: |
4949 次 |
最近记录: |