gpt4 book ai didi

logging - 如何启用 Web API 响应跟踪?

转载 作者:行者123 更新时间:2023-12-04 21:52:24 24 4
gpt4 key购买 nike

我有一个基于 WebAPI 4.0 的服务,但是我们遇到了一些客户端接收 JSON 而不是 XML 的问题,我如何才能在实时服务上查看 WebAPI 内部发生了什么?

最佳答案

确保编译和部署的代码库具有以下设置:

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// etc.

config.EnableSystemDiagnosticsTracing();

// etc.
}
}

然后将此添加到您的 Web.config文件:
<configuration>
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="E:\CompressedLogs\Service1-WebApi-TraceOutput.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
</configuration>

您应该在日志文件中看到这些内容:
w3wp.exe Information: 0 : Request, Method=GET, Url=http://MonkeyChops.potato.org/v1/PantsSpiderman/431?api_key=0, Message='http://MonkeyChops.potato.org/v1/PantsSpiderman/431?api_key=0'
w3wp.exe Information: 0 : Message='PantsSpiderman', Operation=DefaultHttpControllerSelector.SelectController
w3wp.exe Information: 0 : Message='Spandex.MonkeyChops.WebApi.Controllers.PantsSpidermanController', Operation=DefaultHttpControllerActivator.Create
w3wp.exe Information: 0 : Message='Spandex.MonkeyChops.WebApi.Controllers.PantsSpidermanController', Operation=HttpControllerDescriptor.CreateController
w3wp.exe Information: 0 : Message='Selected action 'Get(String id)'', Operation=ApiControllerActionSelector.SelectAction
w3wp.exe Information: 0 : Message='Parameter 'id' bound to the value '431'', Operation=ModelBinderParameterBinding.ExecuteBindingAsync
w3wp.exe Information: 0 : Message='Model state is valid. Values: id=431', Operation=HttpActionBinding.ExecuteBindingAsync
w3wp.exe Information: 0 : Message='Will use same 'XmlMediaTypeFormatter' formatter', Operation=XmlMediaTypeFormatter.GetPerRequestFormatterInstance
w3wp.exe Information: 0 : Message='Selected formatter='XmlMediaTypeFormatter', content-type='text/xml; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
w3wp.exe Information: 0 : Message='Action returned 'StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[Spandex.MonkeyChops.Models.Pants.result, Spandex.MonkeyChops.WebApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
{
Cache-Control: public, must-revalidate, max-age=86400
Content-Type: text/xml; charset=utf-8
}'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync
w3wp.exe Information: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Status=200 (OK)
w3wp.exe Information: 0 : Operation=PantsSpidermanController.ExecuteAsync, Status=200 (OK)
w3wp.exe Information: 0 : Response, Status=200 (OK), Method=GET, Url=http://MonkeyChops.potato.org/v1/PantsSpiderman/431?api_key=0, Message='Content-type='text/xml; charset=utf-8', content-length=unknown'
w3wp.exe Information: 0 : Operation=XmlMediaTypeFormatter.WriteToStreamAsync
w3wp.exe Information: 0 : Operation=PantsSpidermanController.Dispose

关于logging - 如何启用 Web API 响应跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com