Implement API security baseline (sys_user, JWT, audit fields, transactions, unique constraints) and Web real login with 401 handling. Document setup and development workflows in root, WmsLite.Api, and WmsLite.Web README files. Co-authored-by: Cursor <cursoragent@cursor.com>
58 lines
1.4 KiB
C#
58 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace WmsLite.Api.Infrastructure.HttpLogging;
|
|
|
|
/// <summary>
|
|
/// 将当前请求内 SqlSugar 产生的 SQL 收集到 <see cref="HttpContext.Items"/>,供 HTTP 日志合并输出。
|
|
/// </summary>
|
|
public static class HttpSqlLogContext
|
|
{
|
|
public const string ItemsKey = "WmsLite.HttpSqlLogBuffer";
|
|
|
|
public sealed class Buffer
|
|
{
|
|
private readonly List<string> _sql = new();
|
|
private readonly object _gate = new();
|
|
|
|
public void Append(string sql)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(sql))
|
|
{
|
|
return;
|
|
}
|
|
|
|
lock (_gate)
|
|
{
|
|
_sql.Add(sql.Trim());
|
|
}
|
|
}
|
|
|
|
public IReadOnlyList<string> Snapshot()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _sql.ToArray();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void StartRequest(HttpContext context)
|
|
{
|
|
context.Items[ItemsKey] = new Buffer();
|
|
}
|
|
|
|
public static void Append(HttpContext? context, string executableSql)
|
|
{
|
|
if (context?.Items[ItemsKey] is Buffer buffer)
|
|
{
|
|
buffer.Append(executableSql);
|
|
}
|
|
}
|
|
|
|
public static IReadOnlyList<string> GetSqlSnapshot(HttpContext context)
|
|
{
|
|
return context.Items[ItemsKey] is Buffer b ? b.Snapshot() : System.Array.Empty<string>();
|
|
}
|
|
}
|