Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5: How to configure the path of logs in Microsoft.Extensions.Logging

I'm working on an MVC6 web app and I'm new to ASP.NET 5. I can see that the Logging (Microsoft.Extensions.Logging) is used at many places (eg: AccountController.cs) in ASP.NET 5 default web application template, but I couldn't figure out where to configure the path of the created log file. Below settings are found in appsettings.json file.

"Logging": {
"IncludeScopes": false,
"LogLevel": {
  "Default": "Verbose",
  "System": "Information",
  "Microsoft": "Information"
}  }

Is it enough to add a parameter in this section? If yes, what is the parameter name? If no, how to do it?

Previously I used Log4Net and configurations were done inside logger.config file.

like image 367
Sudeep A R Avatar asked Oct 17 '25 09:10

Sudeep A R


1 Answers

ILogger is just an abstraction, you have to implement a concrete logger (log4net, serilog, ...) to log in a file.

public Startup(IHostingEnvironment env)
{
    // Some other code 

    Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug().WriteTo.File("YOUR FILE PATH HERE")
            .CreateLogger();

}

In configure's method (using serilog here) :

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog();

    // Some other code
}

Official documentation

like image 101
AdrienTorris Avatar answered Oct 19 '25 01:10

AdrienTorris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!