码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2020-04-02 13:08:45      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:sep   filename   date   and   div   director   level   mic   min   

$ {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

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

标签:sep   filename   date   and   div   director   level   mic   min   

原文地址:https://www.cnblogs.com/tianfengcc/p/12619025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!