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}".
The ${basedir}
folder is created, because the NLog Internal Logger is very primitive and doesn't know about layout-renders.
${basedir}
in internalLogFileThe 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With