Giter Site home page Giter Site logo

merbla / serilog-extensions-logging-file Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serilog/serilog-extensions-logging-file

0.0 1.0 0.0 591 KB

Add file logging to ASP.NET Core apps in one line of code.

License: Apache License 2.0

PowerShell 4.56% C# 93.22% CSS 2.10% JavaScript 0.11%

serilog-extensions-logging-file's Introduction

Serilog.Extensions.Logging.File NuGet Pre Release Join the chat at https://gitter.im/serilog/serilog Build status

This package makes it a one-liner - loggerFactory.AddFile() - to configure top-quality file logging for ASP.NET Core apps.

  • Text or JSON file output
  • Files roll over on date; capped file size
  • Request ids and event ids included with each message
  • Writes are performed on a background thread
  • Files are periodically flushed to disk (required for Azure App Service log collection)
  • Fast, stable, battle-proven logging code courtesy of Serilog

You can get started quickly with this package, and later migrate to the full Serilog API if you need more sophisticated log file configuration.

Getting started

1. Add the NuGet package as a dependency of your project either with the package manager or directly to the CSPROJ file:

<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />

2. In your Program class, configure logging on the web host builder, and call AddFile() on the provided loggingBuilder.

WebHost.CreateDefaultBuilder(args)
    .ConfigureLogging((hostingContext, builder) =>
    {
        builder.AddFile("Logs/myapp-{Date}.txt");
    })
    .UseStartup<Startup>()
    .Build();

Done! The framework will inject ILogger instances into controllers and other classes:

class HomeController : Controller
{
    readonly ILogger<HomeController> _log;

    public HomeController(ILogger<HomeController> log)
    {
        _log = log;
    }

    public IActionResult Index()
    {
        _log.LogInformation("Hello, world!");
    }
}

The events will appear in the log file:

2016-10-18T11:14:11.0881912+10:00 0HKVMUG8EMJO9 [INF] Hello, world! (f83bcf75)

File format

By default, the file will be written in plain text. The fields in the log file are:

Field Description Format Example
Timestamp The time the event occurred. ISO-8601 with offset 2016-10-18T11:14:11.0881912+10:00
Request id Uniquely identifies all messages raised during a single web request. Alphanumeric 0HKVMUG8EMJO9
Level The log level assigned to the event. Three-character code in brackets [INF]
Message The log message associated with the event. Free text Hello, world!
Event id Identifies messages generated from the same format string/message template. 32-bit hexadecimal, in parentheses (f83bcf75)
Exception Exception associated with the event. Exception.ToString() format (not shown) System.DivideByZeroException: Attempt to divide by zero\r\n\ at...

To record events in newline-separated JSON instead, specify isJson: true when configuring the logger:

loggingBuilder.AddFile("Logs/myapp-{Date}.txt", isJson: true);

This will produce a log file with lines like:

{"@t":"2016-06-07T03:44:57.8532799Z","@m":"Hello, world!","@i":"f83bcf75","RequestId":"0HKVMUG8EMJO9"}

The JSON document includes all properties associated with the event, not just those present in the message. This makes JSON formatted logs a better choice for offline analysis in many cases.

Rolling

The filename provided to AddFile() should include the {Date} placeholder, which will be replaced with the date of the events contained in the file. Filenames use the yyyyMMdd date format so that files can be ordered using a lexicographic sort:

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

To prevent outages due to disk space exhaustion, each file is capped to 1 GB in size. If the file size is exceeded, events will be dropped until the next roll point.

Message templates and event ids

The provider supports the templated log messages used by Microsoft.Extensions.Logging. By writing events with format strings or message templates, the provider can infer which messages came from the same logging statement.

This means that although the text of two messages may be different, their event id fields will match, as shown by the two "view" logging statements below:

2016-10-18T11:14:26.2544709+10:00 0HKVMUG8EMJO9 [INF] Running view at "/Views/Home/About.cshtml". (9707eebe)
2016-10-18T11:14:11.0881912+10:00 0HKVMUG8EMJO9 [INF] Hello, world! (f83bcf75)
2016-10-18T11:14:26.2544709+10:00 0HKVMUG8EMJO9 [INF] Running view at "/Views/Home/Index.cshtml". (9707eebe)

Each log message describing view rendering is tagged with (9707eebe), while the "hello" log message is given (f83bcf75). This makes it easy to search the log for messages describing the same kind of event.

Additional configuration

The AddFile() method exposes some basic options for controlling the connection and log volume.

Parameter Description Example value
pathFormat Filname to write. The filename may include {Date} to specify how the date portion of the filename is calculated. May include environment variables. Logs/log-{Date}.txt
minimumLevel The level below which events will be suppressed (the default is LogLevel.Information). LogLevel.Debug
levelOverrides A dictionary mapping logger name prefixes to minimum logging levels.
isJson If true, the log file will be written in JSON format. true
fileSizeLimitBytes The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, passnull. The default is 1 GiB. 1024 * 1024 * 1024
retainedFileCountLimit The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass null. The default is 31. 31

appsettings.json configuration

The file path and other settings can be read from JSON configuration if desired.

In appsettings.json add a "Logging" property:

{
  "Logging": {
    "PathFormat": "Logs/log-{Date}.txt",
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Information"
    }
  }
}

And then pass the configuration section to the AddFile() method:

loggingBuilder.AddFile(Configuration.GetSection("Logging"));

In addition to the properties shown above, the "Logging" configuration supports:

Property Description Example
Json If true, the log file will be written in JSON format. true
FileSizeLimitBytes The maximum size, in bytes, to which any single log file will be allowed to grow. For unrestricted growth, passnull. The default is 1 GiB. 1024 * 1024 * 1024
RetainedFileCountLimit The maximum number of log files that will be retained, including the current log file. For unlimited retention, pass null. The default is 31. 31

Using the full Serilog API

This package is opinionated, providing the most common/recommended options supported by Serilog. For more sophisticated configuration, using Serilog directly is recommened. See the instructions in Serilog.AspNetCore to get started.

The following packages are used to provide AddFile():

If you decide to switch to the full Serilog API and need help, please drop into the Gitter channel or post your question on Stack Overflow.

serilog-extensions-logging-file's People

Contributors

nblumhardt avatar poke avatar kellystuard avatar micdenny 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.