简体   繁体   English

我如何在ASP.NET Web API中记录原始HTTP请求,而不管它是否路由到控制器?

[英]How do I log the raw HTTP request in ASP.NET Web API whether or not it routes to a controller?

Is it possible to log every HTTP request made to an ASP.NET Web API, even if the request is malformed, or for some other reason fails to route to one of the controllers. 是否可以记录对ASP.NET Web API发出的每个HTTP请求,即使该请求的格式有误,还是由于某些其他原因而无法路由到其中一个控制器。

For example, if a POST method has an Order model as it's parameter, an incorrect request will prevent it from ever reaching the controller's POST method. 例如,如果POST方法将Order模型作为参数,则错误的请求将阻止其到达控制器的POST方法。 I'd like to alert someone so that actions can be taken to prevent future failures. 我想提醒某人,以便可以采取措施防止将来的失败。

Is there a way to capture these requests further upstream from the controller? 有没有办法在控制器的上游捕获这些请求?

Either use Tracing, you need to implement ITraceWriter as shown below 使用跟踪,都需要实现ITraceWriter,如下所示

http://www.asp.net/web-api/overview/testing-and-debugging/tracing-in-aspnet-web-api http://www.asp.net/web-api/overview/testing-and-debugging/tracing-in-aspnet-web-api

Or implement message handlers 或实现消息处理程序

http://www.strathweb.com/2012/05/implementing-message-handlers-to-track-your-asp-net-web-api-usage/ http://www.strathweb.com/2012/05/implementing-message-handlers-to-track-your-asp-net-web-api-usage/

http://www.asp.net/web-api/overview/working-with-http/http-message-handlers http://www.asp.net/web-api/overview/working-with-http/http-message-handlers

Message handlers allow you to change message before it comes to HttpControllerDispatcher, and therefore you can handle common problems with routing and action method selection. 消息处理程序使您可以在HttpControllerDispatcher之前更改消息,因此您可以处理路由和操作方法选择方面的常见问题。

But as the last do not hesitate to use AppFabric logging, when you are hosting on IIS, because it can give you information when something is going wrong before requests come to your Web Application. 但是作为最后一个,当您在IIS上托管时,请不要犹豫使用AppFabric日志记录,因为它可以在出现错误之前向您提供信息,然后再向Web应用程序发送请求。 It handles scenarions with global errors in web application, for example errors with web.config. 它处理Web应用程序中具有全局错误的方案,例如web.config中的错误。

If you enable tracing in the ASP.NET Web API per Tracing in ASP.NET Web API , the built-in tracing infrastructure will log the information you are after. 如果您按ASP.NET Web API中的每个跟踪在ASP.NET Web API中启用跟踪,则内置跟踪基础结构将记录您所需要的信息。

In the case of a malformed request that fails content negotiation, you will see an HttpError occur in the DefaultContentNegotiator. 如果格式错误的请求使内容协商失败,您将在DefaultContentNegotiator中看到HttpError发生。

Here is an example of the simple trace for this type of error: 这是这种错误的简单跟踪示例:

DefaultContentNegotiator;Negotiate;Type='HttpError', formatters=[JsonMediaTypeFormatterTracer, XmlMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer, FormUrlEncodedMediaTypeFormatterTracer] DefaultContentNegotiator; Negotiate; Type ='HttpError',formatters = [JsonMediaTypeFormatterTracer,XmlMediaTypeFormatterTracer,FormUrlEncodedMediaTypeFormatterTracer,FormUrlEncodedMediaTypeFormatterTracer]

The trace writer is given a TraceRecord as its input, which will contain the request information as well as optionally any custom information you might want to use. 跟踪编写器将获得一个TraceRecord作为其输入,它将包含请求信息以及您可能想要使用的任何自定义信息(可选)。

The Web API will use the trace writer you configure to trace information throughout the lifecycle of requests. Web API将使用您配置的跟踪编写器在请求的整个生命周期中跟踪信息。 You can use trace writer to trace both the lifecycle events as well as your own controller code. 您可以使用跟踪编写器来跟踪生命周期事件以及您自己的控制器代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM