我在 Azure 日志流中看不到日志

I cannot see logs in Azure Log Stream

我正在尝试记录我的 ASP.NET 核心应用程序的信息,但我找不到在 Azure 日志流中显示日志的方法。当我在 Visual Studio 中调试时应用程序成功登录,但当我发布到 Azure 时我没有看到任何东西。

这是我的应用服务日志的样子: App Service Logs

我已尝试使用我在 Microsoft 文档中找到的不同方法进行登录,并且 none 有效。

    [HttpGet]
    public string Index()
    {
        Trace.TraceInformation("You are in the face recognition controller. Trace");
        _logger.LogInformation("You are in the face recognition controller. Logger");
        return "You are in the face recognition controller";
    }

控制器构造函数:

    private readonly ILogger _logger;
    public FaceRecognitionController(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<FaceRecognitionController>();
    }

配置方法:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        loggerFactory.CreateLogger("console");
        loggerFactory.CreateLogger("debug");

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

有人知道我能做什么吗?

日志流截图: enter image description here

对于.NET core 3.1,请按照以下步骤操作:

1.Install nuget packagae Microsoft.Extensions.Logging.AzureAppServices, Version 3.1.2 and Microsoft.Extensions.Logging.Console, version 3.1.2 用于您的项目。

2.In Startup.cs -> ConfigureServices方法,添加如下代码:

    public void ConfigureServices(IServiceCollection services)
    {
        //other code

        //add the following code
        services.AddLogging(loggingBuilder =>
        {
            loggingBuilder.AddConsole();
            loggingBuilder.AddDebug();
            loggingBuilder.AddAzureWebAppDiagnostics();
        });
    }

然后在控制器中 class,代码如下所示:

    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogInformation("**********first: hello, this is a test message!!!");
        _logger.LogInformation("**********second: hello, this is a test message!!!");
        return View();
    }

3.Publish 将其设置为 azure,然后按照 post.

中所述配置 "App Service Logs"

4.Nav到"Log stream"在azure portal中,然后访问网站,可以看到日志:

注意:您应该始终在 asp.net 核心中使用 ILogger 进行日志记录,Trace.TraceInformation 可能不适合它。