开源DDD设计模式框架YMNNetCoreFrameWork第6篇-.net Core Logging和Nlog结合

源码地址:https://github.com/topgunymn/YMNNetCoreFrameWork

遇到的坑:使用了Nlog以后,.NETcore自带的日志等级不起作用,只有nlog配置配置文件才能控制他的日志等级

1、.NET core增加NLog日志,.NET CORE自带的日志无法保存到文件,所以使用NLOG

2、下载NLog.Web.AspNetCore

3、注入nlog

//添加NLog
loggerFactory.AddNLog();
//读取Nlog配置文件
env.ConfigureNLog("NLog.config");

 

4、配置文件

<?xml version="1.0" encoding="utf-8"?>

<!--<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Info"
      internalLogFile="c:\temp\internal-nlog.txt">-->


<!-- 自动查找Nlog.XML文件-->

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   autoReload="true"
      throwExceptions="true"
      internalLogLevel="Off">
  <!--variable name="logDirectory" value="${basedir}\log\"/>
   -->
  <!--定义日志要写入的目标对象,可写入file,database,email等-->
  <targets>
    <!--记录bug错误的文件,比较严重的错误,如未知的异常错误-->
    <target xsi:type="File" name="bugerror" fileName="${basedir}\bugerror\${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}|${message} ${exception}" />
    <!--记录异常信息,一般的错误,如程序抛出的已知异常-->
    <target xsi:type="File" name="error" fileName="${basedir}\error\${shortdate}.log"
            layout="日期:${longdate} 级别:${level}|记录者: ${logger} url: ${aspnet-request-url} 方法:: ${callsite} 内容:${message} 异常信息: ${exception}" />
    <!--// 记录普通日志,-->
    <target xsi:type="File" name="info" fileName="${basedir}\info\${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}|${message} ${exception}" />
    <!--//记录特殊日志,按记录类型分类-->
    <target xsi:type="File" name="sepcial" fileName="${basedir}\sepcial\${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}|${message} ${exception}" />
  </targets>

  <!--//  规则,定义loggner.name到target.name的对应关系-->
  <rules>
    <!--//写入到bugerror目标的日志-->
    <logger name="*" level="Fatal" writeTo="bugerror" enabled="true" />
    <!--// 写入到error目标的日志-->
    <logger name="*" level="Error" writeTo="error" enabled="true" />
    <!--/写入到info目标的日志  Trace,Debug,Info,  -->
    <logger name="*" levels="Warn" writeTo="info" enabled="true" />
    <!--//写入到特殊日志目标,示例:LogManager.GetLogger("special")对象写入的所有日志用本规则-->

    <logger name="special" minlevel="Trace" writeTo="sepcial" />
  </rules>

  <!--</nlog>-->






  <!--define various log targets-->
  <!--
  <targets>

    <target xsi:type="Null" name="blackhole" />
    <target xsi:type="Database" name="database" 
       
      connectionString="Data Source=.;Initial Catalog=WisdomMedicalDB;User ID=sa;Password=123456" >
      <commandText>
        insert into ApplicationLogs (
        Application, Logged, Level, Message,
        Logger, Callsite, Exception
        ) values (
        @Application, @Logged, @Level, @Message,
        @Logger, @Callsite, @Exception
        );
      </commandText>

      <parameter name="@application" layout="兰德视迅智慧医院" />
      <parameter name="@logged" layout="${date}" />
      <parameter name="@level" layout="${level}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@callSite" layout="${callsite}" />
      <parameter name="@exception" layout="${exception:tostring}" />
    </target>


  </targets>

  <rules>
    -->
  <!--Skip Microsoft logs and so log only own logs  evels="Debug,Info,Error,Warn"  -->
  <!--
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*"  writeTo="database" />

  </rules>-->
</nlog>

5、注入nlog,        private readonly ILogger _logger = NLog.LogManager.GetCurrentClassLogger();

6、使用

  /// <summary>
        /// 测试日志
        /// </summary>
        /// <returns></returns>
        [HttpGet("TestLog")]

        public async  Task<object> TestLog() {


            _logger.Error("测试错误");
            _logger.Debug("测试debug");
            _logger.Info("测试info");
            _logger.Warn("测试warn");
            //_logger.LogError("测试错误");
            //_logger.LogDebug("测试debug");
            //_logger.LogInformation("测试info");
            //_logger.LogWarning("测试warn");
            return true;
        }

 

 

posted on 2020-02-07 20:54  topguntopgun  阅读(423)  评论(0编辑  收藏  举报

导航