前言
做一个简单的日志数据库
功能不需要特别繁琐
主要就是记录普通日志和错误日志(INFO,ERROR)
用数据库作为日志有好处也有坏处
相比于文本来说 更加容易操作
后期查看日志可以根据时间筛选
当然要求也多了点 没那么灵活了
首先你的PC上还要安装一个SqlServer
本来是想用log4net配置去实现的
发现配置很繁琐 决定自己设计一个 肯定有不少不足之处
分为以下几个步骤
1.建立日志数据表
都用一个表来存放,那么字段就要多设置一个 用来区分不同的日志类型
具体怎么设置 也很简单 字段很简单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RH.Iot.DomainModel.RhLogDto { /// <summary> /// SqlServer数据库记录日志传输模型 /// </summary> [SugarTable( "LogRecord" )] public class RhLogRecordDtoSqlServer { /// <summary> /// 索引 /// </summary> [SugarColumn(IsPrimaryKey = true , IsIdentity = true )] //主键并且自增 (string不能设置自增) public int Id { get ; set ; } /// <summary> /// 日期 /// </summary> public string DateTime { get ; set ; } /// <summary> /// 日志等级名称 /// </summary> public string LevelName { get ; set ; } /// <summary> /// 信息 /// </summary> public string Message { get ; set ; } /// <summary> ///异常 /// </summary> public string Exception { get ; set ; } /// <summary> /// 无参构造器 /// </summary> public RhLogRecordDtoSqlServer() { } /// <summary> /// 有参构造器 /// </summary> public RhLogRecordDtoSqlServer( int Id, string DateTime, string LevelName, string Message, string Exception) { this .Id = Id; this .DateTime = DateTime; this .LevelName =LevelName; this .Message = Message; this .Exception = Exception; } } } |
我这里用到了SqlSugar这个Orm框架
不会的话可以去学一下 用数据库少不了与这个框架打交道
如果你已经初步了解了SqlSugar 请再看一下它的仓储概念
然后引入你的程序 如果你不想也可以 你子要可以保证自己的程序可以访问数据库并且进行基本的插入数据操作就好了
上面是数据库表的映射类
那么表的建立和它的Sql语句
1
2
3
4
5
6
7
8
|
CREATE TABLE [dbo].[LogRecord] ( [Id] INT IDENTITY (1, 1) NOT NULL , [DateTime] NVARCHAR (20) NULL , [LevelName] NCHAR (10) NULL , [Message] NVARCHAR ( MAX ) NULL , [Exception] NVARCHAR ( MAX ) NULL , PRIMARY KEY CLUSTERED ([Id] ASC ) ); |
2.建立相关的数据访问层
我这里使用了仓储 ,你也可以使用自己的方式
3.帮助操作类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
using Microsoft.Extensions.Logging; using RH.Iot.DomainModel.RhLogDto; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RH.Iot.DbAccess.RhSqlServerDbAccess.RhLogDbAccess { /// <summary> /// LogDbHelper 数据库日志操作 /// 使用SqlServer做数据存储 /// 目前提供异常和普通日志记录 /// 方法待扩充 /// 如果遇到数据库连接不上的问题 日志模式回归到txt模式(规划中...) /// </summary> public class RhLogDbHelper { /// <summary> /// 模型 /// </summary> public RhLogRecordDtoSqlServer rhLogRecordDtoSqlServer; /// <summary> /// 私有化数据访问器 /// </summary> private RhLogRecordDtoDbAccessSqlServer DbAccess; /// <summary> /// 构造器注入 /// </summary> /// <param name="dbAccess">提供相应的数据访问类</param> public RhLogDbHelper(RhLogRecordDtoDbAccessSqlServer dbAccess) { DbAccess = dbAccess; rhLogRecordDtoSqlServer = new RhLogRecordDtoSqlServer(); } public void LogInfo( string msg) { rhLogRecordDtoSqlServer.DateTime = DateTime.Now.ToString(); rhLogRecordDtoSqlServer.LevelName = "INFO" ; rhLogRecordDtoSqlServer.Message = msg; DbAccess.InsertAsync(rhLogRecordDtoSqlServer); } public void LogError( string msg,Exception ex) { rhLogRecordDtoSqlServer.DateTime = DateTime.Now.ToString(); rhLogRecordDtoSqlServer.LevelName = "ERROR" ; rhLogRecordDtoSqlServer.Message = msg; rhLogRecordDtoSqlServer.Exception = ex.ToString(); DbAccess.InsertAsync(rhLogRecordDtoSqlServer); } } } |
这些内容不可直接复制
因为项目引用不一样
但可以参考
4.使用
1
2
3
4
5
6
7
8
9
10
11
12
|
RhLogDbHelper rldh = new RhLogDbHelper( new RhLogRecordDtoDbAccessSqlServer()); rldh.LogInfo( "hhahaha" ); try { int a = 1; int b = a / 0; } catch (Exception ex) { rldh.LogError( "除法异常" ,ex); } |
5.结果
这只是很简单的一个日志数据库
后面还要加上更多功能
到此这篇关于C#使用SqlServer作为日志数据库的设计与实现的文章就介绍到这了,更多相关C#用SqlServer作日志数据库内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_38083655/article/details/112171120