做一個簡單的日誌數據庫
功能不需要特別繁瑣
主要就是記錄普通日誌和錯誤日誌(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
{
/// <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語句
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.幫助操作類
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.使用
RhLogDbHelper rldh = new RhLogDbHelper(new RhLogRecordDtoDbAccessSqlServer());
rldh.LogInfo("hhahaha");
try
{
int a = 1;
int b = a / 0;
}
catch (Exception ex)
{
rldh.LogError("除法異常",ex);
}
5.結果
這只是很簡單的一個日誌數據庫
後面還要加上更多功能