0
votes

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;
2
What is the full path of the log folder you expect it to create? - mjwills
Witch project ? ASP.NET, WPF, WinForm, Windows Service ? I think there are a Auth problem. On ASP.NET under IIS check the app pool user, on Windows Service check the user, etc - Max
@mjwills Just added the full path (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. - fharreau
@Max My bad. I wanted to specify it, but I forgot. Just fixed. It is a WPF application. - fharreau
Maybe the error is in that line ? config.Variables["logDirectory"] = new SimpleLayout(@"C:\Users\f.harreau\ScopIt\logs"); remove the new SimpleLayout like this config.Variables["logDirectory"] = @"C:\Users\f.harreau\ScopIt\logs"; - Max

2 Answers

0
votes

After updating to the latest version of NLog, 4.4.12, I had the exact same problem and pursued it perhaps a little too long. Hitting a dead end, I added the following code to my logging class that writes to NLog.

// using System.IO;        
void Initialize()
{
    if(!Directory.Exists("logs"))
    {
        Directory.CreateDirectory("logs");
    }
}

This worked with my service apps, but not with my web apps. When I set up a clean test project including default NLog configs, the app started creating the folders like before. I went back to my original project and uninstalled/reinstalled the NLog packages. I overwrote the NLog config and made sure to include the package in both the logging project and default console application. Again, the app began to create the folders like before.

The issue was entered as a bug on 3.20.17 on the NLog repository, currently closed. This is obviously an issue that can be recreated, but for now, I'm just glad it's working again.

0
votes

For those hitting this issue in a Windows service project, I fixed this by adding a logger to the root Program class, and writing to the log as the first line in Main.

static class Program
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        _logger.Info("Main Thread Started"); <snip>