1、新建NLog配置文件,名称分别为nlog.config和nlog.debug.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="${basedir}logsinternal-nlog.txt"> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <targets> <target name="allfile" xsi:type="File" fileName="${basedir}logsGDStationaryNetCore${shortdate}.log" encoding="utf-8" layout="[${longdate}][${machinename}][${level}] ${message} ${exception}" /> </targets> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog>
另一份配置文件与上面一样,修改保存日志的目录为其他目录
2、在Startup里的Configure方法里配置,并根据环境配置NLog配置文件
//根据环境变量切换NLog配置 var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); var isProd = environment == EnvironmentName.Production; env.ConfigureNLog(isProd ? "nlog.config" : "nlog.debug.config");
可在工程的属性 调试里修改环境变量