Hi,
I cannot believe I'm the first who has this question/problem. How to configure NLog to write log files to working (current) directory, please?
Each time, when I call MyApp.exe (located in C:\Apps folder) from C:\Data\Dec folder, log files are created in C:\Apps folder. It does not matter if I use ${basedir} or not.
C:\Data\Dec> C:\Apps\MyApp.exe
NLog version: 4.4.12
Platform: .Net 4.6.1
Current NLog config & code
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="logfileA" xsi:type="File" fileName="fileA.txt" />
<target name="logfileB" xsi:type="File" fileName="${basedir}/fileB.txt" />
<target name="logfileC" xsi:type="File" fileName="${basedir}\fileC.txt" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="logfileA" />
<logger name="*" minlevel="Info" writeTo="logfileB" />
<logger name="*" minlevel="Info" writeTo="logfileC" />
</rules>
</nlog>
```C#
class Program
{
private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
log.Debug("debug message");
}
}
```
All the times, all three files are created in C:\Apps folder.
The basedir is where your .exe lives, so this is expected.
I think your are looking for the "processDir" option,
e.g.
${basedir:processDir=true}
See https://github.com/NLog/NLog/wiki/Basedir-Layout-Renderer
Thank you for your reply.
No. I do not want to have log files stored in MyApp.exe folder (app domain), but relatively in my current/working folder(C:\Docs\dec).
I understand the meaning of ${basedir}. It is described here: https://github.com/nlog/nlog/wiki/Logging-troubleshooting. Especially chapter Troubleshooting steps, paragraph 6.
If you don't use a fully qualified name of the file (such as c:\logs\log.txt, or ${basedir}\log.txt your logs may be written to the working directory, which is not necessarily the same directory as where the application resides.
I have not path to fileA fully qualified, but nevertheless it appears in MyApp.exe folder instead of C:\Data\dec).
I'm pretty sure, my working directory is C:\Docs\dec. The log file appears in C:\Apps :(
C:\Data\Dec> C:\Apps\MyApp.exe
using of ${basedir:processDir=true} has the same behavior. It does not make sense. I do want to base logger to process dir.
Thanks a lot!
@frunta You can register your own custom layout renderer that returns current directory:
https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer
string startupDirectory = System.IO.Directory.GetCurrentDirectory(); // Ensure it stays the same
NLog.LayoutRenderers.LayoutRenderer.Register("startupdir", (logEvent) => startupDirectory);
Then use it like this:
<target name="logfileC" xsi:type="File" fileName="${startupdir}\fileC.txt" />
I assume your question has been answered, if not, let us know!
@frunta PR #2491 has been created that adds a new layoutrenderer that supports currentdir.
@frunta, you can take a look its wiki page to learn how to use it. It will be released with the next version, v4.5.
${currentdir} is working in .NET and in .NET Core.
Most helpful comment
${currentdir}is working in .NET and in .NET Core.