datalust / seq-extensions-logging

Add centralized log collection to ASP.NET Core apps with one line of code.
https://datalust.co/seq
Apache License 2.0
84 stars 12 forks source link

Minimum level not effective using `ILoggingBuilder` configuration #22

Closed cilerler closed 5 years ago

cilerler commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

(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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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

nblumhardt commented 6 years ago

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 commented 6 years ago

If you put ProviderAlias attribute on you provider type, it's late bound so you don't need a package reference (see https://github.com/Microsoft/ApplicationInsights-aspnetcore/commit/205d73c1ed9f390455c9d7739c9aa1c9e0335dd5)

nblumhardt commented 6 years ago

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

pakrym commented 6 years ago

@nblumhardt full provider type name would work.

nblumhardt commented 6 years ago

👍 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 commented 6 years ago

https://github.com/aspnet/Logging/issues/688

Will be there for 2.1

cilerler commented 6 years ago

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

pakrym commented 6 years ago

@nblumhardt we added a way to access provider configuration section in logging configuration: https://github.com/aspnet/Logging/pull/706

nblumhardt commented 6 years ago

Thanks @pakrym, will take a look 👍

nblumhardt commented 5 years ago

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