Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add http logging for c# #545

Merged
merged 10 commits into from
Nov 4, 2020
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ TwilioClient.SetEdge("sydney");

This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.

## Enable Debug Logging
There are two ways to enable debug logging in the default HTTP client. You can create an environment variable called `TWILIO_LOG_LEVEL` and set it to `debug` or you can set the LogLevel variable on the client as debug:

```csharp
TwilioClient.SetLogLevel("debug");
```

## Handling Exceptions

For an example on how to handle exceptions in this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/csharp-dotnet/usage-guide#handling-errors).
Expand Down Expand Up @@ -106,7 +113,7 @@ var response = new VoiceResponse()
.Play(new Uri("http://demo.twilio.com/hellomonkey/monkey.mp3"))
.Append(gather)
.Append(dial);

// Serialize the TwiML objects to XML string
Console.WriteLine(response);

Expand Down
43 changes: 43 additions & 0 deletions src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class TwilioRestClient : ITwilioRestClient
/// </summary>
public string Edge { get; set; }

/// <summary>
/// Log level for logging
/// </summary>
public string LogLevel { get; set; } = Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL");
private readonly string _username;
private readonly string _password;

Expand Down Expand Up @@ -82,6 +86,9 @@ public Response Request(Request request)
{
request.SetAuth(_username, _password);

if (LogLevel == "debug")
LogRequest(request);

if (Region != null)
request.Region = Region;

Expand All @@ -92,6 +99,11 @@ public Response Request(Request request)
try
{
response = HttpClient.MakeRequest(request);
if (LogLevel == "debug")
{
Console.WriteLine("response.status: " + response.StatusCode);
Console.WriteLine("response.headers: " + response.Headers);
}
}
catch (Exception clientException)
{
Expand Down Expand Up @@ -220,5 +232,36 @@ public static void ValidateSslCertificate(HttpClient client)
);
}
}

/// <summary>
/// Format request information when LogLevel is set to debug
/// </summary>
///
/// <param name="request">HTTP request</param>
private static void LogRequest(Request request)
{
Console.WriteLine("-- BEGIN Twilio API Request --");
Console.WriteLine("request.method: " + request.Method);
Console.WriteLine("request.URI: " + request.Uri);

if (request.QueryParams != null)
{
request.QueryParams.ForEach(parameter => Console.WriteLine(parameter.Key + ":" + parameter.Value));
}

if (request.HeaderParams != null)
{
for (int i = 0; i < request.HeaderParams.Count; i++)
{
var lowercaseHeader = request.HeaderParams[i].Key.ToLower();
if (lowercaseHeader.Contains("authorization") == false)
{
Console.WriteLine(request.HeaderParams[i].Key + ":" + request.HeaderParams[i].Value);
}
}
}

Console.WriteLine("-- END Twilio API Request --");
}
}
}
2 changes: 1 addition & 1 deletion src/Twilio/Http/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Request
/// </summary>
public HttpMethod Method { get; }

private Uri Uri;
public Uri Uri { get; private set; }

/// <summary>
/// Auth username
Expand Down
20 changes: 19 additions & 1 deletion src/Twilio/Twilio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class TwilioClient
private static string _region;
private static string _edge;
private static ITwilioRestClient _restClient;
private static string _logLevel;

private TwilioClient() { }

Expand Down Expand Up @@ -126,6 +127,20 @@ public static void SetEdge(string edge)
_edge = edge;
}

/// <summary>
/// Set the logging level
/// </summary>
/// <param name="loglevel">log level</param>
public static void SetLogLevel(string loglevel)
{
if (_restClient != null)
{
Invalidate();
}

_logLevel = loglevel;
}

/// <summary>
/// Get the rest client
/// </summary>
Expand All @@ -144,7 +159,10 @@ public static ITwilioRestClient GetRestClient()
);
}

_restClient = new TwilioRestClient(_username, _password, accountSid: _accountSid, region: _region, edge: _edge);
_restClient = new TwilioRestClient(_username, _password, accountSid: _accountSid, region: _region, edge: _edge)
{
LogLevel = _logLevel
};
return _restClient;
}

Expand Down
14 changes: 14 additions & 0 deletions test/Twilio.Test/Clients/TwilioRestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Twilio.Clients;
using Twilio.Exceptions;
using Twilio.Http;
using System.IO;

namespace Twilio.Tests.Clients
{
Expand Down Expand Up @@ -113,5 +114,18 @@ public void TestRedirectResponse()
TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client);
twilioClient.Request(request);
}

[Test]
public void TestActivatingDebugLogging()
{
var output = new StringWriter();
Console.SetOut(output);
client.MakeRequest(Arg.Any<Request>()).Returns(new Response(HttpStatusCode.OK, "OK"));
Request request = new Request(HttpMethod.Get, "https://www.contoso.com");
TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client);
twilioClient.LogLevel = "debug";
twilioClient.Request(request);
Assert.That(output.ToString(), Contains.Substring("request.URI: https://www.contoso.com/"));
}
}
}