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

Minimum level not effective using ILoggingBuilder configuration #22

Closed
cilerler opened this issue Aug 22, 2017 · 15 comments
Closed

Minimum level not effective using ILoggingBuilder configuration #22

cilerler opened this issue Aug 22, 2017 · 15 comments

Comments

@cilerler
Copy link

cilerler commented Aug 22, 2017

Make sure you are looking for non-default levels which are Debug and Trace

by the way same code with 1.6 working just fine.
you have to comment serviceCollection.AddLogging(loggingBuilder => loggingBuilder.AddSeq(seqSection));
and uncomment the ones starting with //x)

and last need to change package versions

    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />

Program.cs

using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ConsoleApp1
{
	internal class Program
	{
		private static void Main(string[] args)
		{
			IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
			                                                             .AddJsonFile("appsettings.json", true, true)
			                                                             .Build();
			IServiceCollection serviceCollection = new ServiceCollection();
			IConfigurationSection seqSection = configuration.GetSection("Seq");
			serviceCollection.AddLogging(loggingBuilder => loggingBuilder.AddSeq(seqSection));
			//x serviceCollection.AddLogging();
			IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

			//x ILoggerFactory loggerFactory = serviceProvider.GetService<ILoggerFactory>();
			//x loggerFactory.AddSeq(seqSection);

			Seq.Extensions.Logging.SelfLog.Enable(Console.Out);

			var logger = serviceProvider.GetService<ILogger<Program>>();

			using (logger.BeginScope("{ApplicationName} :: {ApplicationGuid}", nameof(Program), Guid.NewGuid()))
			{
				logger.LogTrace("Trace");
				logger.LogDebug("Debug");
				logger.LogInformation("Information");
			}

			Console.WriteLine($"KEY: {seqSection.Key}{Environment.NewLine}VALUE: {seqSection.Value}");
			Console.ReadLine();
		}
	}
}

appsettings.json

{
  "Seq": {
    "ServerUrl": "http://localhost:5341",
    "MinimumLevel": "Trace",
    "LevelOverride": {
      "Default": "Trace"
    }
  }
}

ConsoleApp1.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.2" />
    <PackageReference Include="Seq.Extensions.Logging" Version="3.0.0" />
  </ItemGroup>

</Project>
@nblumhardt
Copy link
Member

Hi Cengiz,

Turns out, to my surprise, you must call SetMinimumLevel() on the ILoggingBuilder to drop the global logging level, first:

                loggingBuilder.SetMinimumLevel(LogLevel.Trace);
                loggingBuilder.AddSeq(Configuration.GetSection("Seq"));

I don't know whether this is really the expected usage pattern, so digging in a little further.

@pakrym, is there any blog post or doc on how minimum level is managed in v2?

Cheers!

@nblumhardt nblumhardt changed the title Configuration load from JSON is no longer working Minimum level not effective using ILoggingBuilder configuration Aug 22, 2017
@nblumhardt
Copy link
Member

(Especially - how should this work with level overrides? If I bump up the logging level for "Some.Component" to Trace, does this mean the global level now needs to be set to Trace?)

@pakrym
Copy link

pakrym commented Aug 22, 2017

@nblumhardt https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x
(How filtering rules are applied)

Minimum level is only used when there are no rules matched at all, so if you set Some.Component to Trace it would work fine with any minimum level.

@nblumhardt
Copy link
Member

@pakrym thanks for the follow-up!

Does that indicate we're hitting a bug here, then? In my test app, I have the Seq logger set to Trace, and level overrides defined in it for Microsoft and System, but still the global logging level of Information overrides everything else...

@pakrym
Copy link

pakrym commented Aug 22, 2017

@nblumhardt do you have loggingBuilder.AddConfiguration(config) call?

@nblumhardt
Copy link
Member

Do now, still not working as I'd expect, but more to dig through.

Currently going through the uncomfortable realization that, although there are technically no breaking API changes in 2.0, the entire configuration model used by the Serilog provider, Seq provider, and File provider is now completely broken?

Hacking in something like:

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information"
    },
    "Seq": {
      "LogLevel": {
        "Default": "Trace",
        "System": "Information",
        "Microsoft": "Warning"
      }
    }
  },

Does the framework recognize custom provider names here?

@pakrym
Copy link

pakrym commented Aug 22, 2017

If you put ProviderAlias attribute on you provider type, it's late bound so you don't need a package reference (see microsoft/ApplicationInsights-aspnetcore@205d73c)

@nblumhardt
Copy link
Member

Thanks for the tip. Without ProviderAlias, the provider won't work, then?

@pakrym
Copy link

pakrym commented Aug 22, 2017

@nblumhardt full provider type name would work.

@nblumhardt
Copy link
Member

👍 thanks.

Any way we can get access to the logging configuration block, e.g. so that we can include custom data? E.g. ServerUrl in:

  "Logging": {
    "Seq": {
      "ServerUrl": "http://xyz",
      "LogLevel": {
        "Default": "Trace"
      }
    }
  },

@pakrym
Copy link

pakrym commented Aug 22, 2017

aspnet/Logging#688

Will be there for 2.1

@cilerler
Copy link
Author

4.0.0-dev looks just fine 👍 passed my all tests ✔️ Thank you 🤗 @nblumhardt @pakrym 🍪 ☕️

@pakrym
Copy link

pakrym commented Sep 21, 2017

@nblumhardt we added a way to access provider configuration section in logging configuration: aspnet/Logging#706

@nblumhardt
Copy link
Member

Thanks @pakrym, will take a look 👍

@nblumhardt
Copy link
Member

Closing this as I think it's now stale; will open a new ticket for configuration section support

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

3 participants