YYZ.LOGGER 1.0.1

dotnet add package YYZ.LOGGER --version 1.0.1
                    
NuGet\Install-Package YYZ.LOGGER -Version 1.0.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="YYZ.LOGGER" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YYZ.LOGGER" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="YYZ.LOGGER" />
                    
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 YYZ.LOGGER --version 1.0.1
                    
#r "nuget: YYZ.LOGGER, 1.0.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 YYZ.LOGGER@1.0.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=YYZ.LOGGER&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=YYZ.LOGGER&version=1.0.1
                    
Install as a Cake Tool

YyzLogger,让日志自动化规范化,yyds

适合API项目

后台使用LiteDB、消息队列插入数据库的方式,可配置指定时间删除归档数据库日志文件。

  • appsetting.json配置以下选项:
"Yyzlog": {
    "LogFolderName": "log",
    "RetentionDays": 3,
    "CleanupHour": 17,
    "CleanupMinute": 5
}
  • program.cs 注册:
using YYZ.LOGGER;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// 注册 YYZ 日志组件
builder.Services.AddYyzLogger(builder.Configuration);
var app = builder.Build();
// 启动后台日志清理任务
using (var scope = app.Services.CreateScope())
{
   var logger = scope.ServiceProvider.GetRequiredService<IyyzLogger>();
   await logger.StartBacgroudTick();
}
app.UseHttpsRedirection();
app.MapControllers();
app.Run();
  • controller 注入:
  private readonly IyyzLogger _logger;

  public TestController(IyyzLogger logger)
  {
      _logger = logger;
  }
  • controller 使用:

await _logger.WriteBatch(entries);  //批量插入日志
await _logger.WriteOne(log);  //插入单条

/// <summary>
/// 读取日志。
/// level: 0=全部,1=Info,2=Warn,3=Error。
/// BillNo: 单据号过滤,可空。
/// container: 容器号过滤,可空。
/// platform: 业务平台过滤,可空。
/// order: "asc" 或 "desc",为空时默认为时间升序。
/// page: 页码,从1开始。
/// size: 每页大小。
/// </summary>
[HttpGet("logs")]
public async Task<ActionResult<List<YlogEntry>>> GetLogs(
   int level = 0,
   DateTime? t_start = null,
   DateTime? t_end = null,
   string? BillNo = null,
   string? container = null,
   string? platform = null,
   string? order = "asc",
   int page = 1,
   int size = 100)
{
   var start = t_start ?? DateTime.Today.AddDays(-7);
   var end = t_end ?? DateTime.Now;
   
   var logs = await _logger.ReadLog(level, start, end, BillNo, container, platform, order, page, size);
   return Ok(logs);
}

/// <summary>
/// 批量删除符合条件的日志。
/// level: 0=全部,1=Info,2=Warn,3=Error。
/// t_start: 开始时间,必须。
/// t_end: 结束时间,必须。
/// BillNo: 单据号过滤,可空。
/// container: 容器号过滤,可空。
/// platform: 业务平台过滤,可空。
/// </summary>
[HttpDelete("logs")]
public async Task<IActionResult> DeleteLogs(
   int level = 0,
   DateTime? t_start = null,
   DateTime? t_end = null,
   string? BillNo = null,
   string? container = null,
   string? platform = null)
{
   if (!t_start.HasValue || !t_end.HasValue)
   {
       return BadRequest("开始时间和结束时间不能为空");
   }

   // 调用删除方法
   var result = await _logger.DeleteLogByCondition(
       level, 
       t_start.Value, 
       t_end.Value, 
       BillNo, 
       container, 
       platform);

   return Ok(new 
   { 
       Success = result.Success, 
       AffectedCount = result.AffectedCount, 
       Message = result.Message 
   });
}

  • YLogEntry实体类源码:

public class YlogEntry
{
    /// <summary>
    /// 日志ID(大写 UID 字符串)
    /// </summary>
    [BsonId]
    public string Uid { get; set; } = Guid.NewGuid().ToString("N").ToUpperInvariant();

    /// <summary>
    /// 日志级别:1-Info,2-Warn,3-Error,0 仅用于查询(全部)
    /// </summary>
    public int Level { get; set; }

    /// <summary>
    /// 日志时间(含日期)
    /// </summary>
    public DateTime LogTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 日志内容(5000 字以内)
    /// </summary>
    public string Content { get; set; } = string.Empty;

    /// <summary>
    /// 业务平台(可空)
    /// </summary>
    public string? Platform { get; set; }

    /// <summary>
    /// 事件类别(可空)
    /// </summary>
    public string? EventCategory { get; set; }

    /// <summary>
    /// 单据号(可空)
    /// </summary>
    public string? BillNo { get; set; }

    /// <summary>
    /// 容器号(可空)
    /// </summary>
    public string? ContainerNo { get; set; }

    /// <summary>
    /// 方法名称(可空)
    /// </summary>
    public string? MethodName { get; set; }
}

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 174 1/15/2026
1.0.0 117 1/14/2026

增加了按条件删除日志的方法