nlog.config- file not found .Net core 3.1 and Docker

Viewed 1836

I am new to Docker and I am getting an error from the below line in the Main method, it's a .net core 3.1 API with Docker, When it tries to initialize the Nlog, the code unable to find the nlog.config, the file is configured to copy always.

var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();

this is the error I am getting,

System.IO.FileNotFoundException: 'Failed to load NLog LoggingConfiguration. 
  Searched the following locations:
     - /app/bin/Debug/netcoreapp3.1/nlog.config

It is working fine when running with IIS.

Thanks in advance Aabid

2 Answers

It is hard to tell based on the information you have provided. Do you have any additional information - a Dockerfile that you could provide/show relevant lines of? There are some guidelines for dockerizing an Asp.Net Core application over on the docker examples.

If your application works in IIS but not docker then I'd suggest that your nlog.config file isn't part of your container. Perhaps check that your nlog.config file is copied to the output directory during a build? (in VS2019 right click on the file, then properties, then set "Copy to Output Directory" to "Copy if newer")

nlog.config Properties

You can build your container and then enter a bash session to browse the directories/etc by using the following docker commands:

Build the container using:

docker build -t nlog-example .

Enter a bash terminal session using:

docker run --rm -it --entrypoint /bin/bash nlog-example

(note, nlog-example is the docker tag in my example, but yours may be different)

Add an additional answer for those who are dealing with this environment: Docker + Linux:

As On Linux, the file system is case sensitive, let's simply change config name exactly same what you see in images, case-sensitive.

but in another hand, set LogManager.LogFactory.SetCandidateConfigFilePaths is also necessary if renaming not work.

Here my config name is NLog.Config:

var fullPath = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location))
        .FirstOrDefault(x => x.EndsWith("nlog.config", StringComparison.OrdinalIgnoreCase));

LogManager.LogFactory.SetCandidateConfigFilePaths(new[] { fullPath });
var config = Path.GetFileName(fullPath);
return NLogBuilder.ConfigureNLog(config).GetCurrentClassLogger();

enter image description here

Related