| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Net.Http; |
| | 9 | | using System.Net.Http.Headers; |
| | 10 | | using System.Runtime.CompilerServices; |
| | 11 | | using System.Runtime.ExceptionServices; |
| | 12 | | using System.Web; |
| | 13 | | using Azure.Core.Pipeline; |
| | 14 | |
|
| | 15 | | namespace Azure.Storage |
| | 16 | | { |
| | 17 | | internal static class LoggingExtensions |
| | 18 | | { |
| | 19 | | // The Azure.Core logging plan is still being settled, so we're adding |
| | 20 | | // [Condtional] extensions we can implement later to light up logging |
| | 21 | | // on par with the previous ILogger approach. |
| | 22 | |
|
| | 23 | | #pragma warning disable IDE0060 // Remove unused parameter |
| | 24 | | public static IDisposable BeginLoggingScope( |
| | 25 | | this HttpPipeline pipeline, |
| | 26 | | #pragma warning disable CA1801 // Review unused parameters |
| | 27 | | string className, |
| | 28 | | [CallerMemberName] string member = default) |
| | 29 | | #pragma warning restore CA1801 // Review unused parameters |
| | 30 | | // Methods that return values can't be marked [Conditional], but |
| | 31 | | // using statements will check for null before calling Dispose |
| 30190 | 32 | | => null; |
| | 33 | | #pragma warning restore IDE0060 // Remove unused parameter |
| | 34 | |
|
| | 35 | | [Conditional("EnableLoggingHelpers")] |
| | 36 | | public static void LogMethodEnter( |
| | 37 | | this HttpPipeline pipeline, |
| | 38 | | string className, |
| | 39 | | [CallerMemberName] string member = default, |
| | 40 | | string message = default) |
| | 41 | | => LogTrace(pipeline, $"ENTER METHOD {className} {member}\n{message}"); |
| | 42 | |
|
| | 43 | | [Conditional("EnableLoggingHelpers")] |
| | 44 | | public static void LogMethodExit( |
| | 45 | | this HttpPipeline pipeline, |
| | 46 | | string className, |
| | 47 | | [CallerMemberName] string member = default, |
| | 48 | | string message = "") |
| | 49 | | => LogTrace(pipeline, $"EXIT METHOD {className} {member}\n{message}"); |
| | 50 | |
|
| | 51 | | [Conditional("EnableLoggingHelpers")] |
| | 52 | | public static void LogException( |
| | 53 | | this HttpPipeline pipeline, |
| | 54 | | Exception ex, |
| | 55 | | string message = default) |
| | 56 | | { |
| | 57 | | // ExceptionDispatchInfo e => |
| | 58 | | // if (e != default) |
| | 59 | | // { |
| | 60 | | // logger.LogError(e.SourceException, message); |
| | 61 | | // e.Throw(); |
| | 62 | | // } |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | [Conditional("EnableLoggingHelpers")] |
| | 66 | | public static void LogTrace( |
| | 67 | | this HttpPipeline pipeline, |
| | 68 | | string message = default) |
| | 69 | | { |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | /* |
| | 73 | | Temporarily removing unused code that depends on HttpUtility.ParseQueryString |
| | 74 | |
|
| | 75 | |
|
| | 76 | | public static HttpRequestMessage Sanitize(this HttpRequestMessage httpRequest) |
| | 77 | | { |
| | 78 | | if (httpRequest == null) |
| | 79 | | { |
| | 80 | | return null; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | var sanitizedRequest = new HttpRequestMessage |
| | 84 | | { |
| | 85 | | Content = httpRequest.Content, |
| | 86 | | Method = httpRequest.Method, |
| | 87 | | Version = httpRequest.Version, |
| | 88 | | }; |
| | 89 | |
|
| | 90 | | foreach (var header in httpRequest.Headers) |
| | 91 | | { |
| | 92 | | // Redact Authorization header |
| | 93 | | if (header.Key.Equals(Constants.Authorization, StringComparison.InvariantCulture)) |
| | 94 | | { |
| | 95 | | sanitizedRequest.Headers.Authorization = AuthenticationHeaderValue.Parse(Constants.Redacted); |
| | 96 | | } |
| | 97 | | else if(header.Key.Equals(Constants.CopySource, StringComparison.InvariantCulture)) |
| | 98 | | { |
| | 99 | | sanitizedRequest.Headers.Add(Constants.CopySource, new Uri(header.Value.First()).Sanitize().ToString |
| | 100 | | } |
| | 101 | | else |
| | 102 | | { |
| | 103 | | sanitizedRequest.Headers.Add(header.Key, header.Value); |
| | 104 | | } |
| | 105 | | } |
| | 106 | |
|
| | 107 | | foreach (var property in httpRequest.Properties) |
| | 108 | | { |
| | 109 | | sanitizedRequest.Properties.Add(property); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | // Redact SAS Signature, if necessary |
| | 113 | | sanitizedRequest.RequestUri = httpRequest.RequestUri.Sanitize(); |
| | 114 | |
|
| | 115 | | return sanitizedRequest; |
| | 116 | | } |
| | 117 | |
|
| | 118 | | public static Uri Sanitize(this Uri uri) |
| | 119 | | { |
| | 120 | | if(uri?.Query == null) |
| | 121 | | { |
| | 122 | | return uri; |
| | 123 | | } |
| | 124 | | var parameters = HttpUtility.ParseQueryString(uri.Query); |
| | 125 | | if (parameters[Constants.Sas.Parameters.Signature] != null) |
| | 126 | | { |
| | 127 | | parameters[Constants.Sas.Parameters.Signature] = Constants.Redacted; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | var uriBuilder = new UriBuilder(uri) |
| | 131 | | { |
| | 132 | | Query = parameters.ToString() |
| | 133 | | }; |
| | 134 | | return uriBuilder.Uri; |
| | 135 | | } |
| | 136 | | */ |
| | 137 | | } |
| | 138 | | } |