.NET 9.0 的 Blazor Web App 项目,自定义日志 TLog V2 使用备忘

一、TLog V1 使用静态数据库上下文,优点是速度快,缺点是内存占用大,参见 .NET 9.0 的 Blazor Web App 项目、Bootstrap Blazor 组件库、自定义日志 TLog 使用备忘_navigationmanager.tobaserelativepath-CSDN博客

二、 TLog V2 改为 依赖注入 方式,优点、缺点 与 V1 相反,使用方法与 V1 相同。

cs 复制代码
namespace BlazorWebAppNet9Shared.Services;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// TLog 日志服务:首次使用 自动创建 数据库和数据表。<br/><br/>
/// 使用示例,第1步:Program.cs 中,注册服务,builder.Services.AddTLog();<br/>
/// 使用示例,第2步:TLog.Page(UserName, DisplayName, NavigationManager.ToBaseRelativePath(NavigationManager.Uri));
/// </summary>
public static class TLog
{
    private static DbContextOptions<TLogDbContext>? contextOptions;

    /// <summary>
    /// 注册 TLog 服务,示例:builder.Services.AddTLog();
    /// </summary>
    /// <param name="services"></param>
    /// <param name="dbType"></param>
    /// <param name="connectionString"></param>
    /// <returns></returns>
    public static IServiceCollection AddTLog(this IServiceCollection services, TLogDbType dbType = TLogDbType.Sqlite, string connectionString = "Data Source=SQLiteFileTLog.db")
    {
        switch (dbType)
        {
            case TLogDbType.SqlServer:
                //注册 日志上下文
                services.AddDbContext<TLogDbContext>(options => options.UseSqlServer(connectionString));
                //记录 DbContextOptions 其他方法使用
                contextOptions = new DbContextOptionsBuilder<TLogDbContext>().UseSqlServer(connectionString).Options;
                break;
            default:
                services.AddDbContext<TLogDbContext>(options => options.UseSqlite(connectionString));
                contextOptions = new DbContextOptionsBuilder<TLogDbContext>().UseSqlite(connectionString).Options;
                break;
        }

        // 自动创建数据库和数据表:修改实体定义后,删除原有的数据库自动重新建立;或者根据实体定义手动修改数据库和数据表。
        using var tLogDbContext = new TLogDbContext(contextOptions);
        if (tLogDbContext.Database.EnsureCreated())
        {
            TLog.Info("初始化数据库:成功!");
            TLog.Console("初始化数据库:成功!");
        }
        return services;
    }

    /// <summary>
    /// 等价:System.Console.WriteLine($"TLog------------{输出信息}")
    /// </summary>
    /// <param name="输出信息"></param>
    public static void Console(string? 输出信息)
    {
        System.Console.WriteLine($"TLog------------{输出信息}");
    }

