VassasCo.Utility.LogHelper 2.0.0

dotnet add package VassasCo.Utility.LogHelper --version 2.0.0
                    
NuGet\Install-Package VassasCo.Utility.LogHelper -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VassasCo.Utility.LogHelper" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="VassasCo.Utility.LogHelper" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="VassasCo.Utility.LogHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add VassasCo.Utility.LogHelper --version 2.0.0
                    
#r "nuget: VassasCo.Utility.LogHelper, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package VassasCo.Utility.LogHelper@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=VassasCo.Utility.LogHelper&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=VassasCo.Utility.LogHelper&version=2.0.0
                    
Install as a Cake Tool

VassasCo.Utility.LogHelper

轻量级异步文件日志库(桌面端/服务端通用),零外部依赖。
「分类」决定文件、「级别」写入时配置,支持按天分目录、跨天自动切换、批量刷盘、定时清理与可靠关闭。

MIT


目录


功能总览

类别 能力
异步写入 有界队列 + 后台单线程消费,调用方不阻塞
分类分文件 分类决定文件名(Api.logDatabase.log…),级别作为写入参数
级别过滤 低于最低级别的日志直接丢弃
文件组织 按「分类」分文件、按「天」分目录(yyyy-MM/MM-dd
跨天切换 零点自动关闭旧文件、新建当天文件
批量刷盘 定时批量 Flush,避免每条日志触发 IO
定时清理 按「月/日」目录安全解析并删除过期日志
可靠关闭 停止接收 → 排空队列 → 关闭 writer,不丢日志
异常回调 后台写入/清理错误经 OnError 上报,不静默
线程安全 AddLog 可任意线程并发调用

目标框架与依赖

目标框架 依赖
netstandard2.0 无(仅 BCL)
net6.0 无(仅 BCL)
net8.0 无(仅 BCL)

快速开始

using VassasCo.Utility;

// 启动全局日志器
LogManager.Current = LogHelper.Build()
    .SetLogPath("D:/Logs")
    .SetMinLevel(LogLevel.Info)
    .SetRetentionDays(30)
    .Start();

// 扩展方法:方法名即分类,级别作为参数
"用户登录成功".LogSecurity(LogLevel.Info);
"数据库连接失败".LogDatabase(LogLevel.Error);
"接口响应超时".LogApi(LogLevel.Error, "订单服务");   // 模块为可选字段

// 程序退出时可靠关闭(排空队列不丢日志)
LogManager.Shutdown();

核心设计:分类与级别

「分类」与「级别」是两个正交维度:

维度 类型 作用
分类 LogCategories 常量(自由字符串) 决定写入哪个文件Api.logDatabase.log…)
级别 LogLevel 枚举 写入时作为参数传入,用于最低级别过滤与内容标注

LogLevel 枚举:Trace < Debug < Info < Warning < Error < Fatal

LogCategories 常量:

常量 说明
General 默认/通用
Security 安全/鉴权
Performance 性能指标
Business 业务逻辑
Audit 审计
Operation 运维/操作
TimerTask 定时任务
System 系统/框架
Database 数据库访问
Api 接口调用
Network 网络通信

写入 API

扩展方法(按分类命名,方法名决定文件)

message.Log()                    // General
message.LogSecurity(level)       // Security
message.LogPerformance(level)    // Performance
message.LogBusiness(level)       // Business
message.LogAudit(level)          // Audit
message.LogOperation(level)      // Operation
message.LogTimerTask(level)      // TimerTask
message.LogSystem(level)         // System
message.LogDatabase(level)       // Database
message.LogApi(level)            // Api
message.LogNetwork(level)        // Network

统一签名:

Log{Category}(this string message, LogLevel level = LogLevel.Info, string? module = null)

直接写入(任意自定义分类)

logger.AddLog("Payment", LogLevel.Error, "支付失败", "微信支付");
//             ↑分类(决定文件)  ↑级别        ↑消息       ↑可选模块

异常写入

exception.LogError(LogCategories.Api);   // Error 级别,递归展开 InnerException

文件组织与日志格式

LogPath/
└── 2025-06/
    └── 06-17/
        ├── General.log
        ├── Api.log
        ├── Database.log
        ├── Security.log
        └── ...

按「分类」分文件、按「天」分目录(yyyy-MM/MM-dd)。

日志格式:

