根项目文件夹中缺少 Nlog 配置

时间:2021-02-11 15:15:00

标签: .net visual-studio-2019 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\有效,但我现在有另一个问题:

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

enter image description here

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

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 中提到的错误。

1 个答案:

答案 0 :(得分:0)

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

enter image description here

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

enter image description here