Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ${basedir} in NLog target creates a folder by that name

I am using NLog.Web.AspNetCore in a Asp.Net core 2.0 project. If I use the following target:

<target xsi:type="File" name="allfile" fileName="${basedir}/nlog-all-${shortdate}.log"

it creates an empty folder called "${basedir}" in my application's ContentRoot folder. It puts the log file in bin\Debug\netcoreapp2.0.

Why is it not substituting for ${basedir}? It works fine if I use a full path for the log folder.

I should add that I also have the following in nlog.config, inside the < nlog > element:

internalLogFile="${basedir}/internal-nlog.txt"

When there is an internal Nlog error, it writes to this file inside the folder named "${basedir}".

like image 258
John Pankowicz Avatar asked Jan 02 '23 18:01

John Pankowicz


1 Answers

The ${basedir} folder is created, because the NLog Internal Logger is very primitive and doesn't know about layout-renders.

  • Update NLog ver. 4.6 adds support for using ${basedir} in internalLogFile

The workaround for Asp.Net.Core could be the following (Added two new lines to the Wiki Example in Program.cs):

var appBasePath = System.IO.Directory.GetCurrentDirectory();
NLog.GlobalDiagnosticsContext.Set("appbasepath", appBasePath);
var logger = LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();

Then you can use ${gdc:item=appbasepath} in your nlog.config:

<target xsi:type="File" name="allfile" fileName="${gdc:item=appbasepath}/Logs/nlog-all-${shortdate}.log"

While waiting for ${aspnet-appbasepath} to become ready.

like image 73
Rolf Kristensen Avatar answered Feb 23 '23 17:02

Rolf Kristensen