Giter Site home page Giter Site logo

common.logging.serilog's Introduction

Common Logger <-> Serilog

NuGet version Build status

Provides a bridge from the old to the new logging systems.

Usage

Configure app.config/web.config file of your project:

<configSections>
	<sectionGroup name="common">
		<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
	</sectionGroup>
</configSections>

<common>
	<logging>
  		<factoryAdapter type="Common.Logging.Serilog.SerilogFactoryAdapter, Common.Logging.Serilog" />
	</logging>
</common>

You must configure and provide a global logger to use this adapter:

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

// set global instance of Serilog logger which Common.Logger.Serilog requires.
Log.Logger = log;

Links

common.logging.serilog's People

Contributors

arjenvanderende avatar caleb-vear avatar imperugo avatar jaben avatar oltronix avatar richardlawley avatar samfold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

common.logging.serilog's Issues

Common.Logging + Numeric Formating + ElasticSearch Sink => Nummeric fields in Elastic

A typical (I suppose?) set-up is using this Common Logging adapter for Serilog and then having Serilog sink to Elastic with the serilog-sinks-elasticsearch. However, looking at the source code in SerilogCommonLogger.cs, it looks like we're replacing a "numeric formatted string" (like This formatted string is {0}) with the "Serilog formatted string" (like This formatted string is {@0}), which will make fields.0 appear as an indexed field in Elastic.

There is an related issue with the elastic sink, but I think we could handle it pretty nicely here by simply evaluate all numeric formatted parameters in the WriteFormat method of SerilogCommonLogger.cs before passing them to the Serilog. This would be done by

  1. Look at the string format pattern, if does not start with {@ } replace it with the value of the arg
  2. If it starts with {@ } pass it down to the _logger.

Numeric strings are not correct formatted

There was a bug introduced in pull request #12 (Preformat numeric formatted arguments before passing string to Serilogger) that causes string with more than one numeric argument to be wrongly pre-formatted.

The reason is that we manipulate the string from left-to-right and at the same time uses the char indexes from the matcher in the original template string. If the arg's value has more chars than the formatter itself (which is very likely) we will insert the following matchers based on an old index rather than the new one. The solution is to change the string from right-to-left.

Serilog 2.0 update

Howdy!

I'm working on a PR to update this library to work with Serilog 2.

serilog/serilog#802

Let me know if I've been beaten to it, or if you see any issues.

Cheers!

Creating a logger by string name instead of type breaks serilog MinimumLevel.Override

When using the string overload of GetLogger the sourceContext is never set on the LogEvent, which causes issues with the MinimumLevel.Override("loggername", Level) feature available when configuring seriLog.

This complicates logging in cases where we are interested in lower level logs for some packages which are using the string overload to create their loggers. The issue is easily fixed by changing the "NamedContext" string in SerilogFactoryAdapter.cs, but I'm unsure if that could cause other issues?

public ILog GetLogger(string name)
        {
            return new SerilogCommonLogger(new SerilogInstanceWrapper(l =>
                 l.ForContext("NamedContext", name), _logger));
        }


public ILog GetLogger(string name)
        {
            return new SerilogCommonLogger(new SerilogInstanceWrapper(l => l.ForContext("SourceContext", name), _logger));
        }

Using FormatMessageHandler throws an 'System.FormatException: 'Input string was not in a correct format.''

Expectation
It should be expected when using the formatted log statement the result would be the same as when using the format message handler.

commonlogger.DebugFormat(templateString, arg) == commonLogger.Debug(m => m(templateString, arg))

But that is not the case and an exception is thrown (System.FormatException: 'Input string was not in a correct format.')

Reproduce
This behaver is reproducible and can be done by modifying a test with the below:
_commonLogger.DebugFormat(templateString, arg);
replace with
_commonLogger.Debug(m => m(templateString, arg));

The test in question can be found here: https://github.com/ChangemakerStudios/Common.Logging.Serilog/blob/master/Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs#L71

The test will fail with the following

System.FormatException: 'Input string was not in a correct format.'

Possible fix
Fixing this seems to be removing the below line
string formatted = string.Format(formatProvider, message, parameters);
found at
https://github.com/ChangemakerStudios/Common.Logging.Serilog/blob/master/src/SerilogCommonLogger.cs#L561

Outro
Last release from this project is some time ago, but I hope, my message will be seen and a fixed can be created.

Best regards

Update to Common.Logging 3.0

Since Common.Logging has been updated to 3.0.0, this library is no longer compatible. Could it be rebuilt against 3.0.0 please?

netstandard support

Both Serilog and Common.Logging has netstandard support.
Please add netstandard to this package.

Mismatching depenency version in nuspec file

I'm using the latest version of this package (4.3.6) and run into some problems when the adapter is wire up programmatically.

Steps to reproduce:

  1. Create an empty project and reference this package
  2. Wire up the SerilogFactoryAdapter programmatically
Common.Logging.LogManager.Adapter = new Common.Logging.Serilog.SerilogFactoryAdapter(Serilog.Log.Logger);

This results in the following build error

CS1705	Assembly 'Common.Logging.Serilog' with identity 'Common.Logging.Serilog, Version=4.3.6.0, Culture=neutral, PublicKeyToken=null' uses 'Common.Logging.Core, Version=3.4.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e' which has a higher version than referenced assembly 'Common.Logging.Core' with identity 'Common.Logging.Core, Version=3.4.0.0, Culture=neutral, PublicKeyToken=af08829b84f0328e'

I believe that the reason for this is that AppVeyor is instructed to use a nuspec that references 3.4.0 of Common.Logging.Core, while the csproj references 3.4.0.1 of the package.

Add support for nested contexts

Latest version of common.logging won't work with Serilog adapter:

System.TypeLoadException occurred
  HResult=0x80131522
  Message=Method 'get_NestedThreadVariablesContext' in type 'Common.Logging.Serilog.SerilogCommonLogger' from assembly 'Common.Logging.Serilog, Version=4.1.11.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
  Source=Common.Logging.Serilog
  StackTrace:
   at Common.Logging.Serilog.SerilogFactoryAdapter.GetLogger(Type type)

net-commons/common-logging#128

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.