Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log to a file without using third party logger in .Net Core?

How to log to a file without using third party logger (serilog, elmah etc.) in .NET CORE?

public void ConfigureServices(IServiceCollection services) {     services.AddLogging(); }  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {     loggerFactory.AddConsole(Configuration.GetSection("Logging"));     loggerFactory.AddDebug(); } 
like image 585
cilerler Avatar asked Oct 16 '16 18:10

cilerler


People also ask

Which logging framework is best for .NET Core?

NLog is one of the most popular, and one of the best-performing logging frameworks for . NET. Setting up NLog is fairly simple. Developers can use Nuget to download the dependency, then edit the NLog.

How do I inject a logger in .NET Core?

So, go to the Startup. cs file and add the ILoggerFactory parameter in the Configure() method. Then, call the AddFile() extension method to add Serillog file provider, as shown below. ASP.NET Core dependency injection will automatically pass an instance of the LoggerFactory for this parameter.


1 Answers

Issue http://github.com/aspnet/Logging/issues/441 is closed and MS officially recommends to use 3rd party file loggers. You might want to avoid using heavyweight logging frameworks like serilog, nlog etc because they are just excessive in case if all you need is a simple logger that writes to a file and nothing more (without any additional dependencies).

I faced the same situation, and implemented simple (but efficient) file logger: https://github.com/nreco/logging

  • can be used in .NET Core 1.x and .NET Core 2.x / 3.x apps
  • supports custom log message handler for writing logs in JSON or CSV
  • implements simple 'rolling file' feature if max log file size is specified
like image 120
Vitaliy Fedorchenko Avatar answered Sep 19 '22 02:09

Vitaliy Fedorchenko