Skip to content

Commit 85b48f7

Browse files
committed
Add LogResponse() and LogResponseError()
1 parent 3936bed commit 85b48f7

File tree

6 files changed

+58
-75
lines changed

6 files changed

+58
-75
lines changed

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient.Diagnostics/IOAuthLogger.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ public interface IOAuthLogger
5252
/// </summary>
5353
/// <param name="body">The request body.</param>
5454
void LogRequestBody(string body);
55+
56+
/// <summary>
57+
/// Log HTTP response.
58+
/// </summary>
59+
/// <param name="response">The HTTP response.</param>
60+
/// <param name="intuit_tid">The <c>intuit_tid</c> header value.</param>
61+
/// <param name="message">A log message.</param>
62+
/// <param name="body">The response body.</param>
63+
void LogResponse(HttpResponseMessage response, string intuit_tid = null, string message = null, string body = null);
64+
65+
/// <summary>
66+
/// Log HTTP response error.
67+
/// </summary>
68+
/// <param name="response">The HTTP response.</param>
69+
/// <param name="errorDetail">Details about the error.</param>
70+
void LogResponseError(HttpResponseMessage response, string errorDetail = null);
5571
}
5672

5773
/// <summary>
@@ -84,5 +100,13 @@ bool IOAuthLogger.ShouldLogRequestBody()
84100
void IOAuthLogger.LogRequestBody(string body)
85101
{
86102
}
103+
104+
void IOAuthLogger.LogResponse(HttpResponseMessage response, string intuit_tid, string message, string body)
105+
{
106+
}
107+
108+
void IOAuthLogger.LogResponseError(HttpResponseMessage response, string errorDetail)
109+
{
110+
}
87111
}
88112
}

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient.Diagnostics/OAuthAdvancedLogging.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public OAuthAdvancedLogging(bool enableSerilogRequestResponseLoggingForDebug, bo
238238

239239
}
240240

241+
/// <summary>
242+
/// Should response body be logged?
243+
/// </summary>
244+
public bool ShowInfoLogs { get; set; }
245+
241246
/// <summary>
242247
/// Logging message from SDK
243248
/// </summary>
@@ -267,5 +272,23 @@ void IOAuthLogger.LogRequestBody(string body)
267272
{
268273
logger.Write(LogEventLevel.Verbose, "Request Body: " + body);
269274
}
275+
276+
void IOAuthLogger.LogResponse(HttpResponseMessage response, string intuit_tid, string message, string body)
277+
{
278+
logger.Write(LogEventLevel.Information,
279+
"Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode +
280+
message == null ? "" : ", " + message);
281+
282+
if (body != null && !ShowInfoLogs && logger.IsEnabled(LogEventLevel.Debug))
283+
{
284+
logger.Write(LogEventLevel.Debug, "Response Body- " + body);
285+
}
286+
}
287+
288+
void IOAuthLogger.LogResponseError(HttpResponseMessage response, string errorDetail)
289+
{
290+
logger.Write(LogEventLevel.Warning, "Response: Status Code- " + response.StatusCode);
291+
logger.Write(LogEventLevel.Information, "Response: Error Details- " + response.ReasonPhrase + ": " + errorDetail);
292+
}
270293
}
271294
}

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/OAuth2Client.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ private OAuthAdvancedLogging InitializeAdvancedLogger()
182182
if (EnableAdvancedLoggerInfoMode == true)
183183
{
184184
ShowInfoLogs = true;
185+
logger.ShowInfoLogs = true;
185186
}
186187

187188
AdvancedLogger = logger;

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/TokenClient.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ public TimeSpan Timeout
185185
}
186186

187187
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);//errorDetail can be added here if required for BadRequest.
188-
if (OAuth2Client.AdvancedLoggerEnabled != false)
189-
{
190-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
191-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode);
192-
else
193-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Response Body- " + content);
194-
}
188+
Logger.LogResponse(response, intuit_tid, body: content);
195189
return new TokenResponse(content);
196190
}
197191
else
@@ -205,26 +199,12 @@ public TimeSpan Timeout
205199

