How to log to a file without using third party log

2019-03-18 03:19发布

问题:

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();
}

回答1:

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.



回答2:

.NET Core 2.0 2.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:

Install-Package Karambolo.Extensions.Logging.File

.NET Core 2.1:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(lb =>
    {
        lb.AddConfiguration(Configuration.GetSection("Logging"));
        lb.AddFile(o => o.RootPath = AppContext.BaseDirectory);
    });
}

.NET Core 2.0:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(lb =>
    {
        lb.AddConfiguration(Configuration.GetSection("Logging"));
        lb.AddFile(new FileLoggerContext(AppContext.BaseDirectory, "default.log"));
    });

    services.Configure<FileLoggerOptions>(Configuration.GetSection("Logging:File"));
}

.NET Core 1.1:

var context = new FileLoggerContext(AppContext.BaseDirectory, "default.log");
loggerFactory.AddFile(context, Configuration.GetSection("Logging:File"));

For configuration details, see the project site.



回答3:

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 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


回答4:

If you are using IIS, you can enable and view stdout logs:

  1. Edit the web.config file.
  2. Set stdoutLogEnabled to true.
  3. Change the stdoutLogFile path to point to the logs folder (for example, .\logs\stdout).
  4. Save the file.
  5. Make a request to the app.
  6. Navigate to the logs folder. Find and open the most recent stdout log.

For information about stdout logging, see Troubleshoot ASP.NET Core on IIS.