Where does the ASP.NET Core logging API as default store logs?

后端 未结 2 2043
感情败类
感情败类 2021-01-11 23:31

In the ASP.NET Core 2.0, I use the default logging API. My app is hosted as an Azure Web App.

My question: Where are these outputted? And how do I modify it?

相关标签:
2条回答
  • 2021-01-12 00:09

    After playing around for an hour, I got to understand how it plays together in asn ASP.NET Core app.

    First of all, i would recommend watching this YouTube video: https://www.youtube.com/watch?v=icwD6xkyrsc .

    How to solve it

    STEP 1:

    Go to your Startup.cs class. Inside the Configure method, make sure it has the following signature:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    

    Mine did not have the ILoggerFactory. However, you need to add this one. It's by default injected into the class by ASP.NET Core.

    STEP 2:

    Set up your provider.

    I setup the following two:

    loggerFactory.AddDebug();
    loggerFactory.AddAzureWebAppDiagnostics();
    

    The AddAzureWebAppDiagnostics comes from the package Ojisah mentioned in his answer.

    STEP 3:

    Now we can start logging.

    Example in my HomeController:

        private readonly ILogger<HomeController> _logger;
    
        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
    
        public IActionResult Index()
        {
            _logger.LogInformation("TEST INDEX LOGGER");
            return View();
        }
    

    STEP 4: Make sure your log level matches your expectations

    Look at your appsettings.json to make sure the warning level matches your expectations:

    {
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Information"
        }
      }
    }
    

    STEP 5: See the logs

    In Azure, I have been setting up the Diagnostics logs tab, I've set this up so it logs to my blobs:

    STEP 6:

    Now you can download and see the log files which is inside your BLOB.

    Example from my log file where we can see the log from my HomeController:

    2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 1.6257ms
    2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0892ms 200 text/html; charset=utf-8
    2018-03-05 14:15:32.608 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
    2018-03-05 14:15:32.610 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0154ms 302 
    2018-03-05 14:15:32.844 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
    2018-03-05 14:15:32.845 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.571ms 404 
    2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/  
    2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) with arguments ((null)) - ModelState is Valid
    2018-03-05 14:15:46.878 +00:00 [Information] Likvido.Website.Main.Controllers.HomeController: TEST INDEX LOGGER
    2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor: Executing ViewResult, running view at path /Views/Home/Index.cshtml.
    2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 0.7351ms
    2018-03-05 14:15:46.879 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.996ms 200 text/html; charset=utf-8
    2018-03-05 14:15:47.518 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
    2018-03-05 14:15:47.520 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 1.6787ms 302 
    2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
    2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.628ms 404 
    
    0 讨论(0)
  • 2021-01-12 00:15

    You need to add providers as mentioned later in https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x :

    public static void Main(string[] args)
    {
        var webHost = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var env = hostingContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
                config.AddEnvironmentVariables();
            })
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                logging.AddConsole();
                logging.AddDebug();
            })
            .UseStartup<Startup>()
            .Build();
    
        webHost.Run();
    }
    

    For azure you can use the Azure App Service provider (https://www.nuget.org/packages/Microsoft.Extensions.Logging.AzureAppServices)

    logging.AddAzureWebAppDiagnostics();
    
    0 讨论(0)
提交回复
热议问题