206200
if (errorDetail != null && errorDetail != "")
207201
{
208-
if (OAuth2Client.AdvancedLoggerEnabled != false)
209-
{
210-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
211-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
212-
else
213-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
214-
215-
}
202+
Logger.LogResponseError(response, errorDetail);
216203
return new TokenResponse(response.StatusCode, response.ReasonPhrase + ": " + errorDetail);
217204
}
218205
else
219206
{
220-
if (OAuth2Client.AdvancedLoggerEnabled != false)
221-
{
222-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
223-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
224-
else
225-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase);
226-
227-
}
207+
Logger.LogResponseError(response);
228208
return new TokenResponse(response.StatusCode, response.ReasonPhrase);
229209
}
230210
}

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/TokenRevocationClient.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,7 @@ public virtual async Task<TokenRevocationResponse> RevokeAsync(
161161
intuit_tid = "None";
162162
}
163163

164-
if (OAuth2Client.AdvancedLoggerEnabled != false)
165-
{
166-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
167-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Token Revoked successfully");
168-
else
169-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Token Revoked successfully");
170-
}
164+
Logger.LogResponse(response, intuit_tid, message: "Token Revoked successfully");
171165
return new TokenRevocationResponse();
172166
}
173167
else if (response.StatusCode == HttpStatusCode.BadRequest)
@@ -184,13 +178,7 @@ public virtual async Task<TokenRevocationResponse> RevokeAsync(
184178
}
185179

186180
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
187-
if (OAuth2Client.AdvancedLoggerEnabled != false)
188-
{
189-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
190-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode);
191-
else
192-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Response Body- " + content);
193-
}
181+
Logger.LogResponse(response, intuit_tid, body: content);
194182
return new TokenRevocationResponse(content); //errorDetail can be added here if required.
195183
}
196184
else
@@ -205,24 +193,12 @@ public virtual async Task<TokenRevocationResponse> RevokeAsync(
205193

206194
if (errorDetail != null && errorDetail != "")
207195
{
208-
if (OAuth2Client.AdvancedLoggerEnabled != false)
209-
{
210-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
211-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
212-
else
213-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
214-
}
196+
Logger.LogResponse(response, errorDetail);
215197
return new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase + ": " + errorDetail);
216198
}
217199
else
218200
{
219-
if (OAuth2Client.AdvancedLoggerEnabled != false)
220-
{
221-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
222-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
223-
else
224-
OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
225-
}
201+
Logger.LogResponse(response);
226202
return new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase);
227203
}
228204
}

IPPDotNetDevKitCSV3/Code/Intuit.Ipp.OAuth2PlatformClient/Client/UserInfoClient.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,40 +134,19 @@ public TimeSpan Timeout
134134

135135
if (errorDetail != null && errorDetail != "")
136136
{
137-
if (OAuth2Client.AdvancedLoggerEnabled != false)
138-
{
139-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
140-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response: Status Code- " + response.StatusCode);
141-
else
142-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
143-
144-
}
137+
Logger.LogResponseError(response, errorDetail);
145138
return new UserInfoResponse(response.StatusCode, response.ReasonPhrase + ": " + errorDetail);
146139

147140
}
148141
else
149142
{
150-
if (OAuth2Client.AdvancedLoggerEnabled != false)
151-
{
152-
153-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
154-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response: Status Code- " + response.StatusCode);
155-
else
156-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase);
157-
158-
}
143+
Logger.LogResponseError(response);
159144
return new UserInfoResponse(response.StatusCode, response.ReasonPhrase);
160145
}
161146
}
162147

163148
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
164-
if (OAuth2Client.AdvancedLoggerEnabled != false)
165-
{
166-
if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
167-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode);
168-
else
169-
OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Response Body- " + content);
170-
}
149+
Logger.LogResponse(response, intuit_tid, body: content);
171150
return new UserInfoResponse(content);
172151
}
173152
catch (System.Exception ex)

0 commit comments

Comments
 (0)