What is the Mobilize.Extensions.Logging.RollingFile?

This is a Nuget Package that when added to the migration Solutions enable a Logger Provider for ASP.Net Core. This logger provider lets the user adds all logs into files, what can be useful for user to understand the application execution or for determining the reason of an error.

Using the Rolling File Provider

Configure the provider by calling AddFile() on an ILoggingBuilder during logger configuration in Program.cs.

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(builder => builder.AddFile()) // <- Add this line
            .UseStartup()
            .Build();
}

You can pass additional options to the Add File by passing an Action, for example:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(builder => builder.AddFile(options => {
                options.FileName = "diagnostics-"; // The log file prefixes
                options.LogDirectory = "LogFiles"; // The directory to write the logs
                options.FileSizeLimit = 20 * 1024 * 1024; // The maximum log file size (20MB here)
            })) 
            .UseStartup()
            .Build();
}

Finally, The provider will create log files prefixed with the FileName, and suffixed with the current date in the yyyyMMdd format.

log-20160631.txt
log-20160701.txt
log-20160702.txt

Logs will look something like the following:

2017-09-01 18:34:18.083 +01:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://localhost:50037/api/values  
2017-09-01 18:34:18.159 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method SampleApp.Controllers.ValuesController.Get (SampleApp) with arguments ((null)) - ModelState is Valid
2017-09-01 18:34:18.161 +01:00 [Information] SampleApp.Controllers.ValuesController: Executed Get action
2017-09-01 18:34:18.165 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor: Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
2017-09-01 18:34:18.192 +01:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action SampleApp.Controllers.ValuesController.Get (SampleApp) in 36.3435ms
2017-09-01 18:34:18.195 +01:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 113.6076ms 200 application/json; charset=utf-8

Last updated