|  | 
| 1 |  | -namespace CallbackHandler.Bootstrapper | 
|  | 1 | +using Microsoft.Extensions.Logging; | 
|  | 2 | + | 
|  | 3 | +namespace CallbackHandler.Bootstrapper | 
| 2 | 4 | { | 
| 3 | 5 |     using System; | 
|  | 6 | +    using System.Collections.Generic; | 
| 4 | 7 |     using System.Diagnostics.CodeAnalysis; | 
| 5 | 8 |     using System.IO; | 
| 6 | 9 |     using System.Reflection; | 
|  | 
| 14 | 17 |     using Newtonsoft.Json; | 
| 15 | 18 |     using Newtonsoft.Json.Serialization; | 
| 16 | 19 |     using Shared.EventStore.Extensions; | 
|  | 20 | +    using Shared.General; | 
|  | 21 | +    using Shared.Middleware; | 
| 17 | 22 |     using Swashbuckle.AspNetCore.Filters; | 
| 18 | 23 | 
 | 
| 19 | 24 |     [ExcludeFromCodeCoverage] | 
| @@ -71,6 +76,37 @@ public MiddlewareRegistry() | 
| 71 | 76 | 
 | 
| 72 | 77 |             Assembly assembly = this.GetType().GetTypeInfo().Assembly; | 
| 73 | 78 |             this.AddMvcCore().AddApplicationPart(assembly).AddControllersAsServices(); | 
|  | 79 | + | 
|  | 80 | +            bool logRequests = ConfigurationReaderExtensions.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogRequests", true); | 
|  | 81 | +            bool logResponses = ConfigurationReaderExtensions.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogResponses", true); | 
|  | 82 | +            LogLevel middlewareLogLevel = ConfigurationReaderExtensions.GetValueOrDefault<LogLevel>("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning); | 
|  | 83 | + | 
|  | 84 | +            RequestResponseMiddlewareLoggingConfig config = | 
|  | 85 | +                new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses); | 
|  | 86 | + | 
|  | 87 | +            this.AddSingleton(config); | 
|  | 88 | +        } | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    public static class ConfigurationReaderExtensions | 
|  | 92 | +    { | 
|  | 93 | +        public static T GetValueOrDefault<T>(String sectionName, String keyName, T defaultValue) | 
|  | 94 | +        { | 
|  | 95 | +            try | 
|  | 96 | +            { | 
|  | 97 | +                var value = ConfigurationReader.GetValue(sectionName, keyName); | 
|  | 98 | + | 
|  | 99 | +                if (String.IsNullOrEmpty(value)) | 
|  | 100 | +                { | 
|  | 101 | +                    return defaultValue; | 
|  | 102 | +                } | 
|  | 103 | + | 
|  | 104 | +                return (T)Convert.ChangeType(value, typeof(T)); | 
|  | 105 | +            } | 
|  | 106 | +            catch (KeyNotFoundException kex) | 
|  | 107 | +            { | 
|  | 108 | +                return defaultValue; | 
|  | 109 | +            } | 
| 74 | 110 |         } | 
| 75 | 111 |     } | 
| 76 | 112 | } | 
0 commit comments