.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)查看很赞,(不需要密码直接登录)

log4.net  .NET Core WebApi下使用log4.Net日志    log4net使用详解

 

posted @ 2021-02-27 10:48  peterYong  阅读(247)  评论(0编辑  收藏  举报