C# 使用SqlServer作为日志数据库,设计与实现

做一个简单的日志数据库
功能不需要特别繁琐
主要就是记录普通日志和错误日志(INFO,ERROR)
用数据库作为日志有好处也有坏处
相比于文本来说 更加容易操作
后期查看日志可以根据时间筛选
当然要求也多了点 没那么灵活了
首先你的PC上还要安装一个SqlServer
本来是想用log4net配置去实现的
发现配置很繁琐 决定自己设计一个 肯定有不少不足之处

分为以下几个步骤
1.建立日志数据表
都用一个表来存放,那么字段就要多设置一个 用来区分不同的日志类型
具体怎么设置 也很简单 字段很简单

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RH.Iot.DomainModel.RhLogDto
{
///
/// SqlServer数据库记录日志传输模型
///
[SugarTable(“LogRecord”)]
public class RhLogRecordDtoSqlServer
{
///
/// 索引
///
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//主键并且自增 (string不能设置自增)
public int Id { get; set; }
///
/// 日期
///
public string DateTime { get; set; }
///
/// 日志等级名称
///
public string LevelName { get; set; }
///
/// 信息
///
public string Message { get; set; }
///
///异常
///
public string Exception { get; set; }
///
/// 无参构造器
///
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;

    }
}

}

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

我这里用到了SqlSugar这个Orm框架
不会的话可以去学一下 用数据库少不了与这个框架打交道

如果你已经初步了解了SqlSugar 请再看一下它的仓储概念

然后引入你的程序 如果你不想也可以 你子要可以保证自己的程序可以访问数据库并且进行基本的插入数据操作就好了

上面是数据库表的映射类
那么表的建立和它的Sql语句

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)
);

1
2
3
4
5
6
7
8

2.建立相关的数据访问层
我这里使用了仓储 ,你也可以使用自己的方式

3.帮助操作类

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);
        

    }
}

}

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
60
61

这些内容不可直接复制
因为项目引用不一样
但可以参考

4.使用

RhLogDbHelper rldh = new RhLogDbHelper(new RhLogRecordDtoDbAccessSqlServer());
rldh.LogInfo(“hhahaha”);
try
{
int a = 1;
int b = a / 0;
}
catch (Exception ex)
{

            rldh.LogError("除法异常",ex);
        }

1
2
3
4
5
6
7
8
9
10
11
12

5.结果
在这里插入图片描述
这只是很简单的一个日志数据库
后面还要加上更多功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值