From b6806fc4615dde688f36768609acf4c0e8516027 Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Wed, 21 Oct 2020 13:37:21 -0600 Subject: [PATCH 1/8] feat: add http logging for c# --- README.md | 9 +++++- src/Twilio/Clients/TwilioRestClient.cs | 44 +++++++++++++++++++++++++- src/Twilio/Http/Request.cs | 2 +- src/Twilio/Twilio.cs | 17 +++++++++- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 92a082e27..16f1cdd3b 100644 --- a/README.md +++ b/README.md @@ -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). @@ -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); diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index 4cf230bf4..e34182b0c 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -40,6 +40,11 @@ public class TwilioRestClient : ITwilioRestClient /// public string Edge { get; set; } + /// + /// Log level for logging + /// + public string LogLevel { get; } + private readonly string _username; private readonly string _password; @@ -53,13 +58,16 @@ public class TwilioRestClient : ITwilioRestClient /// region to make requests for /// http client used to make the requests /// edge to make requests for + /// log level for http logging + public TwilioRestClient( string username, string password, string accountSid = null, string region = null, HttpClient httpClient = null, - string edge = null + string edge = null, + string logLevel = null ) { _username = username; @@ -70,6 +78,10 @@ public TwilioRestClient( Region = region; Edge = edge; + LogLevel = logLevel; + if (Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL") != null){ + LogLevel = Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL"); + } } /// @@ -82,6 +94,9 @@ public Response Request(Request request) { request.SetAuth(_username, _password); + if (LogLevel == "debug") + logRequest(request); + if (Region != null) request.Region = Region; @@ -92,6 +107,12 @@ 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 + "}"); + Console.WriteLine("response.data: " + response.Content); + } } catch (Exception clientException) { @@ -220,5 +241,26 @@ public static void ValidateSslCertificate(HttpClient client) ); } } + + /// + /// Format request information when LogLevel is set to debug + /// + /// + /// HTTP request + private static void logRequest(Request request){ + Console.WriteLine("-- BEGIN Twilio API Request --"); + Console.Write(request.Method + " "); + Console.WriteLine(request.Uri); + if (request.PostParams != null){ + request.PostParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); + } + if (request.QueryParams != null){ + request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); + } + if (request.HeaderParams != null){ + request.HeaderParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); + } + Console.WriteLine("-- END Twilio API Request --"); + } } } diff --git a/src/Twilio/Http/Request.cs b/src/Twilio/Http/Request.cs index a80a79a92..8cb52ec82 100644 --- a/src/Twilio/Http/Request.cs +++ b/src/Twilio/Http/Request.cs @@ -24,7 +24,7 @@ public class Request /// public HttpMethod Method { get; } - private Uri Uri; + public Uri Uri; /// /// Auth username diff --git a/src/Twilio/Twilio.cs b/src/Twilio/Twilio.cs index 7459f7187..1247bfb06 100644 --- a/src/Twilio/Twilio.cs +++ b/src/Twilio/Twilio.cs @@ -13,6 +13,7 @@ public class TwilioClient private static string _accountSid; private static string _region; private static string _edge; + private static string _logLevel; private static ITwilioRestClient _restClient; private TwilioClient() { } @@ -126,6 +127,20 @@ public static void SetEdge(string edge) _edge = edge; } + /// + /// Set the log levels + /// + /// Logging level + public static void SetLogLevel(string logLevel) + { + if (logLevel != _logLevel) + { + Invalidate(); + } + + _logLevel = logLevel; + } + /// /// Get the rest client /// @@ -144,7 +159,7 @@ 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; } From 91fdd48fc48b2b60128ed4c6166ce761a8ab99b4 Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Thu, 22 Oct 2020 16:13:45 -0600 Subject: [PATCH 2/8] remove auth headers and response body from logs --- src/Twilio/Clients/TwilioRestClient.cs | 33 ++++++++++++++++---------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index e34182b0c..bfa6c570e 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -111,7 +111,6 @@ public Response Request(Request request) { Console.WriteLine("response.status: " + response.StatusCode); Console.WriteLine("response.headers: " + "{"+ response.Headers + "}"); - Console.WriteLine("response.data: " + response.Content); } } catch (Exception clientException) @@ -248,19 +247,27 @@ public static void ValidateSslCertificate(HttpClient client) /// /// HTTP request private static void logRequest(Request request){ - Console.WriteLine("-- BEGIN Twilio API Request --"); - Console.Write(request.Method + " "); - Console.WriteLine(request.Uri); - if (request.PostParams != null){ - request.PostParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); - } - if (request.QueryParams != null){ - request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); - } - if (request.HeaderParams != null){ - request.HeaderParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); + Console.WriteLine("-- BEGIN Twilio API Request --"); + Console.Write(request.Method + " "); + Console.WriteLine(request.Uri); + + if (request.QueryParams != null) + { + request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); + } + + if (request.HeaderParams != null){ + for(int i=0; i Date: Wed, 28 Oct 2020 14:35:54 -0600 Subject: [PATCH 3/8] limit constructor parameters and fix up log format --- README.md | 2 +- src/Twilio/Clients/TwilioRestClient.cs | 30 ++++++++++++-------------- src/Twilio/Twilio.cs | 20 +++-------------- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 16f1cdd3b..990cac52f 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ This will result in the `hostname` transforming from `api.twilio.com` to `api.sy 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"); +TwilioClient.logLevel("debug"); ``` ## Handling Exceptions diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index bfa6c570e..3d7e5f0db 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -1,6 +1,7 @@  using System; using System.Net; +using System.Diagnostics; using Newtonsoft.Json; using Twilio.Exceptions; @@ -43,8 +44,7 @@ public class TwilioRestClient : ITwilioRestClient /// /// Log level for logging /// - public string LogLevel { get; } - + private string _logLevel; private readonly string _username; private readonly string _password; @@ -58,7 +58,6 @@ public class TwilioRestClient : ITwilioRestClient /// region to make requests for /// http client used to make the requests /// edge to make requests for - /// log level for http logging public TwilioRestClient( string username, @@ -66,8 +65,7 @@ public TwilioRestClient( string accountSid = null, string region = null, HttpClient httpClient = null, - string edge = null, - string logLevel = null + string edge = null ) { _username = username; @@ -78,10 +76,9 @@ public TwilioRestClient( Region = region; Edge = edge; - LogLevel = logLevel; - if (Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL") != null){ - LogLevel = Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL"); - } + + _logLevel = TwilioClient.logLevel ?? Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL"); + } /// @@ -94,8 +91,8 @@ public Response Request(Request request) { request.SetAuth(_username, _password); - if (LogLevel == "debug") - logRequest(request); + if (_logLevel == "debug") + LogRequest(request); if (Region != null) request.Region = Region; @@ -107,10 +104,10 @@ public Response Request(Request request) try { response = HttpClient.MakeRequest(request); - if(LogLevel == "debug") + if (_logLevel == "debug") { Console.WriteLine("response.status: " + response.StatusCode); - Console.WriteLine("response.headers: " + "{"+ response.Headers + "}"); + Console.WriteLine("response.headers: " + response.Headers); } } catch (Exception clientException) @@ -246,10 +243,11 @@ public static void ValidateSslCertificate(HttpClient client) /// /// /// HTTP request - private static void logRequest(Request request){ + private static void LogRequest(Request request){ Console.WriteLine("-- BEGIN Twilio API Request --"); - Console.Write(request.Method + " "); - Console.WriteLine(request.Uri); + Console.WriteLine("request.method: " + request.Method); + Console.WriteLine("rquest.URI: " + request.Uri); + if (request.QueryParams != null) { diff --git a/src/Twilio/Twilio.cs b/src/Twilio/Twilio.cs index 1247bfb06..37013cd4d 100644 --- a/src/Twilio/Twilio.cs +++ b/src/Twilio/Twilio.cs @@ -1,6 +1,6 @@ using Twilio.Clients; using Twilio.Exceptions; - +using System; namespace Twilio { /// @@ -13,8 +13,8 @@ public class TwilioClient private static string _accountSid; private static string _region; private static string _edge; - private static string _logLevel; private static ITwilioRestClient _restClient; + public static string logLevel { get; set; } private TwilioClient() { } @@ -127,20 +127,6 @@ public static void SetEdge(string edge) _edge = edge; } - /// - /// Set the log levels - /// - /// Logging level - public static void SetLogLevel(string logLevel) - { - if (logLevel != _logLevel) - { - Invalidate(); - } - - _logLevel = logLevel; - } - /// /// Get the rest client /// @@ -159,7 +145,7 @@ public static ITwilioRestClient GetRestClient() ); } - _restClient = new TwilioRestClient(_username, _password, accountSid: _accountSid, region: _region, edge: _edge, logLevel: _logLevel); + _restClient = new TwilioRestClient(_username, _password, accountSid: _accountSid, region: _region, edge: _edge); return _restClient; } From 73d0ab553f75e8c37be99a89a05b4ddb81c8048a Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Fri, 30 Oct 2020 16:56:06 -0600 Subject: [PATCH 4/8] fix formatting and use setter to set log level --- README.md | 2 +- src/Twilio/Clients/TwilioRestClient.cs | 30 +++++++++++--------------- src/Twilio/Twilio.cs | 23 +++++++++++++++++--- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 990cac52f..16f1cdd3b 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ This will result in the `hostname` transforming from `api.twilio.com` to `api.sy 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.logLevel("debug"); +TwilioClient.SetLogLevel("debug"); ``` ## Handling Exceptions diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index 3d7e5f0db..4cfa9e5d2 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -1,7 +1,6 @@  using System; using System.Net; -using System.Diagnostics; using Newtonsoft.Json; using Twilio.Exceptions; @@ -44,7 +43,7 @@ public class TwilioRestClient : ITwilioRestClient /// /// Log level for logging /// - private string _logLevel; + public string LogLevel { get; set; } = Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL"); private readonly string _username; private readonly string _password; @@ -58,7 +57,6 @@ public class TwilioRestClient : ITwilioRestClient /// region to make requests for /// http client used to make the requests /// edge to make requests for - public TwilioRestClient( string username, string password, @@ -76,9 +74,6 @@ public TwilioRestClient( Region = region; Edge = edge; - - _logLevel = TwilioClient.logLevel ?? Environment.GetEnvironmentVariable("TWILIO_LOG_LEVEL"); - } /// @@ -91,7 +86,7 @@ public Response Request(Request request) { request.SetAuth(_username, _password); - if (_logLevel == "debug") + if (LogLevel == "debug") LogRequest(request); if (Region != null) @@ -104,7 +99,7 @@ public Response Request(Request request) try { response = HttpClient.MakeRequest(request); - if (_logLevel == "debug") + if (LogLevel == "debug") { Console.WriteLine("response.status: " + response.StatusCode); Console.WriteLine("response.headers: " + response.Headers); @@ -243,24 +238,25 @@ public static void ValidateSslCertificate(HttpClient client) /// /// /// HTTP request - private static void LogRequest(Request request){ + private static void LogRequest(Request request) + { Console.WriteLine("-- BEGIN Twilio API Request --"); Console.WriteLine("request.method: " + request.Method); - Console.WriteLine("rquest.URI: " + request.Uri); - + Console.WriteLine("request.URI: " + request.Uri); if (request.QueryParams != null) { - request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key+ ":"+ kpv.Value)); - } + request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key + ":" + kpv.Value)); + } - if (request.HeaderParams != null){ - for(int i=0; i @@ -14,7 +14,7 @@ public class TwilioClient private static string _region; private static string _edge; private static ITwilioRestClient _restClient; - public static string logLevel { get; set; } + private static string _logLevel; private TwilioClient() { } @@ -127,6 +127,20 @@ public static void SetEdge(string edge) _edge = edge; } + /// + /// Set the logging level + /// + /// log level + public static void SetLogLevel(string loglevel) + { + if (_restClient != null) + { + Invalidate(); + } + + _logLevel = loglevel; + } + /// /// Get the rest client /// @@ -145,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; } From 59b00d1c719c5a645d87ab055bf6ed1f186195ab Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Mon, 2 Nov 2020 18:33:27 -0700 Subject: [PATCH 5/8] rename kpv to parameter for readability --- src/Twilio/Clients/TwilioRestClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index 4cfa9e5d2..1a92b5d56 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -246,7 +246,7 @@ private static void LogRequest(Request request) if (request.QueryParams != null) { - request.QueryParams.ForEach(kpv => Console.WriteLine(kpv.Key + ":" + kpv.Value)); + request.QueryParams.ForEach(parameter => Console.WriteLine(parameter.Key + ":" + parameter.Value)); } if (request.HeaderParams != null) From f5e305cf76ea314d08910817034331139768bb52 Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Tue, 3 Nov 2020 10:27:47 -0700 Subject: [PATCH 6/8] add test for activating debug logging --- test/Twilio.Test/Clients/TwilioRestClientTest.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Twilio.Test/Clients/TwilioRestClientTest.cs b/test/Twilio.Test/Clients/TwilioRestClientTest.cs index 399f94ef2..d2a3e4204 100644 --- a/test/Twilio.Test/Clients/TwilioRestClientTest.cs +++ b/test/Twilio.Test/Clients/TwilioRestClientTest.cs @@ -113,5 +113,15 @@ public void TestRedirectResponse() TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client); twilioClient.Request(request); } + + [Test] + public void TestActivatingDebugLogging() + { + client.MakeRequest(Arg.Any()).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); + } } } From 41dcd25dbfca0f4398d2af079e0496b7992944e2 Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Tue, 3 Nov 2020 17:21:21 -0700 Subject: [PATCH 7/8] fix logging test and uri --- src/Twilio/Http/Request.cs | 2 +- test/Twilio.Test/Clients/TwilioRestClientTest.cs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Twilio/Http/Request.cs b/src/Twilio/Http/Request.cs index 8cb52ec82..148572885 100644 --- a/src/Twilio/Http/Request.cs +++ b/src/Twilio/Http/Request.cs @@ -24,7 +24,7 @@ public class Request /// public HttpMethod Method { get; } - public Uri Uri; + public Uri Uri { get; private set; } /// /// Auth username diff --git a/test/Twilio.Test/Clients/TwilioRestClientTest.cs b/test/Twilio.Test/Clients/TwilioRestClientTest.cs index d2a3e4204..ed3df0d8d 100644 --- a/test/Twilio.Test/Clients/TwilioRestClientTest.cs +++ b/test/Twilio.Test/Clients/TwilioRestClientTest.cs @@ -7,6 +7,8 @@ using Twilio.Clients; using Twilio.Exceptions; using Twilio.Http; +using Twilio.Rest.Api.V2010.Account; +using System.IO; namespace Twilio.Tests.Clients { @@ -117,11 +119,14 @@ public void TestRedirectResponse() [Test] public void TestActivatingDebugLogging() { + var output = new StringWriter(); + Console.SetOut(output); client.MakeRequest(Arg.Any()).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/")); } } } From f1bf6e36a92cebe85228820dbe22fe398079fc64 Mon Sep 17 00:00:00 2001 From: Jennifer Mah Date: Tue, 3 Nov 2020 17:28:23 -0700 Subject: [PATCH 8/8] remove typo --- test/Twilio.Test/Clients/TwilioRestClientTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Twilio.Test/Clients/TwilioRestClientTest.cs b/test/Twilio.Test/Clients/TwilioRestClientTest.cs index ed3df0d8d..c60852cf3 100644 --- a/test/Twilio.Test/Clients/TwilioRestClientTest.cs +++ b/test/Twilio.Test/Clients/TwilioRestClientTest.cs @@ -7,7 +7,6 @@ using Twilio.Clients; using Twilio.Exceptions; using Twilio.Http; -using Twilio.Rest.Api.V2010.Account; using System.IO; namespace Twilio.Tests.Clients