增加了操作日志
This commit is contained in:
@@ -120,4 +120,31 @@ public class MonitorController : ControllerBase
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取系统操作日志(读取最新的 50 条)
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("system-logs")]
|
||||||
|
public IActionResult GetSystemLogs()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var logPath = "user_actions.log";
|
||||||
|
if (!System.IO.File.Exists(logPath))
|
||||||
|
{
|
||||||
|
return Ok(new List<string> { "暂无操作记录" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取文件 -> 取最后50行 -> 倒序排列(最新在前)
|
||||||
|
var logs = System.IO.File.ReadLines(logPath)
|
||||||
|
.TakeLast(50)
|
||||||
|
.Reverse()
|
||||||
|
.ToList();
|
||||||
|
return Ok(logs);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, $"读取日志失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -91,7 +91,12 @@ public class Program
|
|||||||
builder.Logging.AddFilter("System", LogLevel.Warning);
|
builder.Logging.AddFilter("System", LogLevel.Warning);
|
||||||
builder.Logging.AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Warning);
|
builder.Logging.AddFilter("Microsoft.AspNetCore.Hosting.Diagnostics", LogLevel.Warning);
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers(options =>
|
||||||
|
{
|
||||||
|
// 注册全局操作日志过滤器
|
||||||
|
options.Filters.Add<UserActionFilter>();
|
||||||
|
});
|
||||||
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(c =>
|
builder.Services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,18 +1,35 @@
|
|||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
|
||||||
|
namespace SHH.CameraSdk;
|
||||||
|
|
||||||
public class UserActionFilter : IActionFilter
|
public class UserActionFilter : IActionFilter
|
||||||
{
|
{
|
||||||
|
// 静态锁,防止多线程同时写文件报错
|
||||||
|
private static readonly object _logLock = new object();
|
||||||
|
|
||||||
public void OnActionExecuted(ActionExecutedContext context)
|
public void OnActionExecuted(ActionExecutedContext context)
|
||||||
{
|
{
|
||||||
|
// 只记录非 GET 请求(即修改性质的操作)
|
||||||
if (context.HttpContext.Request.Method != "GET")
|
if (context.HttpContext.Request.Method != "GET")
|
||||||
{
|
{
|
||||||
var user = context.HttpContext.Connection.RemoteIpAddress;
|
try
|
||||||
var path = context.HttpContext.Request.Path;
|
{
|
||||||
var method = context.HttpContext.Request.Method;
|
var user = context.HttpContext.Connection.RemoteIpAddress?.ToString() ?? "Unknown";
|
||||||
|
var path = context.HttpContext.Request.Path;
|
||||||
|
var method = context.HttpContext.Request.Method;
|
||||||
|
var time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
|
||||||
|
|
||||||
// 写入全局操作日志文件或数据库
|
var logLine = $"{time} | IP: {user} | {method} {path}";
|
||||||
File.AppendAllText("user_actions.log", $"{DateTime.Now} | {user} | {method} {path}\n");
|
|
||||||
|
lock (_logLock)
|
||||||
|
{
|
||||||
|
// 追加写入到运行目录下的 user_actions.log
|
||||||
|
File.AppendAllText("user_actions.log", logLine + Environment.NewLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { /* 忽略日志写入错误,不要影响业务 */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnActionExecuting(ActionExecutingContext context) { }
|
public void OnActionExecuting(ActionExecutingContext context) { }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user