我在Asp.Net core 2.0项目中使用NLog.Web.AspNetCore。如果我使用以下目标:
<target xsi:type="File" name="allfile" fileName="${basedir}/nlog-all-${shortdate}.log"
它将在我的应用程序的ContentRoot文件夹中创建一个名为“ $ {basedir}”的空文件夹。它将日志文件放入bin \ Debug \ netcoreapp2.0中。
为什么不代替$ {basedir}?如果我使用日志文件夹的完整路径,它会很好地工作。
我应该补充一点,在<nlog>元素中,我在nlog.config中还具有以下内容:
internalLogFile="${basedir}/internal-nlog.txt"
当出现内部Nlog错误时,它将写入名为“ $ {basedir}”的文件夹中的该文件。
该${basedir}
文件夹已创建,因为NLog Internal Logger非常原始,并且不了解布局渲染器。
${basedir}
在internalLogFile中使用的支持Asp.Net.Core的解决方法可能如下(在中的Wiki示例中添加了两行Program.cs
):
var appBasePath = System.IO.Directory.GetCurrentDirectory();
NLog.GlobalDiagnosticsContext.Set("appbasepath", appBasePath);
var logger = LogManager.LoadConfiguration("nlog.config").GetCurrentClassLogger();
然后,您可以${gdc:item=appbasepath}
在nlog.config
:
<target xsi:type="File" name="allfile" fileName="${gdc:item=appbasepath}/Logs/nlog-all-${shortdate}.log"
在等待${aspnet-appbasepath}
准备就绪时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句