这是我设法得到多远.
我一直在阅读回复.说:
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# – 请求/响应记录的响应主体所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)