Alternative to HttpContext.Current.Items in ASP.NET Web Api

别说谁变了你拦得住时间么 提交于 2019-12-23 10:48:05

问题


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

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.

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.


回答1:


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.

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



来源:https://stackoverflow.com/questions/31825682/alternative-to-httpcontext-current-items-in-asp-net-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!