Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Rest ServiceClientTracing - how to output tracing results to Console or Debug?

Tags:

c#

.net

azure

ServiceClientTracing class has static boolean IsEnabled property that allows you to turn tracing on. But when you set property to true no output is showing in Console/Debug or anywhere else.

How to set tracing so that output is visible in either Console/Debug Window (or anywhere really)?

like image 404
nikib3ro Avatar asked Oct 25 '17 23:10

nikib3ro


1 Answers

Seems that this is not supported by default in Microsoft.Rest.ClientRuntime project, so you need to role your own class. For example:

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.

Now if you want to turn Tracing on all you need to do is:

ServiceClientTracing.IsEnabled = true;
ServiceClientTracing.AddTracingInterceptor(new DebugTracer());
like image 152
nikib3ro Avatar answered Oct 17 '22 06:10

nikib3ro