Reputation: 1590
I created my own custom loger provider FileLogerProviderr to save logs to file.
I added custom FileLoggerProvider in program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddProvider(new FileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")));
logging.SetMinimumLevel(LogLevel.None);
})
.UseStartup<Startup>();
To test I added log in controller:
public class HomeController : Controller
{
private ILogger<HomeController> logger;
public HomeController(ILogger<HomeController> log)
{
logger = log;
}
public IActionResult Index()
{
logger.LogDebug($"test index path!!!");
return View();
}
}
Working, But in file except test index path I see many additional information. I dont need this information. I need only save in file my log from controller.
Request starting HTTP/1.1 GET http://localhost:44339/
Route matched with {action = "Index", controller = "Home", page = "", area = ""}. Executing action LoggingTest.Controllers.HomeController.Index (LoggingTest)
Executing action method LoggingTest.Controllers.HomeController.Index (LoggingTest) - Validation state: Valid
test index path!!!
Executed action method LoggingTest.Controllers.HomeController.Index (LoggingTest), returned result Microsoft.AspNetCore.Mvc.ViewResult in 11.8159ms.
Executing ViewResult, running view Index.
Executed ViewResult - view Index executed in 43.4928ms.
Executed action LoggingTest.Controllers.HomeController.Index (LoggingTest) in 224.1184ms
Request finished in 400.2906ms 200 text/html; charset=utf-8
Request starting HTTP/1.1 GET http://localhost:44339/favicon.ico
Sending file. Request path: '/favicon.ico'. Physical path: 'C:\Users\dborovsky\projects\LoggingTest\LoggingTest\wwwroot\favicon.ico'
Request finished in 39.8394ms 200 image/x-icon
How I can solve this? Thank you
UPDATE appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "CONSTRING"
},
"Logging": {
"LogLevel": {
"Default": "None",
"System": "None",
"Microsoft": "None"
}
},
"AllowedHosts": "*"
}
Upvotes: 0
Views: 1447
Reputation: 1149
Maybe you´ll like to use a configuration to prevent all events gets logged.
I recommend you inject the logger in ConfigureServices (.ConfigureLogging from Program.cs at least you want to log web hosting events):
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggers=>
{
loggers.AddConsole();
loggers.AddDebug();
loggers.AddProvider(new FileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")));
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Then you´ll can configure as follows (make sure you have the enviroment variant properly configurated):
{
"ConnectionStrings": {
"DefaultConnection": "CONSTRING"
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "None",
"Microsoft": "None"
}
},
"AllowedHosts": "*"
}
And the implementation:
public class HomeController : Controller
{
private ILogger<HomeController> logger;
public HomeController(ILogger<HomeController> log)
{
logger = log;
}
public IActionResult Index()
{
logger.LogDebug($"test index path!!!");
return View();
}
}
Here is as pretty nice Microsoft´s article about logging:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2
Upvotes: 1