Copy content files to output directory of DNX Console app via project.json
Asked Answered
S

4

12

I've just started working with DNX 1.0.0-rc1-update1 in VS2015. My first app is a 'Console Application (package)' project. Everything works, except NLog logging. I suspect it's because the NLog.config doesn't get copied to the output folder. How can I tell VS to copy this file to the output folder via project.json?

I've tried adding a 'resource' variable like this but it doesn't work:

project.json

...
"resource":"NLog.config",
...

EDIT 1: I'm using dnx451 so compatibility is not an issue.

EDIT 2: I added the following to project.json

"scripts": {
    "postbuild": [
      "%project:Directory%/../scripts/copy_resources.bat \\\"%project:Directory%\\\" \\\"%project:Directory%/../artifacts/bin/%project:Name%/%project:Version%/dnx451\\\""
    ] 
  }

copy_resources.bat

echo "Running script" >> C:\logs\log.txt
echo %1 >> C:\logs\log.txt
echo %2 >> C:\logs\log.txt

xcopy %1\NLog.config %2 /U /Y

There's nothing in the output window in VS to indicate that the script was actually run. Furthermore, log.txt is empty.

How can I debug the build process?

Subset answered 27/12, 2015 at 15:56 Comment(3)
Have you experimented with path to your NLog.config in project.json? Approach you applied should work in general: #31973071Wolfie
You may need to update your title. What you tried in your project.json file will compile NLog.config into the assembly. What you are describing in the post is to copy the NLog.config to the output directory.Oshinski
Not sure what you are doing wrong. Only thing I can think of is NLog does not support DNXCORE50 so remove it from your project.json. Microsoft's own sample might be of help. github.com/aspnet/Logging/tree/1.0.0-rc1/samples/SampleAppChecani
M
23

In the meantime, .NET Core RTM was published.

Now, the current way to get stuff copied to the output folder is using the buildOptions section in project.json.

There's the copyToOutput option which you can use like this:

Before:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  // more stuff
}

After:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true,
    "copyToOutput": { "includeFiles": [ "NLog.config" ] }
  },

  // more stuff
}
Mourning answered 3/8, 2016 at 15:8 Comment(1)
For me just this was necessary: "buildOptions": { "copyToOutput": "file.txt" }Befuddle
E
10

Use copyToOutput inside buildOptions:

{
  "buildOptions": {
    "copyToOutput":  "NLog.config" 
  }
}

or for multiple files declare an array:

{
  "buildOptions": {
    "copyToOutput":  ["NLog.config", "testdata\\"]
  }
}

To copy a directory remember to add the trailing \\.

Ecstatic answered 22/9, 2016 at 11:50 Comment(0)
O
3

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

More info: http://docs.asp.net/en/latest/dnx/projects.html#including-excluding-files

You use the content section of project.json like this

{
  "content": [
    "NLog.config"
  ]
}

Now the documentation says that the file should have been copied by default as the content default if * (wildcard for all files), but you can force it with the explicit stating of the file you want in the content section.

Oshinski answered 28/12, 2015 at 0:10 Comment(3)
It still doesn't work. I can't find NLog.config in the output folder (...\MyProject\artifacts\bin\MyProject\Debug\app) nor can I see any console output. I know for sure NLog works if I configure it in the code.Subset
Ok. seeing as the content copy route is not working you could try this one. https://mcmap.net/q/909731/-new-net-quot-project-json-quot-project-copying-pre-built-native-dlls-to-the-output-directory where they use a postbuild script to move files (in this case dlls) over to output directory.Oshinski
This property is deprecated, the answer from @ChristianSpecht has the new propertyBefuddle
S
2

I think it is broken in RC1. I was also looking how to get content files copied to the output folder, and found this issue that looks similar to what we are seeing.

As @Nkosi points out, the default for content is **/* (you can see via the docs link he provided, and also by the schema).

As for your postbuild step, you can get it going by "producing outputs".

Stellarator answered 7/3, 2016 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.