首页 > 解决方案 > 根项目文件夹中缺少 Nlog 配置

问题描述

我对以下事实有疑问:我无法将 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中提到的错误。

标签: .netvisual-studio-2019nlog

解决方案


因此,最后,我的解决方案是手动复制项目根目录中的文件(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以及命令成功的证明:

在此处输入图像描述

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

在此处输入图像描述


推荐阅读