Giter Site home page Giter Site logo

merbla / serilog-extensions-logging Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serilog/serilog-extensions-logging

0.0 1.0 0.0 1.38 MB

Serilog provider for Microsoft.Extensions.Logging

License: Apache License 2.0

PowerShell 3.08% C# 96.31% Shell 0.60%

serilog-extensions-logging's Introduction

Serilog.Extensions.Logging Build status NuGet Version

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

ASP.NET Core 2.0+ Instructions

ASP.NET Core 2.0 applications should prefer Serilog.AspNetCore and UseSerilog() instead.

.NET Core 1.0, 1.1 and Default Provider Integration

The package implements AddSerilog() on ILoggingBuilder and ILoggerFactory to enable the Serilog provider under the default Microsoft.Extensions.Logging implementation.

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.Console writes these to the console.

Install-Package Serilog.Extensions.Logging -DependencyVersion Highest
Install-Package Serilog.Sinks.Console

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

using Serilog;

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

Finally, for .NET Core 2.0+, in your Startup class's Configure() method, remove the existing logger configuration entries and call AddSerilog() on the provided loggingBuilder.

  public void ConfigureServices(IServiceCollection services)
  {
      services.AddLogging(loggingBuilder =>
      	loggingBuilder.AddSerilog(dispose: true));
      
      // Other services ...
  }

For .NET Core 1.0 or 1.1, in your Startup class's Configure() method, remove the existing logger configuration entries and call AddSerilog() on the provided loggerFactory.

  public void Configure(IApplicationBuilder app,
                        IHostingEnvironment env,
                        ILoggerFactory loggerfactory,
                        IApplicationLifetime appLifetime)
  {
      loggerfactory.AddSerilog();
      
      // Ensure any buffered events are sent at shutdown
      appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);

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

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

Credits

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

serilog-extensions-logging's People

Contributors

nblumhardt avatar gertjvr avatar merbla avatar adamchester avatar hhog-pkamburov avatar adamvoss avatar bbrandt avatar neman avatar wlo023 avatar sensarg22 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.