NLog not finding or loading configuration file

10,615

I managed to get this working this morning

There are two running projects using the logging - an MVC app and a WEBAPI app. The problem was the NLog.config file was being copied into the BIN folders of the apps when the solution was being built. The config file needs to be in the project roots alongside the web.config files.

To get around this I created a post build event on the Logging project to copy the config file to the project root of the MVC app and the WEBAPI app.

The logging now works fine.

Share:
10,615
Neil
Author by

Neil

I'm a c++ / c# programmer working for a small company in Dundee, Scotland.

Updated on June 04, 2022

Comments

  • Neil
    Neil almost 2 years

    I'm writing an application and using Ninject to create an instance of a Logging factory which can be used to create Nlog Loggers. I have my own Logger class which wraps the Nlog Logger class

    I've got an IOC project which handles the injection and a Logging project which hosts the LoggerFactory and Logger classes.

    The reason I want this is to have one logging configuration shared by all the other projects rather than have to include the Nlog dll and config in each project separately.

    It all builds and runs fine but when the LoggerFactory comes to create a logger

    public ILogger GetLogger<T>()
    {
        NLog.Logger nLogLogger = NLog.LogManager.GetLogger(typeof(T).ToString());
    
        return new Logging.Logger(nLogLogger);
    }
    

    the configuration it is null which suggests that the NLog.dll isn't finding the NLog.config file.

    NLog.LogManager.Configuration 
    

    I've followed the instructions and said for the NLog.config to be named NLog.dll.nlog to be copied to bin directory and I can see it alongside the NLog.dll

    From the Nlog documentation this is supposed to be enough http://nlog-project.org/wiki/Configuration_file but it's not being discovered. "NLog.dll.nlog in a directory where NLog.dll is located"

    I have the NLog.dll.nlog file marked as content and copyToLocal as true.

    Thanks for any help Neil