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();
}
No file logger is currently included in the framework but adding one is being considered: http://github.com/aspnet/Logging/issues/441. Feel free to upvote the issue on github.
If you are using IIS, you can enable and view stdout logs:
true
..\logs\stdout
).For information about stdout logging, see Troubleshoot ASP.NET Core on IIS.
.NET Core
2.02.1 is out but it still does not provide an ILoggerProvider implementation for file logging.I was looking for a viable and lightweight 3rd-party implementation but found none, so I decided to write one that covers the features of the built-in ConsoleLogger and provides additional essential functionality. My library is free, open-source and has only framework dependencies.
It completely conforms with the Microsoft provider implementations. Usage is as simple as follows:
.NET Core 2.1:
.NET Core 2.0:
.NET Core 1.1:
For configuration details, see the project site.
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