gpt4 book ai didi

c# - Microsoft Rest ServiceClientTracing - 如何将跟踪结果输出到控制台或调试?

转载 作者:太空狗 更新时间:2023-10-29 22:21:20 27 4
gpt4 key购买 nike

ServiceClientTracing 类具有static boolean IsEnabled 属性,允许您打开跟踪。但是,当您将属性设置为 true 时,控制台/调试或其他任何地方都不会显示任何输出。

如何设置跟踪以使输出在控制台/调试窗口(或任何地方)中可见?

最佳答案

似乎在 Microsoft.Rest.ClientRuntime 项目中默认不支持此功能,因此您需要指定自己的类。例如:

class DebugTracer : IServiceClientTracingInterceptor
{
public void Information(string message)
{
Debug.WriteLine(message);
}

public void TraceError(string invocationId, Exception exception)
{
Debug.WriteLine("Exception in {0}: {1}", invocationId, exception);
}

public void ReceiveResponse(string invocationId, HttpResponseMessage response)
{
string requestAsString = (response == null ? string.Empty : response.AsFormattedString());
Debug.WriteLine("invocationId: {0}\r\nresponse: {1}", invocationId, requestAsString);
}

public void SendRequest(string invocationId, HttpRequestMessage request)
{
string requestAsString = (request == null ? string.Empty : request.AsFormattedString());
Debug.WriteLine("invocationId: {0}\r\nrequest: {1}", invocationId, requestAsString);
}

public void Configuration(string source, string name, string value)
{
Debug.WriteLine("Configuration: source={0}, name={1}, value={2}", source, name, value);
}

public void EnterMethod(string invocationId, object instance, string method, IDictionary<string, object> parameters)
{
Debug.WriteLine("invocationId: {0}\r\ninstance: {1}\r\nmethod: {2}\r\nparameters: {3}",
invocationId, instance, method, parameters.AsFormattedString());
}

public void ExitMethod(string invocationId, object returnValue)
{
string returnValueAsString = (returnValue == null ? string.Empty : returnValue.ToString());
Debug.WriteLine("Exit with invocation id {0}, the return value is {1}",
invocationId, returnValueAsString);
}
}

Appropriated from Log4NetTracingInterceptor

现在,如果您想打开跟踪,您需要做的就是:

ServiceClientTracing.IsEnabled = true;
ServiceClientTracing.AddTracingInterceptor(new DebugTracer());

关于c# - Microsoft Rest ServiceClientTracing - 如何将跟踪结果输出到控制台或调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46943669/

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