Skip to content

Zero logger provider implementation for Microsoft.Extensions.Logging.

License

Notifications You must be signed in to change notification settings

RainingNight/zero-logging

Repository files navigation

zero-logging

Zero logger provider for Microsoft.Extensions.Logging, the logging subsystem used by ASP.NET Core.

Logging in Elasticsearch

PLEASE Read Zero.Logging.Elasticsearch.

Logging in File

Install

First, install the Zero.Logging.File NuGet package into your app:

dotnet add package Zero.Logging.File --version 1.0.0-alpha3-20180228

Configure

Next, add file section config in appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "File": {
      "LogLevel": {
        "Default": "Error"
      },
      "RollingInterval": "Minute"
    }
  }
}

Finally, in your application's Program.cs file, configure Zeor.Logging.File first:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddFile();
        })
        .UseStartup<Startup>()
        .Build();

Demonstrate

Call logging methods on that logger object:

public class ValuesController : Controller
{
    private readonly ILogger _logger;

    public ValuesController(ILogger<ValuesController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public void Get()
    {
        _logger.LogTrace("Log Trace.");
        _logger.LogInformation("Log Information.");
        _logger.LogDebug("Log Debug.");
        try
        {
            throw new Exception("Boom");
        }
        catch (Exception ex)
        {
            _logger.LogCritical(1, ex, "Unexpected critical error starting application");
            _logger.LogError(1, ex, "Unexpected error");
            _logger.LogWarning(1, ex, "Unexpected warning");
        }
    }
}

That's it! With the level bumped up a little you will see log output like:

# logs/log-201802271502.txt

2018-02-27 15:02:40.608 +08:00 [Critical] WebApplication1.Controllers.ValuesController: Unexpected critical error starting application
System.Exception: Boom
   at WebApplication1.Controllers.ValuesController.Get() in C:\Users\rainging\source\repos\WebApplication1\WebApplication1\Controllers\ValuesController.cs:line 28
2018-02-27 15:02:40.631 +08:00 [Error] WebApplication1.Controllers.ValuesController: Unexpected error
System.Exception: Boom
   at WebApplication1.Controllers.ValuesController.Get() in C:\Users\rainging\source\repos\WebApplication1\WebApplication1\Controllers\ValuesController.cs:line 28

About

Zero logger provider implementation for Microsoft.Extensions.Logging.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published