Providing full path for FileTarget

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Providing full path for FileTarget

kay.one
I'm trying to provide a fully qualified path to a FileTarget

var lastUpgradeLog = new FileTarget();
            lastUpgradeLog.AutoFlush = true;
            lastUpgradeLog.ConcurrentWrites = false;
            lastUpgradeLog.CreateDirs = true;
            lastUpgradeLog.FileName = "C:\\nLog\\Temp\\Test\\test.log;
            lastUpgradeLog.KeepFileOpen = false;
            lastUpgradeLog.Layout = "${longdate} - ${logger}: ${message} ${exception:format=ToString}";


but as soon as I try to write something to the log I get this DirectoryNotFoundException,
The system cannot find the path specified. (Exception from HRESULT: 0x80070003)

it works fine if I use relative paths to a directory that doesn't exist. but as soon as I try using a full path it blowsup.

is this a bug or am I missing something
Reply | Threaded
Open this post in threaded view
|

Re: Providing full path for FileTarget

Jarek Kowalski
Administrator
You have to either:

- use forward slashes: C:/nlog/temp/test/test.log
- use double backslashes

Note that in C#, backslash is an escape character itself, so you need to either use verbatim string by prefixing it with '@' sign:

 lastUpgradeLog.FileName = @"C:<a href="smb://nLog//Temp//Test//test.log">\\nLog\\Temp\\Test\\test.log";

or manually escape each slash, so instead of two, you get four:

 lastUpgradeLog.FileName = "C:<a href="smb://nLog//Temp//Test//test.log">\\\\nLog\\\\Temp\\\\Test\\\\test.log";

I generally recommend forward slashes, since Windows works with them just fine and they don't have the escaping problem. As a bonus, your app is more likely to work on Unix under Mono, which uses forward slashes.

Jarek

NLog Blog