.NET6 引用NLog记录日志

第一步:引用Nuget包
NLog.Web.AspNetCore
第二步:引用配置文件 并右键配置文件属性,将“复制到输出目录”设置为“始终复制”
<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets> 
    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    --> <!-- target 节点 用于配置记录方式  是文本记录  还是 数据库记录 -->
<!-- 数据库日志记录--> <target name="AllDatabase" xsi:type="Database" dbProvider="System.Data.SqlClient.SqlConnection, System.Data.SqlClient" connectionString="Data Source=DESKTOP-T2D6ILD;Initial Catalog=LogManager;Persist Security Info=True;User ID=sa;Password=sa123" commandText="insert into dbo.NLog (Application, Logged, Level, Message,Logger, CallSite, Exception) values (@Application, @Logged, @Level, @Message,@Logger, @Callsite, @Exception);"> <parameter name="@application" layout="AspNetCoreNlog" /> <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:filename=true}" /> <parameter name="@exception" layout="${exception:tostring}" /> </target> <!-- 文件记录,将日志写入 txt 文本文件 --> <target xsi:type="File" name="allfile" fileName="NLog\nlog-all-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <!--同样是将文件写入日志中,写入的内容有所差别,差别在layout属性中体现。写入日志的数量有差别,差别在路由逻辑中体现--> <target xsi:type="File" name="ownFile-web" fileName="NLog\nlog-my-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="Null" name="blackhole" /> <!-- Write events to a file with the date in the filename. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> --> </targets>
<!--rules 节点 用于配置 记录日志规则 例如 日志记录等级过滤 等--> <rules> <logger name="*" minlevel="Trace" writeTo="AllDatabase" /> <!-- add your logging rules here --> <!--路由顺序会对日志打印产生影响。路由匹配逻辑为顺序匹配。--> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs--> <!--以Microsoft打头的日志将进入此路由,由于此路由没有writeTo属性,所有会被忽略--> <!--且此路由设置了final,所以当此路由被匹配到时。不会再匹配此路由下面的路由。未匹配到此路由时才会继续匹配下一个路由--> <logger name="Microsoft.*" minlevel="Trace" final="true" /> <!--上方已经过滤了所有Microsoft.*的日志,所以此处的日志只会打印除Microsoft.*外的日志--> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" <logger name="*" minlevel="Debug" writeTo="f" /> --> </rules> </nlog>

 第三步:在program.cs文件中加入如下代码

builder.Logging.AddNLog("CfgFile/NLog.config");

第四步:跑起来,查看日志是否存在  就ok 喽

posted @ 2023-03-07 17:26  有追求的小码农  阅读(106)  评论(0)    收藏  举报