【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 配置和使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具