Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 363
Lars Holdgaard Avatar asked Mar 05 '18 13:03

Lars Holdgaard


People also ask

Where does logger store logs .NET Core?

TRACE Level and Debug Logs NET Core agent logs information to the logs folder within C:\ProgramData\Contrast\dotnet-core\ on Windows or /var/tmp/contrast/dotnet-core/ on Linux, by default.

Where does ILogger log to by default?

The default logger logs to Console window. If you run your application on self hosted mode you will see the logs.

Where are .NET logs located?

The . NET Framework agent logs information to the logs folder within C:\ProgramData\Contrast\dotnet\ by default. Notes: Depending on the setup of the Windows profile and folder view settings, the ProgramData folder may be hidden.


2 Answers

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 
like image 182
Lars Holdgaard Avatar answered Oct 26 '22 14:10

Lars Holdgaard


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();
like image 43
Ojisah Avatar answered Oct 26 '22 13:10

Ojisah