【VS2022+ASP.Net Core+.net6】NLog 配置和使用

2022/2/22 9:23:38

本文主要是介绍【VS2022+ASP.Net Core+.net6】NLog 配置和使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

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

  

 

 

大家有问题可以与个人发邮件,收到邮件会第一时间回复大家!

 

自勉:“锲而不舍,金石可镂”

 



这篇关于【VS2022+ASP.Net Core+.net6】NLog 配置和使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程