question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Microsoft.NET.Sdk.Web global using statements break Serilog/NLog/log4net consumers on upgrade

See original GitHub issue

From https://github.com/dotnet/templating/issues/3359#issuecomment-914693605

The .NET 6 version of Microsoft.NET.Sdk.Web introduces a default global using statement for Microsoft.Extensions.Logging. This brings MEL’s ILogger into scope everywhere, breaking existing code that uses Serilog’s (or another framework’s) ILogger interface, with errors like:

/home/pi/dl/nix/seq/tmp/ubuntu.20.04-arm64/src/Seq/Server/Tasks/TaskRunner.cs(15,18): error CS0104: 'ILogger' is an ambiguous reference between 'Microsoft.Extensions.Logging.ILogger' and 'Serilog.ILogger' [/home/pi/dl/nix/seq/tmp/ubuntu.20.04-arm64/src/Seq/Seq.csproj]

The breakage can be worked around by adding <EnableDefaultGlobalUsing>false</EnableDefaultGlobalUsing> or similar to the project’s CSPROJ file, but this is a pretty nasty papercut that penalizes users for choosing a logging library other than Microsoft’s. The chore will be particularly onerous because of the number of projects affected (and volume of errors within these projects).

I’d also expect that many users will turn to the Serilog project asking for guidance or reporting the breakage as a bug, since the default assumption people make when encountering an issue like this is that “Serilog is broken on .NET 6” - which increases our support burden 😃

CC @DamianEdwards

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
davidfowlcommented, Sep 8, 2021

This might be another alternative:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Serilog.Extensions.Hosting" Version="4.1.2" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="Serilog.ILogger" Alias="ILogger" />
  </ItemGroup>

</Project>

OR

global using ILogger = Serilog.ILogger;

using Serilog;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseSerilog();
var app = builder.Build();

app.MapGet("/", (ILogger logger) => "Hello World!");

app.Run();

Tell customers to introduce a global alias for serilog’s ILogger as an override. (This can also be scoped to a file if that is too global).

2reactions
swythancommented, Sep 13, 2021

Note that some users (e.g. my team) use Serilog as an output system, but use Microsoft.Extensions.Logging abstractions for most of the actual logging. It would be confusing to deal with the NuGet reference to Serilog causing ILogger to be switched from one definition to the other.

That said; I’ll probably be disabling global usings everywhere, anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate from ASP.NET Core 5.0 to 6.0
Uses global using directives to eliminate or minimize the number of using statement lines required. The following code displays the Startup.cs ...
Read more >
The SDK 'Microsoft.NET.Sdk' specified could not be found. ...
Since Visual Studio 2022 updated from version 17.4 to 17.5, I couldn't load my project. and get the error "The SDK 'Microsoft.NET.
Read more >
MSBuild reference for .NET SDK projects
Use the NetStandardImplicitPackageVersion property when you want to specify a framework version that's lower than the metapackage version. The ...
Read more >
Migrate from ASP.NET Core 3.1 to 6.0
Migrate from ASP.NET Core 3.1 to 6.0 · In this article · Prerequisites · Update .NET SDK version in global.json · Update the...
Read more >
C# 10.0 implicit global using directives
C# 10's new implicit global imports can make your code more communicative. This article shows how the magic works, and how you can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found