带有 Serilog 程序集的 .NET Standard 2.0 项目问题

Issue with .NET Standard 2.0 project with Serilog assembly

我有一个 .NET Standard 2.0 项目,它使用 Serilog 程序集来使用 serilog 进行日志记录,这里是项目文件:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AssemblyTitle>Logger</AssemblyTitle>
    <Description>Client Web Logger</Description>
    <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.Win32.Registry" />
    <PackageReference Include="SchneiderElectric.Software.SASigning" />
    <PackageReference Include="Serilog" />
    <PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>
<ItemGroup>
  <PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="SET _WWBinDir=&quot;$(ProjectDir)..\..\..\interfaces\bin$(ConfigurationName)$(PlatformName)&quot;&#xD;&#xA;MKDIR %_WWBinDir%&#xD;&#xA;robocopy &quot;$(TargetDir) &quot; &quot;%_WWBinDir%&quot; InSightLogger.dll /ZB /NJH /NJS /NDL /NFL /NC /NS&#xD;&#xA;:END&#xD;&#xA;exit 0&#xD;&#xA;" />
</Target>
我的解决方案中还有另外两个项目,一个在 .NET 6.0 中,另一个在 NET Framework 4.8 中 这是 .NET 6.0 的项目文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup Label="Globals">
    <Description>My Server</Description>
    <TypeScriptToolsVersion>3.1</TypeScriptToolsVersion>
    <TargetFramework>net6.0</TargetFramework>
    <_HasReferenceToSystemRuntime>true</_HasReferenceToSystemRuntime>
    <RootNamespace>My.Insight</RootNamespace>
    <DefineConstants>DESKTOP;LICENSEREQUIRED</DefineConstants>
</PropertyGroup>
<ItemGroup>
    <Compile Remove="App_Start\**" />
    <Content Remove="App_Start\**" />
    <EmbeddedResource Remove="App_Start\**" />
    <None Remove="App_Start\**" />
</ItemGroup>
<ItemGroup>
    <Folder Include="..\..\Common\wwwroot\Insight" />
</ItemGroup>
<ItemGroup>
    <ProjectReference Include="..\..\Favorites\FavoriteApiModel\FavoriteApiModel.csproj" />
    <ProjectReference Include="..\..\Logger\Logger\Logger.csproj" />
    <ProjectReference Include="..\LicenseUtil\LicenseUtil.csproj" />
</ItemGroup>
<ItemGroup>
    <None Include="App.config" />
</ItemGroup>
<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
  <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  <PackageReference Include="WWMenuHttpModule" Version="4.0.21100101" />
</ItemGroup>
<ItemGroup>
    <PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" />
</ItemGroup>

和框架项目文件:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <RootNamespace>my.InSight</RootNamespace>
    <TargetFramework>net48</TargetFramework>
    <AssemblyTitle>Article Server Service</AssemblyTitle>
    <NeutralLanguage>en-US</NeutralLanguage>
    <Description>Article Service</Description>
</PropertyGroup>
<Import Project="..\..\..\SharedComponents\External\StyleCop\StyleCop.Targets" />
<ItemGroup>
    <PackageReference Include="System.Net.Http" />
    <PackageReference Include="Newtonsoft.Json" />
    <PackageReference Include="StyleCop" />
    <PackageReference Include="System.Configuration.ConfigurationManager" />
</ItemGroup>
<ItemGroup>
    <ProjectReference Include="..\..\Logger\Logger\Logger.csproj" />
</ItemGroup>
<ItemGroup>
    <Reference Include="System.Management" />
    <Reference Include="System.ServiceProcess" />
    <Reference Include="System.Configuration.Install" />
</ItemGroup>
<ItemGroup>
    <Compile Update="ProjectInstaller.cs">
        <SubType>Component</SubType>
    </Compile>
    <Compile Update="ProjectInstaller.Designer.cs">
        <DependentUpon>ProjectInstaller.cs</DependentUpon>
    </Compile>
    <Compile Update="myService.cs">
        <SubType>Component</SubType>
    </Compile>
    <Compile Update="myService.Designer.cs">
        <DependentUpon>myService.cs</DependentUpon>
    </Compile>
</ItemGroup>
<ItemGroup>
    <EmbeddedResource Update="ProjectInstaller.resx">
        <DependentUpon>ProjectInstaller.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Update="myService.resx">
        <DependentUpon>myService.cs</DependentUpon>
        <SubType>Designer</SubType>
    </EmbeddedResource>
</ItemGroup>
<ItemGroup>
    <PackageReference Update="Microsoft.CodeAnalysis.FxCopAnalyzers" />
</ItemGroup>

对于 .NET 6.0,一切正常,但对于 .NET Framework 4.8,当我访问记录器项目静态 class 和方法时,它通过异常,这是我得到的异常

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\WonderWorld\MyInsight\Server\aahInSightHost.exe
--- A detailed error log follows. --Pre-bind state information --
LOG: DisplayName = netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51 
Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/WonderWorld/MyInsight/Server/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = aahInSightHost.exe
Calling assembly : Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\WonderWorld\MyInsight\Server\aahInSightHost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Wonderware/HistorianInsight/Server/netstandard.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Wonderware/HistorianInsight/Server/netstandard/netstandard.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Wonderware/HistorianInsight/Server/netstandard.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Wonderware/HistorianInsight/Server/netstandard/netstandard.EXE.
LOG: All probing URLs attempted and failed.

注意: 只有一件事我注意到我创建了 netstandard2.0 项目并且在日志中提到 netstandard 2.1.0.0 正在搜索,但无法理解。

通过仔细调查您的项目文件,我了解到您使用的是 dotnet standard 2.0,但错误地从 dotnet standard 2.1[=14= 搜索] 所以你需要验证所有使用包版本的项目并使用合适的版本。