Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Framework: Configuring Enrichers from json does not work #122

Closed
2 of 7 tasks
aFuerst opened this issue Jul 19, 2018 · 10 comments
Closed
2 of 7 tasks

.NET Framework: Configuring Enrichers from json does not work #122

aFuerst opened this issue Jul 19, 2018 · 10 comments

Comments

@aFuerst
Copy link

aFuerst commented Jul 19, 2018

Does this issue relate to a new feature or an existing bug?

  • Bug
  • New Feature

What version of Serilog is affected? Please list the related NuGet package.
Serilog, Version=2.0.0.0
Serilog.Enrichers.Process, Version=2.0.0.0
Serilog.Enrichers.Thread, Version=2.0.0.0
Serilog.Enrichers.Environment, Version=2.0.0.0
Serilog.Settings.Configuration, Version=2.6.1.0

What is the target framework and operating system? See target frameworks & net standard matrix.

  • netCore 2.0
  • netCore 1.0
  • 4.7
  • 4.6.x
  • 4.5.x

Please describe the current behavior?
Enrichers configured via json configuration are not applied.
The created logger has an empty enricher.

Please describe the expected behavior?
The enrichers specified in the json config should be applied to the created logger and the data they are supposed to add gets put in the logs

If the current behavior is a bug, please provide the steps to reproduce the issue and if possible a minimal demo of the problem
This is the config section I have

"Serilog": {
	"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId", "WithExceptionDetails" ],
	"MinimumLevel": "Verbose",
	"WriteTo": [
		{
			"Name": "Trace",
			"Args": {
				"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
			}
		}
	],
	"Using": [ "Serilog.Sinks.Trace" ]
}

We're using the full .NET Framework.
The logger creation looks like this. We load it entirely from the IConfiguration object.

var logger = new LoggerConfiguration()
	.ReadFrom.Configuration(config)
	.CreateLogger();
@merbla
Copy link
Contributor

merbla commented Jul 19, 2018

@aFuerst are you missing a using in the settings for `Serilog.Enrichers.Environment?

"Using": [ "Serilog.Sinks.Trace", "Serilog.Enrichers.Environment" ]

@aFuerst
Copy link
Author

aFuerst commented Jul 20, 2018

@merbla That was it. Thank you!
I didn't realize I needed to add the namespaces to the Using since you didn't do that in the example in the ReadMe

@aFuerst aFuerst closed this as completed Jul 20, 2018
@tsimbalar
Copy link
Member

Actually the README says :

(This package implements a convention using DependencyContext to find any package with Serilog anywhere in the name and pulls configuration methods from it, so the Using example above is redundant.)

So we are actually saying that "Using" is not necessary for Serilog.* libraries, and the following code does it :

AssemblyName[] GetSerilogConfigurationAssemblies()
{
// ReSharper disable once RedundantAssignment
var query = Enumerable.Empty<AssemblyName>();
var filter = new Func<string, bool>(name => name != null && name.ToLowerInvariant().Contains("serilog"));

So there is either a bug or an incorrect documentation issue here :)

@MV10
Copy link
Contributor

MV10 commented Jul 20, 2018

Is there a test for that? Sounds like we need one if not.

@tsimbalar
Copy link
Member

tsimbalar commented Jul 20, 2018

The sample app does not specify "Using" for WriteTo.Async or Enrich.WithMachineName that live in separate Nuget packages, so something is slightly wrong somewhere ....

@tsimbalar tsimbalar reopened this Jul 20, 2018
@marco-gallinari
Copy link

marco-gallinari commented Jul 25, 2018

We are experiencing the same issue adding serilog to a 4.6.1 web project via ninject.
The project is hosting a suite of wcf services, and we need to inject netstandard dependencies from other .net core projects.

The library is not loading enrichers nor sinks (File and Seq).

         kernel.Bind<ILoggerFactory>().ToMethod(context => {

                var conf = context.Kernel.Get<IConfiguration>();


                var loggerConfig = new LoggerConfiguration();

                loggerConfig.ReadFrom.Configuration(conf);

                return new LoggerFactory().AddSerilog(loggerConfig.CreateLogger(), true);

            }).InSingletonScope();
            kernel.Bind(typeof(ILogger<>)).To(typeof(Logger<>)).InSingletonScope();

It only works as soon as we add all namespaces (enrichers and sinks) to the "Using" property.

This could be an issue with the framework version, because the same json configuration works properly on our .net core projects.

@nblumhardt
Copy link
Member

Ah, yes, the "Using" blocks are only redundant in Core, IIRC the dependency context works differently on Framework :-/

@MV10 MV10 changed the title Configuring Enrichers from json does not work .NET Framework: Configuring Enrichers from json does not work Aug 29, 2018
skomis-mm pushed a commit that referenced this issue Jan 7, 2019
DllScanningAssemblyFinder fixes (#157, #150, #122, #156)
@skomis-mm
Copy link
Contributor

fixed by #159

@mikeblas
Copy link

mikeblas commented Dec 8, 2019

I'm experiencing this same issue. I'm using Core 3.1, though, so maybe something is broken in a different way than was previous fixed.

My config file has these lines that seem relevant:

    "Using": [ "Serilog.Sinks.Console", "Serilog.Enrichers.Thread", "Serilog.Enrichers.Environment", "Serilog.Sinks.File" ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],

The rest of my configuration seems to work, so I'm not sure what else I might check. I don't notice any error messages or exceptions ...

@skomis-mm
Copy link
Contributor

Hi, @mikeblas . Just checked with 3.1 - works as expected. Can you provide your setup or minimal code that reproduce this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants