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

前端 未结 8 1101
北海茫月
北海茫月 2020-12-25 09:22

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

public void ConfigureSer         


        
相关标签:
8条回答
  • 2020-12-25 10:08

    Unfortunately, it not possible with the current version of ILogger. As @Vitaliy Fedorchenko mentioned in his post, the issue http://github.com/aspnet/Logging/issues/441 has been closed after recommending to use 3rd party file loggers. However, most of the file loggers out there are heavyweight (like Serilog) with limited configuration options. Unlike those libraries, Karambolo.Extensions.Logging.File provides an incredible functionality with a huge set of configuration options, which allows multiple logging providers with multiple files for each one.

    This class library contains a lightweight implementation of the Microsoft.Extensions.Logging.ILoggerProvider interface for file logging. Runs on all .NET platforms which implement .NET Standard 2.0+ including .NET Core 2 (ASP.NET Core 2.1+) and .NET Core 3 (ASP.NET Core 3.0+).

    Additional features:

    • Flexible configuration:
      • Two-level log file settings.
      • Fine-grained control over log message filtering.
    • Rolling log files with the customizable counter format.
    • Including log entry date in log file paths using templates.
    • Customizable log text formatting.
    • Extensibility by inheritance.
    • Multiple providers with different settings.

    See the documentation for more informations.

    0 讨论(0)
  • 2020-12-25 10:16

    The ones provided by Adam and Vitaliy are still probably the most simple ones to date (thanks guys btw!).
    Since an external NuGet is necessary anyway, worth mentioning that there is also a "standalone" extension of the Serilog rolling file sink that can be simply used as a logging provider for .net core, without completely swapping out the logging pipeline (pulls other dependencies, but I don't see that as an issue if you need the few extra features provided)

    As of March 2020, this is the complete picture:

    https://www.nuget.org/packages/Karambolo.Extensions.Logging.File

    https://www.nuget.org/packages/NReco.Logging.File

    https://www.nuget.org/packages/Serilog.Extensions.Logging.File

    0 讨论(0)
提交回复
热议问题