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>
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 )