$ {basedir}指向的是 AppDomain.CurrentDomain.BaseDirectory,
Asp.Net.Core的解决方法可能如下(在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
:
<?xml version="1.0"?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" internalLogLevel="Warn" internalLogFile="${gdc:item=appbasepath}loginternal-nlog.txt"> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <targets async="true"> <target name="ownfile" xsi:type="File" concurrentWrites="true" keepFileOpen="true" fileName="${gdc:item=appbasepath}log log-${shortdate}.log" encoding="utf-8" layout="${longdate}|${machinename}|${uppercase:${level}}|${logger}|${message}|${exception:format=tostring}|${aspnet-request-method}|${aspnet-traceidentifier}|${aspnet-request-ip}|${aspnet-request-url}|${aspnet-mvc-action}" /> </targets> <rules> <!--Skip non-critical Microsoft logs and so log only own logs--> <logger name="Microsoft.*" maxLevel="Info" final="true" /> <logger name="*" minlevel="Info" writeTo="ownfile" /> </rules> </nlog>
本文出自:http://tianfeng.cc/Article/5879