c# – 请求响应记录的响应主体

c# – 请求响应记录的响应主体,第1张

概述我正在尝试编写一个Owin midleware组件,它将记录每个传入的请求和对数据库的响应. 这是我设法得到多远. 我一直在阅读回复.说: Stream does not support reading. 我如何阅读Response.Body? public class LoggingMiddleware : OwinMiddleware { private static Log 我正在尝试编写一个Owin mIDleware组件,它将记录每个传入的请求和对数据库的响应.

这是我设法得到多远.

我一直在阅读回复.说:

Stream does not support reading.

我如何阅读Response.Body?

public class LoggingMIDdleware : OwinMIDdleware {        private static Logger log = LogManager.GetLogger("WebAPI");        public LoggingMIDdleware(OwinMIDdleware next,IAppBuilder app)            : base(next)        {        }    public overrIDe async Task Invoke(IOwinContext context)    {        using (var db = new HermesEntitIEs())        {            var sw = new Stopwatch();            sw.Start();            var logRequest = new log_Request            {                Body = new StreamReader(context.Request.Body).ReadToEndAsync().Result,headers = Json.Encode(context.Request.headers),IPTo = context.Request.LocalipAddress,IpFrom = context.Request.RemoteIpAddress,Method = context.Request.Method,Service = "API",Uri = context.Request.Uri.ToString(),Username = context.Request.User.IDentity.name            };            db.log_Request.Add(logRequest);            context.Request.Body.position = 0;            await Next.Invoke(context);            var mem2 = new MemoryStream();            await context.Response.Body.copyToAsync(mem2);            var logResponse = new log_Response            {                headers = Json.Encode(context.Response.headers),Body = new StreamReader(mem2).ReadToEndAsync().Result,ProcessingTime = sw.Elapsed,ResultCode = context.Response.StatusCode,log_Request = logRequest            };            db.log_Response.Add(logResponse);            await db.SaveChangesAsync();        }    }}
@R_403_6120@ Response Body可以这种方式记录:
public class LoggingMIDdleware : OwinMIDdleware{    private static Logger log = LogManager.GetLogger("WebAPI");    public LoggingMIDdleware(OwinMIDdleware next,IAppBuilder app)        : base(next)    {    }    public overrIDe async Task Invoke(IOwinContext context)    {        using (var db = new HermesEntitIEs())        {           var sw = new Stopwatch();           sw.Start();           var logRequest = new log_Request           {               Body = new StreamReader(context.Request.Body).ReadToEndAsync().Result,Username = context.Request.User.IDentity.name           };           db.log_Request.Add(logRequest);           context.Request.Body.position = 0;           Stream stream = context.Response.Body;           MemoryStream responseBuffer = new MemoryStream();           context.Response.Body = responseBuffer;           await Next.Invoke(context);           responseBuffer.Seek(0,SeekOrigin.Begin);           var responseBody = new StreamReader(responseBuffer).ReadToEnd();           //do logging           var logResponse = new log_Response           {               headers = Json.Encode(context.Response.headers),Body = responseBody,log_Request = logRequest           };           db.log_Response.Add(logResponse);           responseBuffer.Seek(0,SeekOrigin.Begin);           await responseBuffer.copyToAsync(stream);           await db.SaveChangesAsync();        }    }}
总结

以上是内存溢出为你收集整理的c# – 请求/响应记录的响应主体全部内容,希望文章能够帮你解决c# – 请求/响应记录的响应主体所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1242565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存