1

I am implementing a custom SQL Server implementation of Microsoft.Extensions.Logging.ILogger. The NuGet package states that it is compatible with .NET Standard 2.0, so I added a class library to my solution targeting .NET Standard 2.0.

Using Visual Studio 2022, I added a new class implementing the ILogger interface and had Visual Studio implement the interface explicitly via the Quick Actions and Refactoring menu. It resulted in this code:

using Microsoft.Extensions.Logging;

internal class Foo : ILogger
{
    public IDisposable BeginScope<TState>(TState state) where TState : notnull
    { //                             (red squiggly line shows up here) ^^^^^^^
        throw new NotImplementedException();
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        throw new NotImplementedException();
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
    {
        throw new NotImplementedException();
    }
}

I am getting a compiler error with the notnull type constraint on the BeginScope method:

Error CS8370 - Feature 'notnull generic type constraint' is not available in C# 7.3. Please use language version 8.0 or greater.

My assumption was that I could create a custom implementation of ILogger in .NET Standard 2.0 since the NuGet package said it supports .NET Standard 2.0. A screenshot of the supported frameworks page for Microsoft.Extensions.Logging version 8.0.0 at NuGet.org is below:

Screenshot of NuGet.org page for Microsoft.Extensions.Logging version 8.0 listing the supported frameworks, clearly showing that .NET Standard 2.0 is supported.

My assumption must be incorrect. Maybe the target framework stated in the NuGet package pertains to using the package in an application rather than implementing an interface in the NuGet package?

Is there a way to implement Microsoft.Extensions.Logging.ILogger in a .NET Standard 2.0 project, or do I just need to target .NET 8+?

3
  • what happens when you set the langversion in the csproj? <LangVersion>latest</LangVersion> or is that not an option ? Commented Jul 11, 2024 at 14:46
  • Or just remove the notnull constraint (though I would recommend using a newer version of C# as Jeb suggested) Commented Jul 11, 2024 at 14:46
  • @DavidG: removing the type constraint completely did the trick. Commented Jul 11, 2024 at 15:02

1 Answer 1

0

Resolving this was deceptively easy. Just remove the type constraint from BeginScope. The notnull type constraint appears to be an artifact of the Visual Studio 2022 quick action to implement the ILogger interface rather than a problem with the underlying frameworks or compiler. This solution would probably work for any case where Visual Studio adds a notnull type constraint when implementing an interface explicitly and you are targeting an older, but compatible framework version.

So, change the method signature from this:

public IDisposable BeginScope<TState>(TState state) where TState : notnull
{

To this:

public IDisposable BeginScope<TState>(TState state)
{

Notice that there is no where : TState notnull after the closing parenthesis for the method parameters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.