FormatLog 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package FormatLog --version 1.0.3
                    
NuGet\Install-Package FormatLog -Version 1.0.3
                    
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="FormatLog" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FormatLog" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="FormatLog" />
                    
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 FormatLog --version 1.0.3
                    
#r "nuget: FormatLog, 1.0.3"
                    
#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 FormatLog@1.0.3
                    
#: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=FormatLog&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=FormatLog&version=1.0.3
                    
Install as a Cake Tool

FormatLog 日志组件方案

方案简介

本方案包含两个项目:

  • FormatLog:高性能结构化日志组件,支持参数化、格式去重、调用上下文、批量写入、游标分页查询。
  • DemoWPF:WPF 演示项目,展示日志写入、查询、富文本渲染等功能。

FormatLog 组件核心

主要功能

  • 结构化日志格式:通过 Format 唯一约束,日志内容参数化,便于检索分析。
  • 参数化日志Log 支持最多 10 个参数,自动归档参数表,避免重复存储。
  • 调用上下文追踪CallerInfo 记录成员、文件、行号,便于定位。
  • 高性能批量写入FLog 双缓冲队列+后台线程,自动批量写入 SQLite,进程退出自动 Flush。
  • 高效分页查询:支持游标分页、双向翻页、条件筛选(级别、时间、格式、参数、调用者等)。
  • 异常持久化:Flush 异常自动保存 JSON/TXT,便于排查。

组件结构

  • FormatLog/Format.cs 日志格式定义,唯一性约束,批量插入 SQL。
  • FormatLog/Log.cs 日志主实体,包含格式、参数、调用上下文、创建时间等。
  • FormatLog/Argument.cs 日志参数实体,唯一性约束。
  • FormatLog/CallerInfo.cs 调用上下文实体,唯一性约束。
  • FormatLog/LogDbContext.cs EF Core 数据库上下文,自动建表,按日期分库。
  • FormatLog/FLog.cs 日志池管理、批量写入、异常处理、分页查询。
  • 其他辅助类:QueryModelFlushInfoKeysetPage

快速入门

  1. 写入日志
FLog.Add(new Log(LogLevel.Info, "用户登录:{0}@{1}", userName, domain).WithCallerInfo());
  1. 查询日志(分页、筛选)
    var query = new QueryModel()
        .WithLevel(LogLevel.Info)
        .WithFormat("登录")
        .OrderBy(OrderType.OrderByIdDescending)
        .WithCursorId(nextCursorId);

    var page = await query.KeysetPaginationAsync();

设计亮点

  • 格式去重:日志格式、参数、调用上下文均自动去重,节省存储空间。
  • 批量高效:双缓冲队列+批量 SQL 插入,极大提升写入性能。
  • 易扩展:支持自定义筛选、分页、异常处理。
  • 数据库分库:按天分库,便于归档和维护。

适用场景

  • 高并发日志写入的桌面/服务端应用
  • 结构化日志分析、检索系统
  • 需要追踪调用上下文、参数的调试/运维场景

依赖

  • .NET 8
  • Microsoft.EntityFrameworkCore.Sqlite

DemoWPF 演示项目

主要功能

  • 日志写入演示:支持多种类型日志(系统信息、乘法、除法、随机字符串、长/短文本、时间戳、用户/磁盘/网络信息等)批量写入。
  • 日志查询与筛选:支持按格式、参数、调用者、级别、时间范围等多条件筛选,支持游标分页、双向翻页。
  • 富文本渲染:日志内容参数高亮显示,支持分段富文本。
  • 性能统计:实时显示日志写入性能(每百条写入耗时)。

主要界面

  • 日志写入页:选择日志类型和等级,批量写入演示。
  • 日志查询页:多条件筛选、分页浏览、富文本高亮。

运行方式

  1. 安装 .NET 8 SDK。
  2. 运行 DemoWPF 项目(WinExe,WPF)。
  3. 依赖 MahApps.Metro(UI美化)、FormatLog(日志核心)。

集成 FormatLog 步骤

  1. 引用 FormatLog 项目或 NuGet 包。
  2. 使用 FLog.Add(new Log(...).WithCallerInfo()) 写入日志。
  3. 使用 QueryModel 进行分页查询。
  4. WPF 可用 LogViewModel 进行富文本分段渲染。

目录结构

FormatLog/
  ├─ Format.cs
  ├─ Log.cs
  ├─ Argument.cs
  ├─ CallerInfo.cs
  ├─ LogDbContext.cs
  ├─ FLog.cs
DemoWPF/
  ├─ LogViewModel.cs
  ├─ LogTextSegment.cs
  ├─ MainWindow.xaml(.cs)

依赖

  • .NET 8
  • Microsoft.EntityFrameworkCore.Sqlite
  • MahApps.Metro(DemoWPF UI)

常见问题

  • 日志写入异常会自动持久化到 JSON/TXT 文件,便于排查。
  • 日志数据库按天分库,便于归档和维护。

如需详细 API 文档或二次开发建议,请查阅源码注释或联系维护者。

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on FormatLog:

Package Downloads
EFCoreLayerKit

基于 Entity Framework Core 的通用数据访问层工具包,支持自动仓储注册、DTO 映射、软删除、自动迁移等,适用于 .NET 8+ 项目。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 189 7/27/2025
1.0.6 290 7/26/2025
1.0.5 517 7/23/2025
1.0.4 513 7/22/2025
1.0.3 131 7/17/2025
1.0.2 119 7/17/2025
1.0.1 123 7/17/2025
1.0.0 126 7/16/2025