65

I upgraded my project to .NET Core 2.2.x and got an obsolete warning regarding the following code - both lines:

public void Configure(IApplicationBuilder app, 
                      IHostingEnvironment env, 
                      ILoggerFactory loggerFactory) 
  {
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));

The suggestion to fix is The recommended alternative is AddConsole(this ILoggingBuilder builder). I thought that is what I am using.

What am I missing here?

James Curran
  • 101,701
  • 37
  • 181
  • 258
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • A `LoggerFactory` surely is not a `LoggingBuilder`, is it? – MakePeaceGreatAgain Dec 18 '18 at 20:11
  • 2
    Logging is set up on the `WebHost` (and has been since 2.0 I believe). See the [docs](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.2). You get console logging for free if you're using `WebHost.CreateDefaultBuilder`. – Kirk Larkin Dec 18 '18 at 20:15

6 Answers6

111

I had the same issue today.

Remove your logging configuration from Startup.cs and go to your Program.cs file and add something like:

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
    })
    .Build();

This used the 'builder' because the variable 'logging' is an IloggingBuilder (whereas your code is still using ILoggerFactory)

UPDATE: The other method I just tried is to stay inside Startup.cs but move the logging stuff from the 'Configure' method to 'ConfigureServices' like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging(loggingBuilder =>
    {
        loggingBuilder.AddConfiguration(Configuration.GetSection("Logging"));
        loggingBuilder.AddConsole();
        loggingBuilder.AddDebug();
    });
}

Perhaps keeps the Program.cs less polluted...

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
Michael Ceber
  • 2,304
  • 1
  • 11
  • 14
  • Is this even necessary (I mean `AddLogging`)?? I just created new Web API project, didn't do anything of this. I simply inject `ILogger logger` via the parameter and can log without a problem (in the Console obviously). So I'm wondering... why `AddConsole`? – Ish Thomas May 20 '20 at 22:09
  • How do you do the ConfigureServices method when you're trying to configure the logging of a Class Library from the actual program? – jeancallisti Oct 15 '21 at 13:42
22

The documentation's recommendation to use AddConsole(this ILoggingBuilder builder) is correct, but for that to work you need to add a reference to the NuGet package Microsoft.Extensions.Logging.Console.

x5657
  • 1,172
  • 2
  • 13
  • 26
16

I got the same warning when I was updating logging code from .Net Core 2.1 to 3.0. The recommended way to do the upgrade is documented on MSDN.

In my case, I was trying to get an instance of LoggerFactory for console which is pretty straightforward in .Net Core 3.0:

using (var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()))
{
    // use loggerFactory
}
meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • 2
    The best answer as it's not tight to ASP.NET Core DI infrastructure. The accepted one leaves the full framework outside the scope. – d_f Feb 27 '20 at 18:51
  • 1
    Exactly. I had to update my logging extension nuget for another dependency resolution and suddenly it breaks and leaves me thinking "where did you move it this time". This answers it succinctly. – Ran Sagy May 21 '20 at 21:56
  • it doesn't work in .NET 5. Where is no method AddConsole in ILoggerFactory. – Alex78191 Dec 18 '21 at 15:49
9

Don't worry about it - this is the dumbest thing ever!

Note

The following code sample uses a ConsoleLoggerProvider constructor that has been obsoleted in version 2.2. Proper replacements for obsolete logging APIs will be available in version 3.0. In the meantime, it is safe to ignore and suppress the warnings.

In case you thought you forgot what Obsolete meant - you hadn't! Don't worry about it and just ignore it for now - or suppress the warning (sorry I don't have the code for that to hand).

(Wish they'd put a better explanation for why this was done - that's what I mean by dumb.)

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • 3
    Not sure the new answer for this since 3.0 is now out - so if you're using 3.0 then this answer itself is now obsolete ;-) – Simon_Weaver Nov 20 '19 at 18:06
3

According to the issue opened on GitHub for this, the replacement methods are already being called if you use CreateDefaultBuilder() method in your Program.cs.

https://github.com/aspnet/Docs/issues/9829

The only issue I have is that I only turned these on for non-Production environments.. and don't see a way to do so going forward.

tommytarheel
  • 121
  • 4
1

If you don't have access to the LoggerFactory.Create(), use can still use the ILoggerFactory with the AddProvider() method giving it a ConsoleLoggerProvider() but it is a bit of a pain if you want to do something simple. The problem is, ConsoleLoggerProvider() requires a IOptionsMonitor<ConsoleLoggerOptions> as a parameter and the easiest thing to do if you

  • you don't have access to the options mechanism in your code base (my problem), or
  • the real options mechanisms in your existing code base don't match up with IOptionsMonitor<>, or
  • you have other reasons not to use the ASP.Net options facilities

is to create a dummy class:

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Options;
    
    class DummyConsoleLoggerOptionsMonitor : IOptionsMonitor<ConsoleLoggerOptions>
    {
        private readonly ConsoleLoggerOptions option = new ConsoleLoggerOptions();

        public DummyConsoleLoggerOptionsMonitor(LogLevel level)
        {
            option.LogToStandardErrorThreshold = level;
        }

        public ConsoleLoggerOptions Get(string name)
        {
            return this.option;
        }

        public IDisposable OnChange(Action<ConsoleLoggerOptions, string> listener)
        {
            return new DummyDisposable();
        }

        public ConsoleLoggerOptions CurrentValue => this.option;

        private sealed class DummyDisposable : IDisposable
        {
            public void Dispose()
            {
            }
        }
    }

You can then use your ILoggerFactory like:

factory.AddProvider(
    new ConsoleLoggerProvider(
        new DummyConsoleLoggerOptionsMonitor(LogLevel.Debug)));
mheyman
  • 4,211
  • 37
  • 34