0

I'm having trouble with the following fact : I cannot add Nlog.config file to the root folder of my .net console application as it gets installed in ...nuget\packages\nlog.config\4.7.7\contentFiles\any\any\NLog.config.

On my machine I can use the property to copy it to the output folder (option "Copy to Output Directory") but it does not help because when I submit my project to Gitlab and it does not include the Nlog.Config because it is either in my output folder of the project, which I don't commit to Gitlab, or in the .nuget folder.

When I run it in AWS, after checking the project out from gitlab, the Nlog.config file is not there, another one by default is used which does not include my modifications.

Could you please help ? Thanks !

EDIT

Ok, I researched a bit more and found out -https://stackoverflow.com/questions/15958271/make-nlog-config-file-load-the-file-from-d-dev-instead-of-bin-debug but still don't understand how to apply it to my example.

EDIT 2

Copying a Nlog.config and Nlog.xsd into the root directory of the project and editing the ConsoleCoreApp1.csproj and replacing :

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

with :

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

results in the following error when trying to build by command line:

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)

EDIT 3

So, finally, my solution of copying manually the files (Nlog.config and Nlog.xsd) in the project's root directory and change the .csproj file to reference them instead of the same files but located into ...\.nuget\packages\nlog.config\4.7.7\contentFiles\any\any\ works, but I have another problem now :

There is a problem with the publish command (the build command is ok). I tried publishing both, from the command line and from Visual Studio 2019 and still get an error.

enter image description here

And this is the output of the error log file which does not help much :

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. 
===================

The command I used is : msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:RuntimeIdentifier=win-x86.

When I run it in the terminal I get the error mentioned in EDIT 2.

R13mus
  • 752
  • 11
  • 20
  • Guess this is also this: https://stackoverflow.com/a/66109358/201303 – Julian Feb 11 '21 at 18:10
  • That was not the problem. It works well to reference a project with , i.e. ``. The problem is my msbuild publish command which does not work `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` ... – R13mus Feb 12 '21 at 04:55
  • Well nuget\packages\nlog.config\4.7.7\contentFiles\any\any\nlog.config. i can't be edited. That precise the reason not to use NLog.config package with ``. It won't work well and it's only confusing. – Julian Feb 13 '21 at 11:21

1 Answers1

0

So, finally, my solution of copying manually the files (Nlog.config and Nlog.xsd) in the project's root directory and change the .csproj file to reference them instead of the same files but located into ...\.nuget\packages\nlog.config\4.7.7\contentFiles\any\any\ works.

Now both the build and the publish commands work, after removing the PublishSingleFile and SelfContained properties from my .csproj file :

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

with

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

And the proof of the successful msbuild publish command:

enter image description here

And the proof of running the app with the log file :

enter image description here

R13mus
  • 752
  • 11
  • 20