Iis 7 Web API 2响应不总是发送?
我在消息处理程序中实现了所有请求/响应数据包的日志记录,如下所示:Iis 7 Web API 2响应不总是发送?,iis-7,asp.net-web-api2,message-handlers,Iis 7,Asp.net Web Api2,Message Handlers,我在消息处理程序中实现了所有请求/响应数据包的日志记录,如下所示: protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var correlationId = Guid.NewGuid(); RequestApiLogger.LogHttpRequest(reque
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var correlationId = Guid.NewGuid();
RequestApiLogger.LogHttpRequest(request, correlationId);
return await base.SendAsync(request, cancellationToken).ContinueWith(
task =>
{
var response = task.Result;
response.Headers.Add("http-tracking-id", correlationId.ToString("D"));
ResponseApiLogger.LogHttpResponse(response, correlationId);
return response;
}, cancellationToken);
}
下面是如何在启动时注册处理程序和记录器
GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
GlobalConfiguration.Configuration.MessageHandlers.Add(new MessageHandler());
我也很难测试这一点,任何建议都非常感谢。所有请求都被记录,但有些响应不是,对吗?在请求日志记录块之后尝试异常处理,可能是SendAsync方法在记录响应之前引发异常如果您在OWIN下运行,则可以创建中间件来独立于web api记录请求和响应。如果web api应用程序内部发生异常,日志记录仍会发生。斯科特·艾伦(Scott Allen)有一篇关于如何做到这一点的好文章:
正确。所有请求都已记录,但有些响应未记录。我试图找出为什么响应永远不会被记录下来。使用跟踪日志记录,我可以在API中看到我一直到Request.CreateResponse,没有任何问题。我更新了帖子,加入了全局记录器及其注册方式。在发出请求但未发生响应的时间内,不会记录任何异常。
GlobalConfiguration.Configuration.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
GlobalConfiguration.Configuration.MessageHandlers.Add(new MessageHandler());