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.
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
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 SerilogLoggerConfiguration
and theILoggerFactory
- Serilog and ASP.NET assign different priorities to the
Debug
andVerbose
levels; Serilog'sDebug
is ASP.NET'sVerbose
, and vice-versa
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.
This package evolved from an earlier package Microsoft.Framework.Logging.Serilog provided by the ASP.NET team.