.NET Core NLog使用SQLite记录Log日志配置及示例代码
时间:2022-11-08
1、NLog使用SQLite的配置文件
<?xml version="1.0" encoding="utf-8"?>
http://www.nlog-project.org/schemas/NLog.xsd"
xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
"
autoReload="true"
throwExceptions="true"
internalLogIncludeTimestamp="true"
internalLogFile="nlog-internal.log"
internalLogLevel="Error">
<targets>
<target xsi:type="Database"
name="db"
dbProvider="System.Data.SQLite.SQLiteConnection, System.Data.SQLite"
connectionString="Data Source=database.db;">
<commandText>
insert into Logs (TimestampUtc, Application, Level, Message, Exception, Logger)
values (@timestamputc, @application, @level, @message, @exception, @logger);
</commandText>
<parameter name="@timestamputc" layout="${date:universalTime=true:format=yyyy-MM-dd HH\:mm\:ss.fff}" />
<parameter name="@application" layout="My App" />
<parameter name="@level" layout="${level}" />
<parameter name="@message" layout="${message}" />
<parameter name="@exception" layout="${exception:format=tostring}" />
<parameter name="@logger" layout="${logger}" />
</target>
</targets>
<rules>
<logger name="MyApp.*" minlevel="Info" writeTo="database" />
<logger name="*" minlevel="Warn" writeTo="database" />
</rules>
</nlog>
2、SQLite中建表语句
CREATE TABLE "Logs" (
`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`TimestampUtc` TEXT NOT NULL,
`Application` TEXT NOT NULL,
`Level` TEXT NOT NULL,
`Message` TEXT NOT NULL,
`Logger` TEXT NOT NULL,
`Exception` TEXT )
3、安装引用System.Data.SQLite Nuget包
Package Manager
PM> Install-Package System.Data.SQLite -Version 1.0.112
或
.NET CLI
> dotnet add package System.Data.SQLite --version 1.0.112
界面管理器安装方法:VS(Visual Studio)中Nuget的使用
相关文档:.NET Core和ASP.NET Core 日志框架Nlog安装配置及示例代码
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。