I have a weird behavior of my logging framework (NLog) for my WPF application. I tried to google the issue but I only found issues concerning NLog not creating the logfile.
I have another problem. NLog throws an exception at the first attempt of logging something:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\f.harreau\ScopIt\logs\2017-07-24.log'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
Before today, I never had this problem. I could delete the log folder, NLog recreated it at the application startup.
If I create the folder manually, everything goes well. If the logfile does not exist, NLog creates the file. NLog just stopped creating the log folder.
My coworker does not have the issue. He can delete the log folder, NLog recreate it at startup.
So it looks like a computer issue. But if I run the installed version of my application, it does create the log folder if it does not exist. So may be a Visual Studio issue?
Visual Studio is run as Administrator. I tried to "reboot" Visual Studio without success.
Do you have any idea of what could cause that?
Here is my NLog configuration:
var config = new LoggingConfiguration();
config.Variables["logDirectory"] = new SimpleLayout(@"C:\Users\f.harreau\ScopIt\logs");
// Creating Targets
// -- All logs
var allLogs = new FileTarget("allLogs");
allLogs.Layout = "${longdate} ${logger} ${uppercase:${level}} ${message}";
allLogs.FileName = "${var:logDirectory}/${shortdate}.log";
// -- Expert system logs
var expertSystemLogs = new FileTarget("expertSystemLogs");
expertSystemLogs.Layout = "${longdate} ${logger} ${uppercase:${level}} ${message}";
expertSystemLogs.FileName = "${var:logDirectory}/${shortdate}.expert_system.log";
// -- Error logs
var errorLogs = new FileTarget("errorLogs");
errorLogs.Layout = "${longdate} ${logger} ${uppercase:${level}} ${message} ${exception:format=tostring}";
errorLogs.FileName = "${var:logDirectory}/${shortdate}.errors.log";
// Adding the targets to the config
config.AddTarget(allLogs);
config.AddTarget(expertSystemLogs);
config.AddTarget(errorLogs);
// Defining rules
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, allLogs));
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Error, errorLogs));
config.LoggingRules.Add(new LoggingRule("MyCompany.ExpertSystem.*", LogLevel.Info, expertSystemLogs));
// Activating the configuration
LogManager.Configuration = config;
C:\Users\f.harreau\ScopIt\logs) to the question. Also tried right now with the path hard coded (exactly like in the question), and it failed too. - fharreauconfig.Variables["logDirectory"] = new SimpleLayout(@"C:\Users\f.harreau\ScopIt\logs");remove thenew SimpleLayoutlike thisconfig.Variables["logDirectory"] = @"C:\Users\f.harreau\ScopIt\logs";- Max