您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了.net core webapi如何通过中间件获取请求和响应内容,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
本文主要根据中间件来实现对.net core webapi
中产生的请求和响应数据进行获取并存入日志文件中;
这里不详细介绍日志文件的使用。你可以自己接入NLog,log4net,Exceptionless等
创建接口记录的中间件
using Microliu.Core.Loggers; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Internal; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ptibro.Partner.API.Extensions { public class RequestResponseLoggingMiddleware { private readonly RequestDelegate _next; private readonly ILogger _logger; private SortedDictionary<string, object> _data; private Stopwatch _stopwatch; public RequestResponseLoggingMiddleware(RequestDelegate next, ILogger logger) { _next = next; _logger = logger; _stopwatch = new Stopwatch(); } public async Task Invoke(HttpContext context) { _stopwatch.Restart(); _data = new SortedDictionary<string, object>(); HttpRequest request = context.Request; _data.Add("request.url", request.Path.ToString()); _data.Add("request.headers", request.Headers.ToDictionary(x => x.Key, v => string.Join(";", v.Value.ToList()))); _data.Add("request.method", request.Method); _data.Add("request.executeStartTime", DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // 获取请求body内容 if (request.Method.ToLower().Equals("post")) { // 启用倒带功能,就可以让 Request.Body 可以再次读取 request.EnableRewind(); Stream stream = request.Body; byte[] buffer = new byte[request.ContentLength.Value]; stream.Read(buffer, 0, buffer.Length); _data.Add("request.body", Encoding.UTF8.GetString(buffer)); request.Body.Position = 0; } else if (request.Method.ToLower().Equals("get")) { _data.Add("request.body", request.QueryString.Value); } // 获取Response.Body内容 var originalBodyStream = context.Response.Body; using (var responseBody = new MemoryStream()) { context.Response.Body = responseBody; await _next(context); _data.Add("response.body", await GetResponse(context.Response)); _data.Add("response.executeEndTime", DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); await responseBody.CopyToAsync(originalBodyStream); } // 响应完成记录时间和存入日志 context.Response.OnCompleted(() => { _stopwatch.Stop(); _data.Add("elaspedTime", _stopwatch.ElapsedMilliseconds + "ms"); var json = JsonConvert.SerializeObject(_data); _logger.Debug(json, "api", request.Method.ToUpper()); return Task.CompletedTask; }); } /// <summary> /// 获取响应内容 /// </summary> /// <param name="response"></param> /// <returns></returns> public async Task<string> GetResponse(HttpResponse response) { response.Body.Seek(0, SeekOrigin.Begin); var text = await new StreamReader(response.Body).ReadToEndAsync(); response.Body.Seek(0, SeekOrigin.Begin); return text; } } /// <summary> /// 扩展中间件 /// </summary> public static class RequestResponseLoggingMiddlewareExtensions { public static IApplicationBuilder UseRequestResponseLogging(this IApplicationBuilder app) { return app.UseMiddleware<RequestResponseLoggingMiddleware>(); } } }
在startup.cs中Configure方法中使用中间件
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseErrorHandling();// 全局异常尽量放上面 ... app.UseRequestResponseLogging(); ... app.UseExceptionless(Configuration); app.UseMvc(); }
现在请求一次看一下记录的效果:我的日志存在exceptionless上,如下图
解析json,记录的数据如下:
感谢你能够认真阅读完这篇文章,希望小编分享的“.net core webapi如何通过中间件获取请求和响应内容”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。