【问题标题】:Nlog configuration missing from the root project folder根项目文件夹中缺少 Nlog 配置
【发布时间】:2021-02-11 15:15:00
【问题描述】:

我遇到了以下事实:我无法将 Nlog.config 文件添加到我的 .net 控制台应用程序的根文件夹中,因为它安装在 ...nuget\packages\nlog.config\4.7.7\ contentFiles\any\any\NLog.config.

在我的机器上,我可以使用该属性将其复制到输出文件夹(选项“复制到输出目录”),但它没有帮助,因为当我将项目提交到 Gitlab 时,它不包含 Nlog.Config,因为它要么在我没有提交给 Gitlab 的项目的输出文件夹中,要么在 .nuget 文件夹中。

当我在 AWS 中运行它时,从 gitlab 中检查出项目后,Nlog.config 文件不存在,默认使用另一个不包括我的修改的文件。

你能帮忙吗?谢谢!

编辑

好的,我进行了更多研究,发现-https://stackoverflow.com/questions/15958271/make-nlog-config-file-load-the-file-from-d-dev-instead-of- bin-debug 但仍然不明白如何将它应用到我的示例中。

编辑 2

复制一个Nlog.config和Nlog.xsd到项目根目录,编辑ConsoleCoreApp1.csproj并替换:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <ItemGroup>    
    <None Update="C:\Users\User\.nuget\packages\nlog.config\4.7.7\contentFiles\any\any\NLog.config">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="C:\Users\User\.nuget\packages\nlog.schema\4.7.7\contentFiles\any\any\NLog.xsd">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    ...
  <ItemGroup>
</Project>

与:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <ItemGroup>    
    <None Update="NLog.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="NLog.xsd">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    ...
  <ItemGroup>
</Project>

尝试通过命令行构建时出现以下错误:

Build FAILED.

"I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj" (publish target) (1) ->
(GenerateSingleFileBundle target) ->
  C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: System.ArgumentException: Invalid input specification: Found multiple entries with the same BundleRelativePath [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.NET.HostModel.Bundle.Bundler.GenerateBundle(IReadOnlyList`1 fileSpecs) [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateBundle.ExecuteCore() [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.NET.Build.Tasks.TaskBase.Execute() [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.NET.Build.Tasks.TaskWithAssemblyResolveHooks.Execute() [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]
C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]

    0 Warning(s)
    1 Error(s)

编辑 3

所以,最后,我手动复制项目根目录中的文件(Nlog.config 和 Nlog.xsd)并更改 .csproj 文件以引用它们而不是相同文件但位于 ...\.nuget\packages\nlog.config\4.7.7\contentFiles\any\any\ 中的解决方案,但我现在有另一个问题:

发布命令有问题(构建命令没问题)。我尝试从命令行和 Visual Studio 2019 发布两者,但仍然出现错误。

这是错误日志文件的输出,没有多大帮助:

2/12/2021 6:05:32 AM
System.AggregateException: One or more errors occurred. ---> Microsoft.WebTools.Shared.Exceptions.WebToolsException: Publish has encountered an error. We were unable to determine the cause of the error. Check the output log for more details. 
   --- End of inner exception stack trace ---
---> (Inner Exception #0) Microsoft.WebTools.Shared.Exceptions.WebToolsException: Publish has encountered an error. We were unable to determine the cause of the error. Check the output log for more details. <---

Microsoft.WebTools.Shared.Exceptions.WebToolsException: Publish has encountered an error. We were unable to determine the cause of the error. Check the output log for more details. 
===================

我使用的命令是: msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:RuntimeIdentifier=win-x86.

当我在终端中运行它时,我得到了 EDIT 2 中提到的错误。

【问题讨论】:

  • 猜猜这也是这个:stackoverflow.com/a/66109358/201303
  • 这不是问题。使用 引用项目效果很好,即&lt;PackageReference Include="NLog.Config" Version="4.7.7" /&gt;。问题是我的 msbuild 发布命令不起作用msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86 /p:PublishSingleFile=true ...
  • 那么 nuget\packages\nlog.config\4.7.7\contentFiles\any\any\nlog.config。我无法编辑。这正是不将 NLog.config 包与&lt;PackageReference&gt; 一起使用的原因。它不会很好地工作,而且只会令人困惑。

标签: .net visual-studio-2019 nlog


【解决方案1】:

所以,最后,我手动复制项目根目录中的文件(Nlog.config 和 Nlog.xsd)并更改 .csproj 文件以引用它们而不是相同文件但位于 ...\.nuget\packages\nlog.config\4.7.7\contentFiles\any\any\ 中的解决方案有效。

从我的 .csproj 文件中删除 PublishSingleFile 和 SelfContained 属性后,现在构建和发布命令都可以工作了:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
  ...
</Project>

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
  </PropertyGroup>
  ...
</Project>

以及msbuild publish命令成功的证明:

以及使用日志文件运行应用程序的证明:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多