The Serilog.Extensions.Logging.File package implements loggerFactory.AddFile() to quickly and easily set up file logging in ASP.NET Core apps.

1. Add the NuGet package to your project, either with the package manager or directly into the CSPROJ file:

    <PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.1" />

2. In your Startup class’s Configure() method, call AddFile() on the loggerFactory.

    public void Configure(IApplicationBuilder app,
                          IHostingEnvironment env,
                          ILoggerFactory loggerFactory)
    {
        loggerFactory.AddFile("Logs/myapp-{Date}.txt");

Done! The framework will inject ILogger<T> into controllers and other classes:

class HomeController : Controller
{
    readonly ILogger<HomeController> _log;
    
    public HomeController(ILogger<HomeController> log)
    {
        _log = log;
    }
    
    public IActionResult Index()
    {
        _log.LogInformation("Hello, world!");
    }
}

And events will appear in the log file:

2016-10-18T11:14:11.0881912+10:00 0HKVMUG8EMJO9 [INF] Hello, world! (f83bcf75)

Each log line includes a timestamp, RequestId from ASP.NET Core, the log level, message, and a unique event id. Events that have an associated Exception property will show this as well.

Why ship this package?

ASP.NET Core’s logging subsystem, Microsoft.Extensions.Logging, comes with a couple of simple providers that can log to the console and the .NET Debug class. More advanced logging features, and broader targets including databases, log servers and so-on are supported through complete providers like Serilog and NLog.

One place the pattern can fall short is in the early stages of project setup, when logging requirements are simple. At this stage, a one-liner like loggerFactory.AddFile() with sensible defaults could get the job done. Serilog configuration for .NET Core isn’t complex, but there are definitely some details that can be safely ignored in the early days of a new project.

That’s where Serilog.Extensions.Logging.File fits in. The goal of this package is to achieve that quick setup experience while maintainig a low-risk transition to the flexible LoggerConfiguration API when it’s needed.

There’s an element of experimentation in this approach and I’m very interested to see how it pans out. If you find this package useful, or have some feedback, please drop by the Gitter channel and let us know!

(Updated March 2017 for .NET Core 1.0 tooling.)