38

Unless I'm totally missing it, I'm under the impression that the NLog documentation uses ${basedir} in its examples, without explaining what its location is supposed to be.

Where can I find information that lists all possible options with a meaningful description?

I have this configuration defined:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
  <targets>
    <target name="file" xsi:type="File"
                layout="${longdate} ${logger} ${message}"
                fileName="${basedir}/logs/${shortdate}.txt"
                keepFileOpen="false"
                encoding="iso-8859-2" />
  </targets>
  <rules>
    <logger name="*" minlevel="Debug" writeTo="file" />
  </rules>
</nlog>

It works as far as I can tell, but I haven't got a clue where it logs anything at.

InteXX
  • 6,135
  • 6
  • 43
  • 80
Spikee
  • 3,967
  • 7
  • 35
  • 68
  • 2
    Remember to change File Properties inside Visual Studio for the NLog-config-file to Copy to Output Directory => Always. Alternative inject the nlog-config into the app.config https://gist.github.com/Chrisso/1703644 – Rolf Kristensen Jun 14 '17 at 18:14

4 Answers4

40

${basedir} — Directory where the application runs, aka. AppDomain.BaseDirectory

I think, you will find this manual page helpful.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
galakt
  • 1,374
  • 13
  • 22
  • 7
    So the `bin\debug` folder, if you are running it from VS? In that case it doesn't work, because I don't see any files. I don't get any errors though. – Spikee Feb 05 '16 at 12:45
  • @Spikee You can check it in your application by AppDomain.CurrentDomain.BaseDirectory – galakt Feb 05 '16 at 12:50
  • 1
    That is `bin\debug` indeed, but I don't see the `logs` subfolder, nor any files. – Spikee Feb 05 '16 at 12:55
  • @Spikee I tried it right now and it works for me. Are you sure that you catch and log your exception? Are you sure that you call LogManager.Flush(); before close application? – galakt Feb 05 '16 at 13:08
  • 1
    Note that for asp.net projects ${basedir} (and AppDomain.CurrentDomain.BaseDirectory for that matter) point to the root directory of your website which (when using visual studio) is the directory containing the Source-Code for the entry-level ASP.NET project of your ASP.NET website (aka the "bin/[Debug-Release]" part is not there for ASP.NET websites when debugging from within visual studio). I couldn't find my log files because I was looking for them at the wrong folder when using VS. Interesting huh? https://stackoverflow.com/a/8669872/863651 – XDS Jul 12 '17 at 11:20
  • Addendum: Julian below claims that the /bin folder is included in ${basedir} (AppDomain.CurrentDomain.BaseDirectory) in aspnet-core (Go figure ...). Just keep this in mind. – XDS Jul 12 '17 at 11:27
  • If I use fileName="${basedir}/filename.log", it created an empty folder named "${basedir} " in my asp.net core root folder and puts the log files in "bin\Debug\netcoreapp2.0" ????? Why is it not substituting for ${basedir} ? – John Pankowicz Feb 24 '18 at 02:24
7

Based on already provided answers and comments, the answer can be summed up for .NET application:

AppDomain.CurrentDomain.BaseDirectory

For Console or Windows Forms application, this directory is bin/debug while within Visual Studio. If application is deployed, the path will most probably be the executable path.

For Web applications (ASP.NET) this will be the Web application root directory.

Not seeing any files may be triggered by several causes that include: NLog configuration errors and not being able to write the target file. To expose these errors make sure that NLog.config (or Nlog configuration embedded within web.config or app.config) specifies an internal log file to output such errors:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="C:\NLogError\NLog.log">

<!-- targets and rules come here -->

</nlog>
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
4

Another possibility for failure is that if you are using NLog.config, NLog can't find the config file. Set the file to Copy Always in your build and it will end up in your bin directory, so NLog can find it at runtime.

If you copy the NLog configuration info to your App.config, you won't have this problem.

Evan
  • 457
  • 4
  • 14
  • I also ran into a similar thing where I needed to flip the "Copy to Output" option on for a copy of "NLog.xsd" (which is referenced in the NLog configuration) in my Visual Studio project before I could get any logs to write in my "bin/debug" folder during testing. – Dalek Control Feb 27 '18 at 14:57
  • never set anything to "copy always". use "copy if newer" otherwise your build will always be considered out of date (it will compile the project even if nothing is changed) – Ammo Goettsch Apr 26 '20 at 15:55
3

It depends also of the platform. For regular .Net projects it's indeed bin or bin/debug etc. (depending of your build settings)

For coreclr / aspnet5 it's the bin folder of your dnx runtime. That's still work in progress.

Julian
  • 33,915
  • 22
  • 119
  • 174