azure - 我需要如何登录才能在应用服务日志中查看我的日志

标签 azure asp.net-core .net-core azure-web-app-service azure-app-service-envrmnt

我已启用应用程序日志记录到文件系统和 Blob。我使用 ILogger<T>.LogInformation() 记录消息和Trace.WriteLine() 。这些在 blob 中都不可见。我也在文件系统上找不到它们。当我启用日志流时,我也看不到它们。

我需要在 ASP.NET Core 应用程序中配置某些内容吗?

最佳答案

[Microsoft.Extensions.Logging.AzureAppServices][1] 提供程序包将日志写入 Azure 应用服务应用文件系统中的文本文件以及以下位置的 Blob 存储: Azure 存储帐户。

提供程序包不包含在共享框架中。要使用提供程序,请将提供程序包添加到项目中。

要配置提供商设置,请使用 AzureFileLoggerOptionsAzureBlobLoggerOptions ,如下例所示:

public static void Main(string[] args)
{
    var host = CreateHostBuilder(args).Build();

    var todoRepository = host.Services.GetRequiredService<ITodoRepository>();
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Feed the dog" });
    todoRepository.Add(new Core.Model.TodoItem() { Name = "Walk the dog" });

    var logger = host.Services.GetRequiredService<ILogger<Program>>();
    logger.LogInformation("Seeded the database.");

    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
            .ConfigureServices(serviceCollection => serviceCollection
                .Configure<AzureFileLoggerOptions>(options =>
                {
                    options.FileName = "azure-diagnostics-";
                    options.FileSizeLimit = 50 * 1024;
                    options.RetainedFileCountLimit = 5;
                }).Configure<AzureBlobLoggerOptions>(options =>
                {
                    options.BlobName = "log.txt";
                })
            )
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

您可以引用以下链接以获取更多引用:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#azure-app-service-provider

希望有帮助。

关于azure - 我需要如何登录才能在应用服务日志中查看我的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462181/

相关文章:

c# - Blazor - 导航和状态问题

asp.net-core - 无法使用 ASP.NET CORE 2.2 生成用于本地化的响应 cookie

c# - 使用 xUnit 和 asp.net core 测试我的服务

angularjs - Visual Studio 2015 显示 'Debug is Starting'

c# - IIS Express 与 dotnet 运行

使用自定义属性时出现 Azure B2c 错误 'Unable to validate the information provided.'

http - 在 Azure Web 应用服务中从 https 重定向到 http

azure - 从 PowerShell 连接到 Azure 时出错

c# - Botframework Web 应用程序 Azure IDX20803 : Unable to obtain configuration from: '[PII is hidden]

c# - 为什么我的 KendoUI 网格在我的 ASP.NET Core 应用程序中显示重复记录?