Skip to content

DrewScoggins/serilog-extensions-logging

 
 

Repository files navigation

Serilog.Extensions.Logging

Build status NuGet Version

A Serilog provider for Microsoft.Extensions.Logging, the logging subsystem used by ASP.NET 5.

This package routes ASP.NET log messages through Serilog, so you can get information about ASP.NET's internal operations logged to the same Serilog sinks as your application events.

Instructions

First, install the Serilog.Extensions.Logging NuGet package into your web or console app. You will need a way to view the log messages - Serilog.Sinks.Literate writes these to the console.

Install-Package Serilog.Extensions.Logging
Install-Package Serilog.Sinks.Literate

Next, in your application's Startup method, configure Serilog first:

using Serilog;

public class Startup
{
  public Startup(IHostingEnvironment env)
  {
    Log.Logger = new LoggerConfiguration()
      .WriteTo.LiterateConsole()
      .CreateLogger();
      
    // Other startup code

Finally, in your Startup class's Configure() method, call AddSerilog() on the provided loggerFactory.

  public void Configure(IApplicationBuilder app,
                        IHostingEnvironment env,
                        ILoggerFactory loggerfactory)
  {
      loggerfactory.AddSerilog();

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

2015-05-15 22:14:44.646 +10:00 [DBG] RouteCollection.RouteAsync
	Routes: 
		Microsoft.AspNet.Mvc.Routing.AttributeRoute
		{controller=Home}/{action=Index}/{id?}
	Handled? True
2015-05-15 22:14:44.647 +10:00 [DBG] RouterMiddleware.Invoke
	Handled? True
2015-05-15 22:14:45.706 +10:00 [DBG] /lib/jquery/jquery.js not modified
2015-05-15 22:14:45.706 +10:00 [DBG] /css/site.css not modified
2015-05-15 22:14:45.741 +10:00 [DBG] Handled. Status code: 304 File: /css/site.css

Levels

If you want to get more information from the log you'll need to bump up the level.

Two things:

  • You need to set MinimumLevel on both the Serilog LoggerConfiguration and the ILoggerFactory
  • Serilog and ASP.NET assign different priorities to the Debug and Verbose levels; Serilog's Debug is ASP.NET's Verbose, and vice-versa

Building from source

To build the dev branch, which tracks the dev branch of Microsoft.Extensions.Logging, you must add https://www.myget.org/F/aspnetvnext/ to your package sources.

Credits

This package evolved from an earlier package Microsoft.Framework.Logging.Serilog provided by the ASP.NET team.

About

Serilog provider for Microsoft.Framework.Logging

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 84.7%
  • PowerShell 15.3%