zoukankan      html  css  js  c++  java
  • .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)查看很赞,(不需要密码直接登录)

  • 相关阅读:
    ff与ie 的关于js兼容性
    CSS清除浮动的方法
    java8 LocalDateTime
    BigDecimal
    JAVA将 Word 文档转换为 PDF
    Ionic4
    SpringBoot后端统一格式返回
    SpringBoot集成JWT
    Java Lombok
    SpringBoot 中通过 CORS 解决跨域问题
  • 原文地址:https://www.cnblogs.com/peterYong/p/13264985.html
Copyright © 2011-2022 走看看