RT.MesWeb.Audit 1.2.0

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

Audit 使用说明

安装

dotnet add package RT.MesWeb.Audit

目标框架:net8.0 / net10.0

配置要求

  • 需要在 appsettings.json 中添加以下配置:
  • 必填:Audit:ConnectionString(无默认值,缺失时 ConfigureAudit 在启动注册阶段直接抛 InvalidOperationException)。DatabaseType 默认 SqlServer,RequestLogLength 默认 1000,EnableRequestLog 默认 true,EnableLoginLog/EnableOperationLog 默认 true。
{
    "Audit": {
        "ConnectionString": "Server=127.0.0.1;uid=sa;pwd=password;database=dbname;Trust Server Certificate=true;",
        "DatabaseType": "SqlServer",    // 可选值:SqlServer / Oracle / DM / MySql / PostgreSql(大小写不敏感;非法值启动即报错)
        "RequestLogLength": 1000,       // 日志记录请求内容长度上限,为0表示不限制长度,默认为1000
        "EnableRequestLog": true,       // 是否把动作参数写入 SYS_OPER_LOG.REQUEST;含密码/令牌等敏感参数时应设为 false
        "EnableLoginLog": true,         // 是否启用登录日志,默认为true
        "EnableOperationLog": true,     // 是否启用操作日志,默认为true
        "HostName": ""                  // 审计日志 HOST_NAME;为空时用服务端主机名,多节点部署可设为 MES-APP-1 等

    }
}

EnableRequestLog 是整体开关:关闭后所有操作日志的 REQUEST 字段为空。修改密码、新建用户等动作的参数里带明文密码,生产环境建议设为 false。

数据库要求

  • AuditEntities指向的数据库应当包含以下表:
    • SYS_LOGIN_LOG:登录日志。
    • SYS_OPER_LOG:操作日志。
  • AuditEntities指向的数据库应当包含以下序列:
    • SYS_SEQ_NO:用于生成日志编号的序列。
  • 数据库中应包含序号生成机制
    • SQL Server:应包含生成Sequence的存储过程和Sequence序列对象
    • Oracle:包含生成Sequence的序列对象即可
    • DM:包含生成Sequence的序列对象即可
    • MySQL:应包含序列表 sequence(NAME/CURRENT_VALUE/INCREMENT)、函数 CurrVal/NextVal/SetVal 及存储过程 aic_sp_get_next_sequence_value
    • PostgreSQL:包含生成Sequence的序列对象即可

记录规则

MesAuditDataProvider 按「控制器 + 动作」决定写哪张表:

接口 动作 写入
POST /api/authorize/token,GrantType=password LoginController.Token SYS_LOGIN_LOG(等同一次登录,用户名取请求体 Username)
POST /api/authorize/token,GrantType=refresh_token 等其它 grant LoginController.Token 不记录(刷新是会话续期,不是登录)
POST /api/authorize/login LoginController.LoginAsync SYS_LOGIN_LOG(旧接口,保留兼容)
POST /api/authorize/logout、/revoke、/heartbeat 同控制器 不记录(LogoutAsync 未标 [AuditIgnore],但提供程序对这三个动作一律返回 -1)
其它控制器的带 [Audit] 动作 — SYS_OPER_LOG

登录成功/失败原因通过 HttpContext.SetAuditScope(...) 写入审计消息,落库到 SYS_LOGIN_LOG.REMARK(如「登录成功,用户:admin,会话编码:…」「登录失败:…」)。

操作日志的登录类型(LOG_MODE / APP_ID)由 app.ConfigureAudit() 注册的作用域钩子在创建审计事件时从 Authorization 头解析(自动去掉 Bearer 前缀;非 JWT 或损坏的令牌按匿名处理),审计事件中不保留原始令牌。因此 app.ConfigureAudit() 未调用时,操作日志的登录类型为空、APP_ID 为 UnKnown。

SYS_LOGIN_LOG.CLIENT_ID 存登录/令牌请求里的 ClientId(OAuth2 公共客户端标识):/api/authorize/token 的 password grant 取请求体 ClientId;旧 /api/authorize/login(LoginRequestDto)没有 ClientId 字段,客户端编码留空。

SYS_LOGIN_LOG.HOST_NAME / SYS_OPER_LOG.HOST_NAME 优先取 Audit:HostName,未配置时用服务端主机名(Environment.MachineName),不再硬编码 MES-APP。

LOGIN_RESULT 存状态码:

  • /api/authorize/token(password grant):控制器通过 HttpContext.SetLoginResult(...) 写入 ApiResult.StatusCode 的取值——200 登录成功、400 请求参数错误、403 用户名或口令错误、409 多端冲突、500 服务端异常(统一响应下 HTTP 恒为 200,不能作为登录结果);
  • 旧 /api/authorize/login:维持原样,存 Action.ResponseStatus(同样是状态码字符串)。

使用说明

  • 应用程序构建时应调用以下方法:

// 在应用程序构建前调用以下方法以注册审计服务
services.ConfigureAudit(configuration);

// 在应用程序构建后调用以下方法以启用审计功能
app.ConfigureAudit();

  • 两处调用缺一不可:services.ConfigureAudit 负责绑定配置、注册 IHttpContextAccessor 与审计提供程序(连接串缺失/数据库类型非法会在此启动即失败);app.ConfigureAudit 注册审计作用域创建钩子(补充当前用户,并从 Authorization 头解析登录类型——只保留登录类型,不保留原始令牌),并把 ILogger<MesAuditDataProvider> 注入提供程序。
  • 审计写库失败不会中断业务请求:表缺失、连接失败、字段超长等异常会被捕获并返回 -1,异常通过 ILogger<MesAuditDataProvider> 输出(未调用 app.ConfigureAudit 时退化为 Trace)。因此排查"日志没落库"时先看应用日志里有没有「审计日志写入失败(…)」。
  • AuditEntities不用依赖注入,若需要使用AuditEntities进行数据库操作,可直接实例化对象:
using (var dbContext = new AuditEntities(options))
{
    // 使用dbContext进行数据库操作
}
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 is compatible.  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 RT.MesWeb.Audit:

Package Downloads
RT.MesWeb.Authentication

Authentication server for mes system

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 123 11/2/2026
1.1.0 219 7/3/2026
1.0.1 214 5/8/2026
1.0.0 153 4/27/2026