• .Net Core程序中记录日志


    NLog

    NLog是适用于各种.NET平台(包括.NET standard)的灵活、’免费的日志记录平台。使用NLog可以轻松地写入多个目标(数据库,文件,控制台)并即时更改日志记录配置。

    Nuget包的引用

    NLog.Extensions.Logging

    创建nlog.config配置文件

    同样适用于Linux环境,将在当前执行目录下的logs目录中打印日志

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          internalLogFile="/logs/console-example-internal.log"
          internalLogLevel="Info" >
    
    
      <!-- the targets to write to -->
      <targets>
        <!-- write logs to file -->
        <target xsi:type="File" name="target1" fileName="${basedir}/logs/${shortdate}/console-example.log"
                layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
        <target xsi:type="Console" name="target2"
                layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
    
    
      </targets>
    
      <!-- rules to map from logger name to target -->
      <rules>
        <logger name="*" minlevel="Trace" writeTo="target1,target2" />
    
      </rules>
    </nlog>
    View Code

    使用

    var logger = LogManager.GetLogger("*");
    logger.Info("这个信息");
    logger.Debug("这个调试");
    logger.Error("这个是错误");
    
     //LogManager.Shutdown 来实现数据刷新并且关闭内部所有的线程和定时器。

     NLog日志框架使用探究

    其他日志参考

    玩转ASP.NET Core中的日志组件

    github:Microsoft.Extensions.Logging 日志组件拓展其他 

    其中,UI(http://xxxxx:xx/logging)查看很赞,(不需要密码直接登录)

  • 相关阅读:
    开课 博客
    给定数组求数组中和最大子数组的和
    课堂测验
    读梦断代码有感(3)2019.2.20
    读梦断代码有感(2)2019.2.10
    读梦断代码有感(1)2019.2.05
    进度七
    进度 六
    sjz地铁作业
    进度四
  • 原文地址:https://www.cnblogs.com/peterYong/p/13264985.html
Copyright © 2020-2023  润新知