[2025-06-17 12:30:45.123] [Error] [订单服务] 接口响应超时
  • 分类体现在文件名里,不再写入每行内容。
  • [级别] 始终写入;[模块] 仅在传入 module 时出现。

配置参考

LogHelperBuilder(链式配置)

方法 默认值 说明
SetLogPath(path) "Logs" 日志根目录
SetMinLevel(level) Info 最低记录级别,低于则丢弃
SetRetentionDays(days) 30 日志保留天数(最小 1)
SetQueueCapacity(n) 100000 有界队列容量(最小 100),满时丢弃新日志并触发回调
SetAutoFlushInterval(t) 2s 批量刷盘间隔
SetCleanupInterval(t) 1h 过期日志清理扫描间隔
EnableDailyCleanup(bool) true 是否启用按天自动清理
OnError(handler) null 后台写入/清理错误回调
Start() 构建并启动日志器

可靠性特性

  • 异步写入:有界队列 + 后台单线程消费,AddLog 立即返回不阻塞调用方。
  • 跨天自动切换StreamWriter 按「分类 + 日期」缓存,零点后自动关闭旧文件、新建当天文件,杜绝日志写进前一天的 bug。
  • 批量刷盘:不再每条 Flush,改为定时批量落盘,显著降低 IO 开销。
  • 定时清理:按「月/日」目录安全解析并删除过期日志。
  • 可靠关闭Dispose / Shutdown 先停止接收、再排空队列、最后关闭所有 writer,不丢日志。

安全策略

策略 说明
分类文件名清洗 分类被用作文件名,自动移除非法文件名字符、路径分隔符、路径穿越(..),从根源杜绝路径注入
异常不静默 后台错误经 OnError 回调上报;回调自身异常被吞掉不影响主流程
线程安全 AddLog 可任意线程并发调用,内部用 BlockingCollection + 锁保证安全
内存可见性 _disposed 等跨线程标志用 Volatile / Interlocked 保证可见性

API 速查

LogHelper(实例)

方法 说明
Build() 创建链式构建器 LogHelperBuilder
AddLog(category, level, message, module?) 写入一条日志(线程安全)
Flush() 立即将所有缓冲日志刷盘
Dispose() 停止接收、排空队列、释放资源(幂等)

LogManager(静态门面)

成员 说明
Current 获取/设置全局日志器;读取未初始化时抛 InvalidOperationException
IsInitialized 是否已初始化
Shutdown() 停止并释放全局日志器(幂等)

LogHelperExtensions(扩展方法)

方法 说明
Log(level?, module?) General 分类
LogSecurity / LogPerformance / ... 对应分类,共 11 个
LogError(category?, module?) 记录异常为 Error 级别,递归展开 InnerException

测试

cd VassasCo.Utility
dotnet test

12 个单元测试覆盖:基本写入、按分类分文件、最低级别过滤、空分类回退、模块字段、分类文件名清洗、写入失败回调、可靠关闭排空、并发线程安全、分类扩展方法、异常链记录、全局管理器生命周期。


许可证

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on VassasCo.Utility.LogHelper:

Package Downloads
VassasCo.Utility

C# 桌面开发工具库 (WinForm / WPF / Avalonia): - ConfigHelper:零代码实体类⇋JSON/XML配置双向映射(带注释、热重载、原子保存、列表展开) - LogHelper:异步高性能日志系统(建造者模式、异步队列、自动清理、14种日志类型) - CrashDumpHelper:崩溃捕获+FirstChance异常追踪+MiniDump生成 - SnowflakeIdHelper:分布式雪花ID生成器(单调时钟杜绝回拨、集群WorkerId分配、ID反解) - ScheduleHelper:全能定时任务调度器(CRON/固定速率/固定延迟、重试退避、超时取消、日历过滤、线程池、优雅关闭) - RetryHelper:智能重试器(指数退避+抖动+断路器三态+降级+超时,同步/异步) - EventBus:进程内事件总线(类型发布/订阅、特性自动注册、优先级、条件过滤、粘性事件、多通道),支持 .NET Standard 2.0 / .NET 6 / .NET 8 / .NET 9 / .NET 10 - ExcelMapper:原生对象→Excel映射(嵌套类子表头合并、数组独立Sheet、Dictionary自适应、全可配置样式)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 5 9/23/2026
1.1.0 276 6/18/2026