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

标签 c# .net azure

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/

相关文章:

c# - 从 C# 调用 c 函数

c# - XNA SpriteBatch.Draw 3D 向量 HLSL

c# - 将图像从 C# 保存到 SQL Server 数据库

c# - 如何在 Collection<T> 上使用 ToList

c# - WCF 和 Entities Framework 延迟加载

c# - ASP.NET核心标识: Multiple providers for the same account

c# - Powershell 脚本 SelectSingleNode 不工作

Azure SQL 同步组到本地错误

azure - 如何知道特定 Azure VM 类型是否支持高级磁盘?

azure - 如何检查 TDE 是否已启用并在 Azure Sql 数据库中运行