    /// <summary>
    /// 操作类型 = TLogOpStyle.其他信息
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    /// <param name="操作说明"></param>
    public static void Info(string 用户名, string 姓名, string 操作对象 = "", string 操作说明 = "")
    {
        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.其他信息, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象, 操作说明 = 操作说明 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.其他信息
    /// </summary>
    /// <param name="操作说明"></param>
    public static void Info(string 操作说明)
    {
        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.其他信息, 用户名 = "用户名", 姓名 = "姓名", 操作对象 = "", 操作说明 = 操作说明 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.页面访问
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    public static void Page(string 用户名, string 姓名, string 操作对象)
    {
        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.页面访问, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.新建
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    /// <param name="操作说明"></param>
    /// <param name="操作结果"></param>
    public static void Create(string 用户名, string 姓名, string 操作对象, string 操作说明 = "", bool 操作结果 = true)
    {
        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.新建, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象, 操作说明 = 操作说明, 操作结果 = 操作结果 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.删除
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    /// <param name="操作说明"></param>
    /// <param name="操作结果"></param>
    public static void Delete(string 用户名, string 姓名, string 操作对象, string 操作说明 = "", bool 操作结果 = true)
    {

        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.删除, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象, 操作说明 = 操作说明, 操作结果 = 操作结果 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();

    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.编辑
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    /// <param name="操作说明"></param>
    /// <param name="操作结果"></param>
    public static void Update(string 用户名, string 姓名, string 操作对象, string 操作说明 = "", bool 操作结果 = true)
    {

        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.编辑, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象, 操作说明 = 操作说明, 操作结果 = 操作结果 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }
    /// <summary>
    /// 操作类型 = TLogOpStyle.查询
    /// </summary>
    /// <param name="用户名"></param>
    /// <param name="姓名"></param>
    /// <param name="操作对象"></param>
    /// <param name="操作说明"></param>
    /// <param name="操作结果"></param>
    public static void Read(string 用户名, string 姓名, string 操作对象, string 操作说明 = "", bool 操作结果 = true)
    {

        using var tLogDbContext = new TLogDbContext(contextOptions!);
        tLogDbContext.Add(new TLogEntity() { 操作类型 = TLogOpStyle.查询, 用户名 = 用户名, 姓名 = 姓名, 操作对象 = 操作对象, 操作说明 = 操作说明, 操作结果 = 操作结果 });
        tLogDbContext.SaveChanges();
        tLogDbContext.Dispose();
    }

}

/// <summary>
/// 日志数据库上下文:建议使用独立数据库,与业务数据分开存放。
/// </summary>
/// <param name="options"></param>
public class TLogDbContext(DbContextOptions<TLogDbContext> options) : DbContext(options)
{
    public DbSet<TLogEntity> TLogEntitys { get; set; }
}

/// <summary>
/// 日志实体:后续可以根据需要增加,注意不要编辑、删除已有属性。
/// </summary>
[Index(nameof(Id))]
[Index(nameof(时间))]
[Index(nameof(用户名))]
[Index(nameof(姓名))]
[Index(nameof(操作类型))]
[Index(nameof(操作对象))]
[Index(nameof(操作类型), nameof(操作对象))]
public class TLogEntity
{
    [Key]
    public long Id { get; set; }
    public DateTime 时间 { get; set; } = DateTime.Now;
    public required string 用户名 { get; set; }
    public required string 姓名 { get; set; }
    public TLogOpStyle 操作类型 { get; set; } = TLogOpStyle.页面访问;
    public required string 操作对象 { get; set; }
    public string 操作说明 { get; set; } = "";
    public bool 操作结果 { get; set; } = true;
}

/// <summary>
/// 日志类型:后续可以根据需要增加,注意不要编辑、删除已有类型。
/// </summary>
public enum TLogOpStyle
{
    /// <summary>
    /// 记录页面访问日志:Page
    /// </summary>
    页面访问,
    /// <summary>
    /// 增删改查CRUD:Create
    /// </summary>
    新建,
    /// <summary>
    /// 增删改查CRUD:Read
    /// </summary>
    查询,
    /// <summary>
    /// 增删改查CRUD:Update
    /// </summary>
    编辑,
    /// <summary>
    /// 增删改查CRUD:Delete
    /// </summary>
    删除,
    /// <summary>
    /// Information:Info
    /// </summary>
    其他信息
}
/// <summary>
/// 日志使用数据库类型:默认使用 Sqlite
/// </summary>
public enum TLogDbType
{
    /// <summary>
    /// 使用 SQL Server database
    /// </summary>
    SqlServer,
    /// <summary>
    /// 使用 SQLite database
    /// </summary>
    Sqlite
}
相关推荐
云心雨禅1 天前
WordPress提速指南:Memcached+Super Static Cache+CDN缓存网站内容
linux·服务器·数据库·缓存·memcached
Mr.wangh1 天前
Redis主从复制
java·数据库·redis
穿背心的程序猿1 天前
推荐一款集成AI功能的数据库管理工具
数据库
周杰伦fans1 天前
C# 23种设计模式详解与示例
开发语言·设计模式·c#
柱子jason1 天前
使用IOT-Tree消息流【标签读写】功能详细说明
数据库·物联网·时序数据库·influxdb·iot-tree
鲲志说1 天前
电子证照系统国产化改造实践:从MongoDB到金仓数据库的平滑迁移与性能优化
大数据·数据库·mongodb·性能优化·数据库开发·数据库架构·金仓数据库
xb11321 天前
C#——方法的定义、调用与调试
开发语言·c#
范纹杉想快点毕业1 天前
单片机开发中的队列数据结构详解,队列数据结构在单片机软件开发中的应用详解,C语言
c语言·数据库·stm32·单片机·嵌入式硬件·mongodb·fpga开发
code bean1 天前
【C#】以 BlockingCollection 为核心的多相机 YOLO 检测任务处理框架
c#
William_cl1 天前
【连载2】 MySQL 事务原理详解
数据库·mysql