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

Alv*_*ero 4 c# logging azure azure-web-app-service asp.net-core-3.1

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

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

我尝试使用我在 Microsoft 文档中找到的不同方法进行登录,但都没有奏效。

    [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";
    }
Run Code Online (Sandbox Code Playgroud)

控制器构造函数:

    private readonly ILogger _logger;
    public FaceRecognitionController(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<FaceRecognitionController>();
    }
Run Code Online (Sandbox Code Playgroud)

配置方法:

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();
        });
    }
Run Code Online (Sandbox Code Playgroud)

有谁知道我能做什么?

日志流截图: 在此处输入图片描述

Iva*_*ang 9

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

1.为您的项目安装 nuget 包Microsoft.Extensions.Logging.AzureAppServices,版本 3.1.2Microsoft.Extensions.Logging.Console,版本 3.1.2

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

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

        //add the following code
        services.AddLogging(loggingBuilder =>
        {
            loggingBuilder.AddConsole();
            loggingBuilder.AddDebug();
            loggingBuilder.AddAzureWebAppDiagnostics();
        });
    }
Run Code Online (Sandbox Code Playgroud)

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

    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();
    }
Run Code Online (Sandbox Code Playgroud)

3.将其发布到azure,然后按照您的帖子中所述配置“应用服务日志”。

4.在azure门户导航到“日志流”,然后访问网站,可以看到日志:

在此处输入图片说明

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