azure - ASP.NET Core 3.1 无法在 Azure 服务日志流上查看我的日志

标签 azure asp.net-core logging

以下是我的配置方法:

1.安装nuget包Microsoft.Extensions.Logging.AzureAppServices,版本3.1.2和Microsoft.Extensions.Logging.Console,版本3.1.2

我在 azure 中启用了应用程序日志记录

在program.cs中

 public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    // We have to be precise on the logging levels
                    logging.AddConsole();
                    logging.AddDebug();
                    logging.AddAzureWebAppDiagnostics();
                })
                 .ConfigureWebHostDefaults(builder =>
                 {
                     builder.ConfigureAppConfiguration((builderContext, config) =>
                     {
                         var env = builderContext.HostingEnvironment;
                      
                     builder.UseStartup<Startup>()
                       
                         .UseSerilog(); ;
                 }).ConfigureServices(services =>
                 {
                  
                     services.Configure<AzureFileLoggerOptions>(options =>
                     {
                         options.FileName = "my-azure-diagnostics-";
                         options.FileSizeLimit = 50 * 1024;
                         options.RetainedFileCountLimit = 5;
                     });
                 }); ;
        }

在startup.cs中的ConfigureServices方法中

 // Logging
            services.AddLogging(builder => builder
                    .AddConsole()
                    .AddDebug()
                    .AddAzureWebAppDiagnostics()
                    .AddApplicationInsights())

                   // .AddConfiguration(Configuration.GetSection("Logging")))
                ;

我添加了日志消息

    _logger.LogDebug("DEBUG message. GET enpoint was called.");

但我看不到我的日志,我缺少什么?

最佳答案

我在这里展示了适合我的代码。请部署,如果不起作用,则应用服务日志配置不正确

引用 Microsoft.Extensions.Logging (5.0.0), Microsoft.Extensions.Logging.AzureAppServices(5.0.3)

在Program.cs中

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddConsole();
            logging.AddDebug();
            logging.AddAzureWebAppDiagnostics();
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

在 Startup.cs 中

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();

}

在appsettings.json

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

在index.cshtml.cs中

public void OnGet()
{
    _logger.LogInformation($"Logging {DateTime.Now.ToLongTimeString()}");
}

门户中的配置

enter image description here

关于azure - ASP.NET Core 3.1 无法在 Azure 服务日志流上查看我的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66365192/

相关文章:

java.lang.reflect.InaccessibleObjectException : Unable to make field private final java. lang.Object java.util.Optional.value 可访问:

mongodb - Go:创建 io.Writer 接口(interface)用于记录到 mongodb 数据库

Azure CustomScript 扩展未在 Centos 7.2 中执行

django - Azure/Kudu 将 python PATH 从 2.7 更新到 3.6

c# - ASP.NET Core RC2 和 .NET 4.5.1 应用程序之间的共享 cookie 身份验证

asp.net-core - ASP.NET 核心 3.1 :Adding Classes Libraries to the Project_ MissingMethodException: Method not found: 'Boolean Microsoft. EntityFrameworkCore.Migrations

asp.net-core - 在ASP.Net Core中转换Open Id Connect声明

java - 每天如何将日志文件存储在目录(具有当前日期的目录名称)中

Azure DevOps 自托管代理 - 如何复制云托管代理?

css - 字体在 azure 上不起作用