Coder Social home page Coder Social logo

sonaljain14 / serilog-sinks-log4net Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serilog-archive/serilog-sinks-log4net

0.0 0.0 0.0 36 KB

A Serilog sink that writes events to log4net

License: Apache License 2.0

PowerShell 20.40% C# 79.60%

serilog-sinks-log4net's Introduction

Serilog.Sinks.Log4Net

Build status NuGet Version

Duplicates Serilog events through the log4net pipeline to allow integration with existing code and libraries.

Package - Serilog.Sinks.Log4Net | Platforms - .NET 4.5

var log = new LoggerConfiguration()
    .WriteTo.Log4Net()
    .CreateLogger();

Context Message Marker

Passing true for supplyContextMessage, will add a context message of Serilog-Log4NetSink scoped just for the log call under the NDC stack, which you can utilise for Log4Net filters and other purposes. See Log4Net documentation.

e.g.

var log = new LoggerConfiguration()
    .WriteTo.Log4Net(supplyContextMessage: true)
    .CreateLogger();

This can let you specify a filter on an appender in Log4Net. The example below, disables the Seq appender for Log4Net if the context message is present. Useful to stop doubling up logs, for when you have Log4Net and Serilog both pushing to the same sink during migration of logging practices.

<log4net>
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%newline%-5level %logger %newline - %message" />
    </layout>
  </appender>
  <appender name="SeqAppender" type="Seq.Client.Log4Net.SeqAppender, Seq.Client.Log4Net">
    <filter type="log4net.Filter.PropertyFilter">
      <key value="NDC" />
      <stringToMatch value="Serilog-Log4NetSink" />
      <acceptOnMatch value="false" />
    </filter>
    <bufferSize value="1" />
    <serverUrl value="http://localhost:5341/" />
  </appender>
  <root>
    <level value="All" />
    <appender-ref ref="TraceAppender" />
    <appender-ref ref="SeqAppender" />
  </root>
</log4net>

Preserving Stacktrace information

Passing 3 (Default is 3) for skipFrames, will load the relative number of frames from the call stack. This is required to be configurable to allow the caller class, method name, line number to be pulled. This is best used in scenarios where you have your own custom wrapper over Serilog or log4net.

e.g.

var log = new LoggerConfiguration()
    .WriteTo.Log4Net(skipFrames: 3)
    .CreateLogger();

This change allows the correct values returned for log4net conversion template.

<conversionPattern value="[%utcdate{yyyy-MM-ddTHH:mm:ssZ}}][%level][%thread][%C(%M),%line][%message]%newline"/>

Output received in logs when skipFrames is 3

[2017-12-19T16:51:53Z}][INFO][1][Serilog.Sinks.Log4Net.Sample.Program(Main),39][SERILOG-custom property added for "ikson01" { firstname: "john", lastname: "doe" }]

Output received in logs when skipFrames is 0

[2017-12-19T16:53:12Z}][INFO][1][Serilog.Core.Sinks.SafeAggregateSink(Emit),0][SERILOG-custom property added for "ikson01" { firstname: "john", lastname: "doe" }]

(More information.)

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.