NLog在.NET6中的使用

1 使用Nuget引入NLog包

NLog.Web.AspNetCore

2 Program.cs中添加引用

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

3 代碼結構

 

 4 NLog.config文件配置

<?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 name="AllDatabase" xsi:type="Database"
              dbProvider="System.Data.SqlClient.SqlConnection, System.Data.SqlClient"
              connectionString="Data Source=DESKTOP-KUQBMBC;Initial Catalog=LogManager; Integrated Security=true;"
              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>

        <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>
        <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>
NLog.config

5 數據庫腳本

create table Log4Net
(
    Id int identity(1,1) not null,
    Date datetime not null,
    Thread varchar(255) not null,
    [Level] varchar(50) not null,
    Logger varchar(255) not null,
    Message varchar(4000) not null,
    Exception varchar(2000) null
)

create table NLog
(
    Id int identity(1,1) not null,
    Application nvarchar(50) not null,
    Logged datetime not null,
    Level nvarchar(50) not null,
    Message nvarchar(max) not null,
    Logger nvarchar(250) null,
    Callsite nvarchar(max) null,
    Exception nvarchar(max) null
)
LogManager.sql

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章