ASP.NET Web Api中HttpContext.Current.Items的替代方法 [英] Alternative to HttpContext.Current.Items in ASP.NET Web Api

查看:318
本文介绍了ASP.NET Web Api中HttpContext.Current.Items的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有非System.Web/HttpContext替代HttpContext.Current.Items(按请求存储数据)?

is there a non- System.Web/HttpContext alternative to HttpContext.Current.Items, a per-request data storage?

我想保存请求的信息,保存它们并从记录器(NLog)中读取值.我发现执行此操作的一个好地方是DefaultHttpControllerActivator/IHttpControllerActivator.

I want to save information of a request, save them and read the values from my logger (NLog). I found a good place to do this is the DefaultHttpControllerActivator/IHttpControllerActivator.

这样,日志消息应该始终看起来相同,并且开发人员不会对所提供的格式或信息产生太大的影响.

This way the log messages should always look the same and the developer doesn't have much influence on the format or information provided.

编辑:经过更多研究后,我发现: 如何为异步.net方法创建callcontext? 我希望将逻辑调用上下文与不可变集合一起使用能够按预期工作.

Edit: After some more research i found this: How can we create a callcontext for async .net methods? I hope using the logical call context with immutable collections will work as expected.

推荐答案

HttpRequestMessage.Properties是用于存储任意按请求数据的IDictionary<string, Object>集合.在控制器动作中,可以很容易地将其作为基ApiController类上的Request.Properties属性使用.

HttpRequestMessage.Properties is an IDictionary<string, Object> collection for storing arbitrary per-request data. Within a controller action this is readily available as Request.Properties property on the base ApiController class.

不确定您打算如何使用它进行日志记录,但是您可能还想研究编写日志记录DelegatingHandler

Not sure how exactly you're planning on using this for logging, but you may also want to look into writing a Logging DelegatingHandler

这篇关于ASP.NET Web Api中HttpContext.Current.Items的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