Home > Software design >  Serilog, .NET 7, error NU1605 ("detected package downgrade") and Microsoft.NETCore.Targets
Serilog, .NET 7, error NU1605 ("detected package downgrade") and Microsoft.NETCore.Targets

Time:11-16

In a .NET 7 project, I use Serilog (the latest Serilog.Extensions.Logging.File v3.0.0) and get a bunch of errors NU1605 when trying to compile the project with dotnet publish:

Agent.csproj : error NU1605: Detected package downgrade: System.IO from 4.3.0 to 4.1.0. Reference the package directly from the project to select a different version. 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> Serilog.Sinks.File 3.2.0 -> System.IO.FileSystem 4.0.1 -> runtime.win.System.IO.FileSystem 4.3.0 -> System.IO (>= 4.3.0) 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> System.IO (>= 4.1.0)
Agent.csproj : error NU1605: Detected package downgrade: System.IO.FileSystem.Primitives from 4.3.0 to 4.0.1. Reference the package directly from the project to select a different version. 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> Serilog.Sinks.File 3.2.0 -> System.IO.FileSystem 4.0.1 -> runtime.win.System.IO.FileSystem 4.3.0 -> System.IO.FileSystem.Primitives (>= 4.3.0) 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> System.IO.FileSystem.Primitives (>= 4.0.1)
Agent.csproj : error NU1605: Detected package downgrade: System.Runtime.Handles from 4.3.0 to 4.0.1. Reference the package directly from the project to select a different version. 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> Serilog.Sinks.File 3.2.0 -> System.IO.FileSystem 4.0.1 -> runtime.win.System.IO.FileSystem 4.3.0 -> System.Runtime.Handles (>= 4.3.0) 
Agent.csproj : error NU1605:  Agent -> Serilog.Extensions.Logging.File 3.0.0 -> Serilog.Sinks.RollingFile 3.3.0 -> Serilog.Sinks.File 3.2.0 -> System.IO.FileSystem 4.0.1 -> System.Runtime.Handles (>= 4.0.1)

Upon some digging, I've found that adding the following to my .csproj file solves the problem:

    <PackageReference Include="Microsoft.NETCore.Platforms" Version="7.0.0" />
    <PackageReference Include="Microsoft.NETCore.Targets" Version="5.0.0" PrivateAssets="All" />

However, the Microsoft.NETCore.Targets package was stopped being updated at v6.0.0-preview.4.21253.7. Apparently, it has been deprecated, see the "Delete Microsoft.NETCore.Targets package" issue.

To troubleshoot it further, I've enabled "repeatable package restores using a lock file" (the right thing to do for CI builds anyway). Here's what I see in my packages.lock.json:

      "Serilog.Sinks.RollingFile": {
        "type": "Transitive",
        "resolved": "3.3.0",
        "contentHash": "2lT5X1r3GH4P0bRWJfhA7etGl8Q2Ipw9AACvtAHWRUSpYZ42NGVyHoVs2ALBZ/cAkkS tA4jl80Zie144eLQPg==",
        "dependencies": {
          "Serilog.Sinks.File": "3.2.0",
          "System.IO": "4.1.0",
          "System.IO.FileSystem.Primitives": "4.0.1",
          "System.Runtime.InteropServices": "4.1.0",
          "System.Text.Encoding.Extensions": "4.0.11"
        }
      },

// ...

      "System.IO": {
        "type": "Transitive",
        "resolved": "4.1.0",
        "contentHash": "3KlTJceQc3gnGIaHZ7UBZO26SHL1SHE4ddrmiwumFnId CEHP O8r386tZKaE6zlk5/mF8vifMBzHj9SaXN mQ==",
        "dependencies": {
          "Microsoft.NETCore.Platforms": "1.0.1",
          "Microsoft.NETCore.Targets": "1.0.1",
          "System.Runtime": "4.1.0",
          "System.Text.Encoding": "4.0.11",
          "System.Threading.Tasks": "4.0.11"
        }
      },
      "System.IO.FileSystem": {
        "type": "Transitive",
        "resolved": "4.0.1",
        "contentHash": "IBErlVq5jOggAD69bg1t0pJcHaDbJbWNUZTPI96fkYWzwYbN6D9wRHMULLDd9dHsl7C2YsxXL31LMfPI1SWt8w==",
        "dependencies": {
          "Microsoft.NETCore.Platforms": "1.0.1",
          "Microsoft.NETCore.Targets": "1.0.1",
          "System.IO": "4.1.0",
          "System.IO.FileSystem.Primitives": "4.0.1",
          "System.Runtime": "4.1.0",
          "System.Runtime.Handles": "4.0.1",
          "System.Text.Encoding": "4.0.11",
          "System.Threading.Tasks": "4.0.11"
        }
      },

Given that Microsoft.NETCore.Targets has been deprecated, what is the proper strategy to solve this problem, which I believe can be pretty common these days, now that .NET 3.x LTS is nearing EOL and .NET has been released?

Also, what is the magic behind the current workaround, provided that Microsoft.NETCore.Targets is basically just an empty package, with a zero-length lib\netstandard1.0\_._ file inside it?

CodePudding user response:

I use serilog and just updated to .net 7 myself. Didn't have any issues but I don't use that file. Just these:

  <PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
  <PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
  <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />

Just noticed that Serilog.Extensions.Logging.File uses the deprecated RollingFile sink.

CodePudding user response:

I've found an answer at least to this part:

what is the magic behind the current workaround, provided that Microsoft.NETCore.Targets is basically just an empty package, with a zero-length lib\netstandard1.0\_._ file inside it?

Here is a great explanation of how the actual fix works. Essentially, Microsoft.NETCore.Targets is an empty package from v3.1.0 and on, and the only purpose of it is to override and prevent loading any of its earlier versions, like those from .NET Core 1.x or 2.x:

The older version had a runtime.json which caused package downgrades

  • Related