86 lines
3.3 KiB
C#
86 lines
3.3 KiB
C#
using APT.Infrastructure.Core;
|
||
using log4net;
|
||
using Microsoft.AspNetCore.Mvc.Filters;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace APT.Infrastructure.Core
|
||
{
|
||
public class LogAttribute : Attribute, IAsyncActionFilter
|
||
{
|
||
/// <summary>
|
||
/// 添加操作日志 liyouming
|
||
/// </summary>
|
||
/// <param name="context"></param>
|
||
/// <param name="next"></param>
|
||
/// <returns></returns>
|
||
public async Task OnActionExecutionAsync(Microsoft.AspNetCore.Mvc.Filters.ActionExecutingContext context, ActionExecutionDelegate next)
|
||
{
|
||
|
||
try
|
||
{
|
||
|
||
string actionArguments = context.ActionArguments.ToJson();
|
||
var resultContext = await next();
|
||
|
||
string url = resultContext.HttpContext.Request.Host + resultContext.HttpContext.Request.Path + resultContext.HttpContext.Request.QueryString;
|
||
|
||
string method = resultContext.HttpContext.Request.Method;
|
||
|
||
dynamic result = resultContext.Result.GetType().Name == "EmptyResult" ? new { Value = "EmptyResult" } : resultContext.Result as dynamic;
|
||
|
||
var userHeads = resultContext.HttpContext.Request.Headers;
|
||
var userid = string.Empty;
|
||
var username = string.Empty;
|
||
if (userHeads.ContainsKey("userid"))
|
||
{
|
||
userid = userHeads["userid"].ToString();
|
||
}
|
||
if (userHeads.ContainsKey("username"))
|
||
{
|
||
username = userHeads["username"].ToString();
|
||
}
|
||
string response = JsonConvert.SerializeObject(result.Value);
|
||
//var str = $"用户ID:[{userid}],姓名:[{username}],api地址:[{url}],方法:[{method}],接口参数:[{actionArguments}],应答:[{response}]";
|
||
//var points = new Point
|
||
//{
|
||
// Name = "ApiLog",
|
||
// Timestamp = DateTime.Now,
|
||
// Tags = new Dictionary<string, object>()
|
||
//{
|
||
// { "Id", Guid.NewGuid()}
|
||
//},
|
||
// Fields = new Dictionary<string, object>()
|
||
//{
|
||
// { "UserId", userid },
|
||
// { "UserName", username },
|
||
// { "ApiUrl", url },
|
||
// { "Method", method },
|
||
// { "ActionArguments", actionArguments },
|
||
// { "Response", response }
|
||
//},
|
||
//};
|
||
|
||
|
||
LoggerManager.GetLogger().Info("时序数据库开始写入数据");
|
||
//var ret = await InfluxDbHelper.InfluxDbClient.Client.WriteAsync(points, InfluxDbHelper.InfluxLogDbName);
|
||
//LoggerManager.GetLogger().Info("时序数据库插入结果:" + ret.Success);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LoggerManager.GetLogger().Error(Infrastructure.Core.LogHelper.GetCurSourceFileName() + " - " + LogHelper.GetLineNum() + ":" + ex.Message);
|
||
//throw;
|
||
}
|
||
finally
|
||
{
|
||
await next.Invoke();
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|