在NLog目标中使用$ {basedir}可以按该名称创建一个文件夹

Joh*_*icz 2 nlog asp.net-core

我在Asp.Net核心2.0项目中使用NLog.Web.AspNetCore.如果我使用以下目标:

<target xsi:type="File" name="allfile" fileName="${basedir}/nlog-all-${shortdate}.log"
Run Code Online (Sandbox Code Playgroud)

它在我的应用程序的ContentRoot文件夹中创建一个名为"$ {basedir}"的空文件夹.它将日志文件放在bin\Debug \netcoreapp2.0中.

为什么不替换$ {basedir}?如果我使用日志文件夹的完整路径,它工作正常.

我应该补充一点,我在<nlog>元素中的nlog.config中也有以下内容:

internalLogFile="${basedir}/internal-nlog.txt"
Run Code Online (Sandbox Code Playgroud)

当存在内部Nlog错误时,它会在名为"$ {basedir}"的文件夹中写入此文件.

Rol*_*sen 7

${basedir}文件夹已创建,因为NLog Internal Logger非常原始且不了解布局渲染.

Asp.Net.Core的解决方法可能如下(在Wiki示例中添加了两个新行Program.cs):

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

然后你可以${gdc:item=appbasepath}在你的nlog.config:

<target xsi:type="File" name="allfile" fileName="${gdc:item=appbasepath}/Logs/nlog-all-${shortdate}.log"
Run Code Online (Sandbox Code Playgroud)

在等待${aspnet-appbasepath}准备好的同时.