【NLog 配置和使用】
1 NuGet包引入:NLog.Web.AspNetCore、System.Data.SqlClient
2 添加 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"> <variable name="myvar" value="myvalue"/> <targets> <!--Error保存至文件--> <target name="error_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8" fileName="${basedir}/NLogs/${date:yyyyMMdd}_Error.TXT" archiveFileName="${basedir}/NLogs/${date:yyyyMMdd}_Error.{#}.TXT" archiveDateFormat="yyyyMMdd" archiveAboveSize="104857600" archiveNumbering="Sequence" layout="${date:yyyy-MM-dd HH\:mm\:ss} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" /> <!--Trace保存至文件--> <target name="trace_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8" fileName="${basedir}/NLogs/${date:yyyyMMdd}_Trace.TXT" archiveFileName="${basedir}/NLogs/${date:yyyyMMdd}_Trace.{#}.TXT" archiveDateFormat="yyyyMMdd" archiveAboveSize="104857600" archiveNumbering="Sequence" layout="${date:yyyy-MM-dd HH\:mm\:ss} ${uppercase:${level}}: ${message}" /> <!--配置Sql Server--> <target name="database" xsi:type="Database" dbProvider="System.Data.SqlClient.SqlConnection,System.Data.SqlClient" connectionString="Data Source=localhost;Initial Catalog=数据库名;Persist Security Info=True;User ID=账户;Password=密码" commandText="insert into 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> </targets> <rules> <!-- add your logging rules here --> <logger name="*" minlevel="Trace" maxlevel="Warn" writeTo="trace_file" /> <!--<logger name="*" minlevel="Debug" writeTo="debugger" />--> <logger name="*" minlevel="Error" writeTo="error_file" /> <!--配置Sql Server--> <logger name="*" minlevel="Info" writeTo="database" /> </rules> </nlog>
3 Program.cs中注册
4 构造函数中注入得到实例并写日志
使用实例写日志,日志示例:
运行后,写到文件中的日志,与数据库中的日志是一样的。
注:数据库脚本:
USE [数据库名] GO /****** Object: Table [dbo].[NLog] Script Date: 2022-02-22 00:57:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[NLog]( [Id] [bigint] 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, CONSTRAINT [PK_NLog] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
大家有问题可以与个人发邮件,收到邮件会第一时间回复大家!
自勉:“锲而不舍,金石可镂”