TJC.Cyclops.LogLib 2025.12.2.1

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

Cyclops.LogLib

项目概述

Cyclops.LogLib是Cyclops框架中的日志收集组件,提供高效的日志批量收集、处理和数据库持久化功能。该组件采用单例模式实现,支持两种类型日志的收集:普通系统日志(包括异常日志)和API调用日志,并提供批处理机制优化数据库写入性能。

核心功能模块

1. 日志收集器

  • 单例模式实现的LoggerCollector,集中管理日志收集和处理流程
  • 自动注册日志事件,监听Logger的日志记录和错误事件
  • 支持服务名称自动获取和配置

2. 批处理机制

  • 基于Batcher<T>实现的日志批处理器
  • 分别针对普通日志(LogInfo)和API日志(ApiLogInfo)提供独立的批处理队列
  • 支持批处理完成事件和错误处理事件

3. 数据库持久化

  • 使用BaseRepository实现日志的数据库操作
  • 支持两种日志表的批量插入
  • 异常处理和错误重试机制

4. 设备信息解析

  • 自动从UserAgent中解析设备信息、操作系统和浏览器信息
  • 为API调用日志提供详细的客户端环境信息

技术栈

  • .NET 8.0 - 基础运行时环境
  • SqlSugar - ORM框架,用于数据库操作
  • Cyclops.Common - 通用工具类库
  • Cyclops.Orm - ORM封装组件

环境依赖

  • 数据库连接字符串配置(名为"LogsDB")
  • .NET 8.0运行时环境
  • Cyclops.Common和Cyclops.Orm组件

数据模型

DbLogError

异常日志表模型,包含以下关键字段:

  • ServiceName - 服务名称
  • Description - 日志描述
  • Parmas - 相关参数
  • Exception - 异常信息JSON字符串

DbLogApi

API调用日志表模型,包含以下关键字段:

  • ServiceName - 服务名称
  • CallIp - 调用方IP
  • Url - 请求地址
  • RequestMethod - 请求方式
  • Header - 请求头
  • UserAgent - 用户代理信息
  • Input - 输入参数
  • Output - 输出结果
  • StatusCode - HTTP状态码
  • UserId - 用户ID
  • Cost - 请求耗时
  • Device/Os/Ua - 解析后的设备信息

安装与配置

1. 安装组件

dotnet add package Cyclops.LogLib

2. 数据库配置

在应用程序配置文件中添加名为"LogsDB"的数据库连接字符串:

{
  "ConnectionStrings": {
    "LogsDB": "Data Source=your_server;Initial Catalog=LogsDB;User ID=your_user;Password=your_password;"
  }
}

3. 启动日志收集器

在应用程序启动时初始化并启动日志收集器:

// 在应用程序启动时
var loggerCollector = LoggerCollector.Instance;
loggerCollector.Start();

使用示例

1. 自动收集日志

配置完成后,通过Cyclops.Common.LogCom.Logger记录的日志会自动被收集:

// 记录普通日志
Logger.Info("这是一条信息日志");

// 记录错误日志
try
{
    // 可能抛出异常的代码
}
catch (Exception ex)
{
    Logger.Error("操作失败", ex);
}

// 记录API调用日志
Logger.ApiLog(new ApiLogInfo
{
    Url = "/api/users",
    RequestMethod = "GET",
    Input = "{\"id\":1}",
    Output = "{\"success\":true}",
    Cost = 100,
    StatusCode = 200,
    CallIp = "192.168.1.1",
    UserID = "user123"
});

2. 手动添加日志到批处理器

如果需要手动控制日志收集,可以直接使用批处理器:

// 直接获取实例并添加日志
var logCollector = LoggerCollector.Instance;
var logInfo = new LogInfo { Description = "手动添加的日志" };

// 这种方式不推荐,建议通过Logger来记录,让收集器自动处理

高级配置

批处理器配置

批处理器的行为(如批量大小、处理间隔等)可以在Cyclops.Common中进行配置。

启用/禁用数据库日志

通过DbConnectionOptions的EnableDBLogs属性控制是否将日志写入数据库:

// 在数据库配置中设置
_errorLogRepo.DbConnectionOptions.EnableDBLogs = true; // 默认值

注意事项

  • 对于高流量应用,建议适当调整批处理器配置以优化性能
  • 当数据库连接不可用时,组件会抛出异常并停止日志收集

贡献者

  • yswenli

许可证

保留所有权利

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 TJC.Cyclops.LogLib:

Package Downloads
TJC.Cyclops.Web.Core

企服版框架中api核心功能项目,基于aspnetcore集成di、jwt、swagger、codefirtst、支持多种常见数据库、nacos配置中心、统一接口回复参数、全局异常捕获、全局接口日志、防重放攻击、图形验证码、快捷上下文对象、上传下载、数据导入导出等功能

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2026.2.26.1 56 2/26/2026
2026.2.4.1 129 2/4/2026
2026.1.15.1 148 1/15/2026
2026.1.14.2 136 1/14/2026
2026.1.14.1 140 1/14/2026
2026.1.13.2 142 1/13/2026
2026.1.13.1 153 1/13/2026
2026.1.7.1 163 1/7/2026
2025.12.23.1 237 12/23/2025
2025.12.16.1 343 12/16/2025
2025.12.15.2 296 12/15/2025
2025.12.15.1 316 12/15/2025
2025.12.12.1 201 12/12/2025
2025.12.11.1 485 12/11/2025
2025.12.4.1 263 12/4/2025
2025.12.3.3 752 12/3/2025
2025.12.3.2 710 12/3/2025
2025.12.3.1 743 12/3/2025
2025.12.2.1 734 12/2/2025
2025.11.28.1 238 11/28/2025
Loading failed

企服版框架集成日志核心