C#使用SqlServer作為日誌資料庫的設計與實現
阿新 • • 發佈:2021-01-07
前言
做一個簡單的日誌資料庫
功能不需要特別繁瑣
主要就是記錄普通日誌和錯誤日誌(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.結果
這只是很簡單的一個日誌資料庫
後面還要加上更多功能
到此這篇關於C#使用SqlServer作為日誌資料庫的設計與實現的文章就介紹到這了,更多相關C#用SqlServer作日誌資料庫內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!