Giter Site home page Giter Site logo

szilard-talian / serilog-enrichers-thread Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serilog/serilog-enrichers-thread

0.0 0.0 0.0 72 KB

Enrich Serilog events with properties from the current thread.

License: Apache License 2.0

C# 88.45% PowerShell 11.55%

serilog-enrichers-thread's Introduction

Serilog.Enrichers.Thread Build status NuGet Version

Enrich Serilog events with properties from the current thread.

WithThreadName() is only supported in .NetStandard 2.0 and .NetFramework 4.5.

Getting started

Install the package from NuGet:

Install-Package Serilog.Enrichers.Thread

In your logger configuration, apply Enrich.WithThreadId() and Enrich.WithThreadName():

Log.Logger = new LoggerConfiguration()
    .Enrich.WithThreadId()
    .Enrich.WithThreadName()
    .CreateLogger();

Many sinks simply include all properties without further action required, so the thread id will be logged automatically. However, some sinks, such as the File and Console sinks use an output template and the new ThreadId may not be automatically output in your sink. In this case, in order for the ThreadId or ThreadName to show up in the logging, you will need to create or modify your output template.

w.File(...., outputTemplate:
  "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}")

Here, {Properties} can include not only ThreadId and ThreadName, but any other enrichment which is applied. Alternatively, {ThreadId} could be used instead, if you want to only add the thread id enrichment and {ThreadName}, if you want to only add the thread name enrichment.

An example, which also uses the Serilogs.Sinks.Async Nuget package, is below:

            Thread.CurrentThread.Name = "MyWorker";
              
            var logger = Log.Logger = new LoggerConfiguration()
                 .MinimumLevel.Debug()
                 .WriteTo.Console(restrictedToMinimumLevel:Serilog.Events.LogEventLevel.Information)
                 .WriteTo.Async(w=>w.File("..\\..\\..\\..\\logs\\SerilogLogFile.json", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj} <{ThreadId}><{ThreadName}>{NewLine}{Exception}"))
                 .Enrich.WithThreadId()
                 .CreateLogger();

Which would produce an output in the log file as follows:

2018-04-06 13:12:45.684 +02:00 [ERR] The file file_name.svg does not exist <4><MyWorker>

Where, <4> is an example thread id and <MyWorker> is an example thread name.

To use the enricher, first install the NuGet package:

Install-Package Serilog.Enrichers.Thread

Note: The {ThreadName} property will only be attached when it is not null. Otherwise it will be omitted. If you want to get this property always attached you can use the following:

using Serilog.Enrichers;

Log.Logger = new LoggerConfiguration()
    .Enrich.WithThreadName()
    .Enrich.WithProperty(ThreadNameEnricher.ThreadNamePropertyName, "MyDefault")
    .CreateLogger();

The enrichment order is important. Otherwise "MyDefault" would always win.

Copyright © 2016 Serilog Contributors - Provided under the Apache License, Version 2.0.

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.