asp.net core 3 使用nlog日志组件,使用$ {basedir}保存位置不对,记录下怎么解决...

$ {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}\log\internal-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\nlog-${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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值