我在 Azure 日志流中看不到日志 [英] I cannot see logs in Azure Log Stream

查看:36
本文介绍了我在 Azure 日志流中看不到日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的应用服务日志如下所示:

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

I am trying to log information of my ASP.NET Core App and I cannot find a way to display the loogs in Azure Log Stream. The application successfully logs when I debug in Visual Studio but I do not see anything when I publish to Azure.

This is how my App Service Logs looks like: App Service Logs

I have tried to log using different methods that I have found in Microsoft documentation and none has worked.

    [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";
    }

Controller constructor:

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

Configure method:

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

Does anyone know what I can do?

Log Stream screenshot: enter image description here

解决方案

For .NET core 3.1, please follow the steps below:

1.Install nuget packagae Microsoft.Extensions.Logging.AzureAppServices, Version 3.1.2 and Microsoft.Extensions.Logging.Console, version 3.1.2 for your project.

2.In Startup.cs -> ConfigureServices method, add the following code:

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

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

then in the controller class, code looks like below:

    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 it to azure, and then configure "App Service Logs" as mentioned in your post.

4.Nav to "Log stream" in azure portal, then visit the web site, you can see the logs:

Note: You should always use ILogger in asp.net core for logging, Trace.TraceInformation may not work for it.

这篇关于我在 Azure 日志流中看不到日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