gpt4 book ai didi

c# - 如何为 ASP.NET Core 应用程序配置 Azure 应用服务日志记录提供程序?

转载 作者:行者123 更新时间:2023-12-04 12:37:41 25 4
gpt4 key购买 nike

我正在尝试在 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>();
});

app.settings 中的日志配置:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

我正在 Controller 中记录信息、警告和错误。

按照 Azure 文档 ( https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer ),我在 Azure 应用程序 -> 应用程序服务日志中启用了对文件系统的日志记录:

Azure config

我的应用程序部署正常并且工作正常。我期望在应用程序的文件系统中的某个位置找到一个名为“azure-diagnostics”的日志文件。我已通过转到 Azure 应用程序 -> SSH 并运行来检查文件系统:

find . -name '*azure-diagnostics*'

它不返回任何内容。我还使用 Kudu 和 VS Cloud Explorer 检查了文件系统,文件不存在。如果我添加控制台日志记录提供程序,日志就会很好地显示在标准 Azure 日志文件中。我在这里缺少什么?

最佳答案

我无法使用 AzureWebAppDiagnostics 日志记录提供程序使其工作,但我已经通过使用带有文件接收器的 Serilog 提供程序解决了我的问题。

在program.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>();
});
}

UseSeriLog() 调用替换了默认的 MS 日志工厂,因此我还可以从应用程序设置中删除日志记录配置。

现在我可以看到我的自定义日志文件按预期出现在/home/LogFiles 目录中。

关于c# - 如何为 ASP.NET Core 应用程序配置 Azure 应用服务日志记录提供程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com