I cannot see logs in Azure Log Stream

后端 未结 1 1920
予麋鹿
予麋鹿 2021-01-20 13:10

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 Vis

相关标签:
1条回答
  • 2021-01-20 13:54

    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.

    0 讨论(0)
提交回复
热议问题