1- namespace EstateReportingAPI . Bootstrapper {
1+ using Shared . Middleware ;
2+
3+ namespace EstateReportingAPI . Bootstrapper {
24 using System . Diagnostics . CodeAnalysis ;
35 using System . Net . Security ;
46 using System . Reflection ;
@@ -88,6 +90,15 @@ public MiddlewareRegistry(){
8890
8991 Assembly assembly = this . GetType ( ) . GetTypeInfo ( ) . Assembly ;
9092 this . AddMvcCore ( ) . AddApplicationPart ( assembly ) . AddControllersAsServices ( ) ;
93+
94+ bool logRequests = ConfigurationReaderExtensions . GetValueOrDefault < Boolean > ( "MiddlewareLogging" , "LogRequests" , true ) ;
95+ bool logResponses = ConfigurationReaderExtensions . GetValueOrDefault < Boolean > ( "MiddlewareLogging" , "LogResponses" , true ) ;
96+ LogLevel middlewareLogLevel = ConfigurationReaderExtensions . GetValueOrDefault < LogLevel > ( "MiddlewareLogging" , "MiddlewareLogLevel" , LogLevel . Warning ) ;
97+
98+ RequestResponseMiddlewareLoggingConfig config =
99+ new RequestResponseMiddlewareLoggingConfig ( middlewareLogLevel , logRequests , logResponses ) ;
100+
101+ this . AddSingleton ( config ) ;
91102 }
92103
93104 private HttpClientHandler ApiEndpointHttpHandler ( IServiceProvider serviceProvider ) {
@@ -101,4 +112,26 @@ private HttpClientHandler ApiEndpointHttpHandler(IServiceProvider serviceProvide
101112 } ;
102113 }
103114 }
115+
116+ public static class ConfigurationReaderExtensions
117+ {
118+ public static T GetValueOrDefault < T > ( String sectionName , String keyName , T defaultValue )
119+ {
120+ try
121+ {
122+ var value = ConfigurationReader . GetValue ( sectionName , keyName ) ;
123+
124+ if ( String . IsNullOrEmpty ( value ) )
125+ {
126+ return defaultValue ;
127+ }
128+
129+ return ( T ) Convert . ChangeType ( value , typeof ( T ) ) ;
130+ }
131+ catch ( KeyNotFoundException kex )
132+ {
133+ return defaultValue ;
134+ }
135+ }
136+ }
104137}
0 commit comments