Serilog、.NET 7、错误NU1605(“检测到软件包降级”)和Microsoft. NET核心,目标

omtl5h9j  于 2022-11-19  发布在  .NET
关注(0)|答案(2)|浏览(323)

在一个.NET 7项目中,我使用Serilog(最新的Serilog.Extensions.Logging.File v3.0.0),并在尝试使用dotnet publish编译项目时得到一系列错误NU1605

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)

经过深入研究,我发现在.csproj文件中添加以下内容可以解决这个问题:

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

但是,Microsoft.NETCore.Targets软件包在v6.0.0-preview.4.21253.7停止更新。显然,它已经被弃用,请参阅"Delete Microsoft.NETCore.Targets package"问题。
为了进一步排除故障,我启用了"repeatable package restores using a lock file"(无论如何,这是CI构建的正确做法)。

"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"
        }
      },

鉴于Microsoft.NETCore.Targets已被弃用,解决此问题的正确策略是什么?我相信,现在.NET 3.x LTS已接近EOL,而且.NET已发布,解决此问题的策略可能相当普遍。
此外,假设Microsoft.NETCore.Targets基本上只是一个空包,其中包含一个零长度的lib\netstandard1.0\_._文件,那么当前的解决方案背后的魔力是什么?

dldeef67

dldeef671#

我使用serilog和刚刚更新到.net 7自己。没有任何问题,但我不使用该文件。只有这些:

<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" />

刚刚注意到Serilog.Extensions.Logging.File使用了不推荐使用的RollingFile接收器。

smdnsysy

smdnsysy2#

我至少找到了这部分的答案:
假设Microsoft.NETCore.Targets基本上只是一个空包,里面有一个零长度的lib\netstandard1.0\_._文件,那么当前的解决方案背后有什么魔力呢?
这里有一个很好的解释,说明了实际的修复是如何工作的。本质上,Microsoft.NETCore.Targets是v3.1.0和更高版本的一个空包,它的唯一目的是覆盖和阻止加载它的任何早期版本,比如来自.NET Core 1.x或2.x的版本:
旧版本有一个runtime.json,这会导致包降级

相关问题