.NET 6 API + Middleware + Audit rail

Request相关的参数。 需要在Program.cs 注入IHttpContextAccessor

cs 复制代码
 //Below services used to get token
         services.AddHttpContextAccessor();
         services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
cs 复制代码
        public string GetClientIpAddress()
        {
            var clientIp = _httpContextAccessor?.HttpContext?.Connection?.RemoteIpAddress?.ToString();

            return clientIp!;
        }

        public string GetBrowserInfo()
        {
            var browserInfo = _httpContextAccessor?.HttpContext?.Request?.Headers["User-Agent"].ToString();

            return string.IsNullOrEmpty(browserInfo) ? string.Empty : browserInfo;
        }

        public string GetMethodName()
        {
            var url = _httpContextAccessor?.HttpContext?.Request.Path;
            return string.IsNullOrEmpty(url) ? string.Empty : url;
        }


        public string GetParametersInfo()
        {
            var parameter = string.Empty;
            try
            {
                var request = _httpContextAccessor?.HttpContext?.Request;
                if (request != null)
                {
                    var methond = request.Method;
                    Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();

                    if (methond == "GET")
                    {
                        if (request.Query != null && request.Query.Count() > 0)
                        {
                            foreach (var item in request.Query)
                            {
                                keyValuePairs.Add(item.Key, item.Value);
                            }
                            parameter = JsonConvert.SerializeObject(keyValuePairs);
                        }
                    }
                    else
                    {
                        if (request.HasFormContentType)
                        {
                            //For Upload documents
                            if (request.Form.Keys != null)
                            {
                                foreach (var itemFormKey in request.Form.Keys)
                                {
                                    keyValuePairs.Add(itemFormKey, request.Form[itemFormKey]);
                                }
                            }

                            if (request.Form.Files != null && request.Form.Files.Count() > 0)
                            {
                                keyValuePairs.Add($"Attachments-{Guid.NewGuid()}", string.Join("; ", request.Form.Files.Select(x => x.FileName)));
                            }

                            parameter = JsonConvert.SerializeObject(keyValuePairs);
                        }
                        else if (request.Query != null && request.Query.Count() > 0)
                        {
                            foreach (var item in request.Query)
                            {
                                keyValuePairs.Add(item.Key, item.Value);
                            }
                            parameter = JsonConvert.SerializeObject(keyValuePairs);
                        }
                        if (request.HasJsonContentType())
                        {
                            var rawMessage = GetRawBodyAsync(request).Result;
                            parameter += rawMessage;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                parameter = ex.Message;
                Log.Error(ex, $"GetParametersInfo failed. {ex.Message}");
            }

            return parameter;
        }

Create GlobalRequestHandlingMiddleware

cs 复制代码
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Newtonsoft.Json.Serialization;
using NotImplementedException = System.NotImplementedException;
using UnauthorizedAccessException = System.UnauthorizedAccessException;
using KeyNotFoundException = System.Collections.Generic.KeyNotFoundException;
using Serilog;
using System.Diagnostics;
	
	/// <summary>
    /// Web API Audit Trail and Exception Result handler
    /// </summary>
    public class GlobalRequestHandlingMiddleware
    {
        private readonly RequestDelegate _next;

        private readonly IAuditTrailManager _auditTrailManager;

        public GlobalRequestHandlingMiddleware(RequestDelegate next, IAuditTrailManager auditTrailManager)
        {
            _next = next;
            _auditTrailManager = auditTrailManager ?? throw new ArgumentNullException(nameof(auditTrailManager));
        }

        public async Task Invoke(HttpContext context)
        {

            Stopwatch stopwatch = new();
            stopwatch.Start();

            try
            {
                auditTrailLogDto auditInfo = _auditTrailManager.GenerateWebAPIAuditTrailLogWithoutSaveToDb();

                await _next(context);

                stopwatch.Stop();
                auditInfo.ExecutionDuration = (int)stopwatch.ElapsedMilliseconds;
                _auditTrailManager.InsertWebAPIAuditTrailLogs(auditInfo);
            }
            catch (Exception ex)
            {
                await HandleExceptionAsync(context, ex, stopwatch);
            }
        }

        private Task HandleExceptionAsync(HttpContext context, Exception exception, Stopwatch stopwatch)
        {
			/**
			//GenerateWebAPIAuditTrailLogWithoutSaveToDb
			 var auditInfo = new AbpAuditLogDto
			 {
				 ServiceName = serviceName,//_httpContextAccessor?.HttpContext?.Request.Path
				 ClientIpAddress = GetClientIpAddress(),
				 BrowserInfo = GetBrowserInfo(),
				 Parameters = GetParametersInfo(),
				 MethodName = GetMethodName(),
				 ExecutionTime = DateTime.UtcNow,
			 };
			
			
			*/
            AuditTrailLogDto auditInfo = _auditTrailManager.GenerateWebAPIAuditTrailLogWithoutSaveToDb();//generate audittrail

            HttpStatusCode status = HttpStatusCode.BadRequest;
            var stackTrace = exception.StackTrace;
            string message = string.Empty;
            Guid errorCode = Guid.NewGuid();

            var exceptionType = exception.GetType();
            if (exceptionType == typeof(UserFriendlyException))
            {
                var userException = (exception as UserFriendlyException);
                status = userException?.Code ?? HttpStatusCode.BadRequest;
                message = userException?.Details ?? exception.Message;
            }
            else if (exceptionType == typeof(BadRequestException))
            {
                message = exception.Message;
                status = HttpStatusCode.BadRequest;
            }
            else if (exceptionType == typeof(NotFoundException) || exceptionType == typeof(KeyNotFoundException))
            {
                message = exception.Message;
                status = HttpStatusCode.NotFound;
                stackTrace = exception.StackTrace;
            }
            else if (exceptionType == typeof(NotImplementedException))
            {
                status = HttpStatusCode.NotImplemented;
                message = exception.Message;
            }
            else if (exceptionType == typeof(UnauthorizedAccessException))
            {
                status = HttpStatusCode.Unauthorized;
                message = exception.Message;
            }
            else
            {
                status = HttpStatusCode.InternalServerError;
                message = exception.Message;
                stackTrace = exception.StackTrace;
            }
            Log.Error(exception, $"ServiceName: {auditInfo.ServiceName}. Status Code: {status},message: {message}.");
  

            GlobalExceptionResponse exceptionResponseBase = new()
            {
                Success = false,
                Error = new()
                {
                    Message = message,
                    Code = (int)status,
                    Details = message,
                    StackTrace = stackTrace,
                    Id = errorCode,
                    DateTime = DateTime.UtcNow
                }
            };

            stopwatch.Stop();
            auditInfo.Exception = $"Status Code: {status},message: {message}. Details: {exception}";
            auditInfo.ExecutionDuration = (int)stopwatch.ElapsedMilliseconds;
            _auditTrailManager.InsertWebAPIAuditTrailLogs(auditInfo);//save to db

            var exceptionResult = JsonConvert.SerializeObject(exceptionResponseBase, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
            context.Response.ContentType = HttpHeaderConstant.RESPONSE_HEADER_APPLICATION_JSON;
            context.Response.StatusCode = (int)status;
            Log.Error(exception, $"ServiceName: {auditInfo.ServiceName}. Status Code: {status},message: {message}. User Id: {auditInfo.UserId}. RemoteIpAddress: {auditInfo.ClientIpAddress}. {exceptionResult}");
            if (context != null)
            {
                return context.Response.WriteAsync(exceptionResult);
            }
            else
            {
                return Task.FromResult(exceptionResult);
            }
        }
    }

Use Middleware

cs 复制代码
    /// <summary>
    /// Web API Audit Trail and Exception Result handler
    /// </summary>
    public static class ApplicationBuilderExtensions
    {
        /// <summary>
        /// Web API Audit Trail and Exception Result handler
        /// </summary>
        /// <param name="applicationBuilder"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseGlobalRequestHandling(this IApplicationBuilder applicationBuilder)
            => applicationBuilder.UseMiddleware<GlobalRequestHandlingMiddleware>();
    }

Register in Program.cs

cs 复制代码
         //Below services used to get token
         services.AddHttpContextAccessor();
         services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
         services.AddSingleton<IPrincipalAccessor, PrincipalAccessor>();

           app.UseGlobalRequestHandling();//add Global Resust HandlingMiddleware
相关推荐
霍先生的虚拟宇宙网络7 分钟前
.net 支持跨平台(桌面)系列技术汇总
.net
djk888825 分钟前
.net的winfrom程序 窗体透明&打开窗体时出现在屏幕右上角
.net
运维&陈同学1 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
是阿建吖!1 小时前
【Linux】进程状态
linux·运维
明明跟你说过2 小时前
Linux中的【tcpdump】:深入介绍与实战使用
linux·运维·测试工具·tcpdump
Mr_Xuhhh3 小时前
重生之我在学环境变量
linux·运维·服务器·前端·chrome·算法
九鼎科技-Leo10 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
朝九晚五ฺ10 小时前
【Linux探索学习】第十四弹——进程优先级:深入理解操作系统中的进程优先级
linux·运维·学习
Kkooe11 小时前
GitLab|数据迁移
运维·服务器·git
久醉不在酒12 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql