BitzOrcas 提供7 类审计系统,包含分片存储表、异步基于 Channel 的管道、实体级变更跟踪和可配置的保留策略。
架构
请求 → RequestAuditMiddleware → IAuditLogger │ ▼ ChannelAuditQueue (无界 Channel<T>) │ ▼ 后台 AuditWriter │ ┌──────────────────┼──────────────────┐ ▼ ▼ ▼ SysAuditLog SysActivityLog SysExternalRequestLogRecord SysSpecialLog SysCommunicationLog SysEntityPropertyChangesLog + SysSpecialLog7 类审计
| 类别 | 表 | 用途 |
|---|---|---|
| 常规 | SysAuditLog | 一般审计跟踪条目 |
| 活动 | SysActivityLog | 用户活动跟踪 |
| 实体属性变更 | SysEntityPropertyChangesLog | 字段级变更跟踪 |
| 通信 | SysCommunicationLog | 入站/出站 API 调用 |
| 外部请求 | SysExternalRequestLogRecord | 带详情的出站 HTTP 调用 |
| 特殊 | SysSpecialLog | 特殊/敏感事件 |
实体变更跟踪
SqlSugarEntityChangeAuditor 跟踪任何实体的字段级变更:
{ "entityType": "NoteEntity", "entityId": "abc123", "changes": [ { "property": "Title", "oldValue": "旧标题", "newValue": "新标题" }, { "property": "Content", "oldValue": "...", "newValue": "..." } ]}审计管道组件
| 组件 | 位置 | 用途 |
|---|---|---|
IAuditLogger | BitzOrcas.Infrastructure | 入队审计条目 |
ChannelAuditQueue | BitzOrcas.Infrastructure | 后台队列处理器 |
AuditEntryNormalizer | BitzOrcas.Infrastructure | 规范化/验证条目 |
IAuditLogStore | BitzOrcas.Infrastructure | 持久化端口 |
SqlSugarAuditLogStore | Infra.SqlSugar.Auditing | SqlSugar 实现 |
IAuditQueryPort | BitzOrcas.Infrastructure | 只读查询端口 |
IAuditRetentionPort | BitzOrcas.Infrastructure | 保留清理端口 |
IAuditTableInitializer | BitzOrcas.Infrastructure | Schema 初始化端口 |
分片实体层次
审计表使用具有内置分片的独立实体层次:
AuditEntityBase (雪花 Id)├── AuditTenantEntityBase (+ TenantId)│ └── AuditTenantSoftDeleteEntityBase (+ IsDeleted)│ ├── SysAuditLogEntity│ ├── SysActivityLogEntity│ ├── SysCommunicationLogEntity│ ├── SysExternalRequestLogRecordEntity│ ├── SysEntityPropertyChangesLogEntity│ └── SysSpecialLogEntity保留策略
通过 JobHost 中的 Quartz.NET 配置:
{ "Audit": { "Retention": { "Enabled": true, "CronExpression": "0 0 2 * * ?", "MaxAgeDays": 90 } }}AuditRetentionQuartzJob 按 Cron 调度运行,调用 IAuditRetentionPort.CleanupAsync() 清除超过保留期的记录。
审计排除
使用 [AuditIgnore] 特性排除特定请求处理器或属性的审计:
[AuditIgnore]public class PingQueryHandler : IRequestHandler<PingQuery, Pong> { }审计端点
| 端点 | 方法 | 描述 |
|---|---|---|
/api/audit/logs | GET | 分页审计日志查询 |
/api/audit/activity | GET | 活动日志查询 |
/api/audit/entity-changes | GET | 实体变更历史 |
/api/audit/external-requests | GET | 外部请求审计 |