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

18,650

Solution 1

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:

enter image description here

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 

Solution 2

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();
Share:
18,650

Related videos on Youtube

Lars Holdgaard
Author by

Lars Holdgaard

An entrepreneur since I was 18. Been building a lot of stuff: some successful, some not so successful Currently building the best debt collection company in the world at Likvido. In my free time (when not tinkering with code), I'm very much into crypto, traveling and being digital-nomad when it suits my lifestyle... And I blog at LarsHoldgaard.com!

Updated on September 16, 2022

Comments

  • Lars Holdgaard
    Lars Holdgaard over 1 year

    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?

    (I don't need them in my database or file system for now, just read the recent logs for some debugging).

    WHAT I AM DOING:

    In my Startup.cs file I have injected the logging:

    private readonly ILogger<Startup> _logger;
    
    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        _logger = logger;
        Configuration = configuration;
    }
    

    And I then write a couple of:

    _logger.LogInformation("Configure RUNNING");
    

    However, I haven't been able to find any of these in a log inside FTP/Azure portal.