Log4Net在Sqlserver中初始化表结构

Log4net 使用不是即开即用,需要预先配置数据库,表,字段。

连接字符串示例(SQL server)为例

 这里包含了配置package,和字符串

这里需要的Package是System.Data.SqlClient,需要提前引入这个包才可以。Version不用动,我本地的System.Data.SqlClient是4.8.5,切换过去不好用,默认这个就可以了

<connectionType value="System.Data.SqlClient.SqlConnection,System.Data.SqlClient, Version=4.6.1.3, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<connectionString value="Data Source=192.168.0.12;Initial Catalog=Steven.Test.DB,1433;Persist Security Info=True;User ID=sa;Password=密码替换这里;TrustServerCertificate=true" /

 配置的Sql

<commandText value="INSERT INTO SystemLog ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />

需要改表名就在上面sql里面改一下就好了,我这里是SystemLog

数据库自己建,或者对齐现有自己的,我这个sql的表结构是Navicat导出的,直接全部复制即可

/*
 Navicat Premium Data Transfer


 Date: 14/08/2023 13:57:58
*/


-- ----------------------------
-- Table structure for SystemLog
-- ----------------------------
IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[SystemLog]') AND type IN ('U'))
    DROP TABLE [dbo].[SystemLog]
GO

CREATE TABLE [dbo].[SystemLog] (
  [Id] int  IDENTITY(1,1) NOT NULL,
  [Date] datetime2(7)  NOT NULL,
  [Thread] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL,
  [Level] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL,
  [Logger] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL,
  [Message] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL,
  [Exception] nvarchar(max) COLLATE SQL_Latin1_General_CP1_CI_AS  NULL
)
GO

ALTER TABLE [dbo].[SystemLog] SET (LOCK_ESCALATION = TABLE)
GO


-- ----------------------------
-- Auto increment value for SystemLog
-- ----------------------------
DBCC CHECKIDENT ('[dbo].[SystemLog]', RESEED, 1)
GO


-- ----------------------------
-- Primary Key structure for table SystemLog
-- ----------------------------
ALTER TABLE [dbo].[SystemLog] ADD CONSTRAINT [PK_SystemLog] PRIMARY KEY CLUSTERED ([Id])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)  
ON [PRIMARY]
GO

 

posted @ 2023-08-14 14:27  王月半子  阅读(18)  评论(0编辑  收藏  举报