@@ -33,30 +33,50 @@ public static IServiceCollection AddHttpOverrides(this IServiceCollection servic
33
33
string httpLoggingSectionName = DefaultConfiguration . HttpLoggingKey ,
34
34
string healthCheckSectionName = DefaultConfiguration . HealthCheckKey )
35
35
{
36
- ILogger ? logger = null ;
36
+ ILogger ? logger1 = null ;
37
37
38
- void EnsureLogger ( )
38
+ ILogger EnsureLogger ( )
39
39
{
40
- logger ??= _loggerLazy . Value ;
41
- logger ??= NullLogger . Instance ;
40
+ logger1 ??= _loggerLazy . Value ! ;
41
+ return logger1 ??= NullLogger . Instance ;
42
42
}
43
43
44
44
var healthCheckConfigurationSection = string . IsNullOrEmpty ( healthCheckSectionName ) ? configuration : configuration . GetSection ( healthCheckSectionName ) ;
45
45
var healthCheckAppOptions = _healthCheckAppOptions = healthCheckConfigurationSection . Get < HealthCheckAppOptions > ( ) ?? new HealthCheckAppOptions ( ) ;
46
46
if ( healthCheckAppOptions . IsEnabled )
47
47
{
48
- EnsureLogger ( ) ;
48
+ var logger = EnsureLogger ( ) ;
49
+ var logLevel = healthCheckAppOptions . LogLevel ;
50
+
49
51
if ( healthCheckConfigurationSection . GetChildren ( ) . Any ( ) )
50
52
{
51
- logger ? . LogDebug ( "Attempt to load HealthCheckOptions from configuration" ) ;
53
+ logger . Log ( logLevel , "Attempt to load HealthCheckOptions from configuration" ) ;
52
54
services . Configure < HealthCheckOptions > ( healthCheckConfigurationSection ) ;
53
55
}
54
56
else
55
57
{
56
- logger ? . LogDebug ( "Add HealthChecks" ) ;
58
+ logger . Log ( logLevel , "Add HealthChecks" ) ;
57
59
}
58
60
59
- services . Configure < HealthCheckAppOptions > ( healthCheckConfigurationSection ) ;
61
+ if ( healthCheckAppOptions . RemoveResponseWriter )
62
+ {
63
+ logger . Log ( logLevel , "HealthCheckOptions remove ResponseWriter" ) ;
64
+
65
+ services . Configure < HealthCheckOptions > ( string . Empty , ( HealthCheckOptions opt ) =>
66
+ {
67
+ opt . ResponseWriter = null ! ;
68
+ } ) ;
69
+ }
70
+ else if ( ! string . IsNullOrEmpty ( healthCheckAppOptions . Prefix ) || ! string . IsNullOrEmpty ( healthCheckAppOptions . Suffix ) )
71
+ {
72
+ logger . Log ( logLevel , "HealthCheckOptions customize Prefix:{prefix} and Suffix:{suffix}" , healthCheckAppOptions . Prefix , healthCheckAppOptions . Suffix ) ;
73
+
74
+ var writer = HealthCheckResponseWriters . Instance = new HealthCheckResponseWriters ( healthCheckAppOptions . Prefix , healthCheckAppOptions . Suffix ) ;
75
+ services . Configure < HealthCheckOptions > ( string . Empty , ( HealthCheckOptions opt ) =>
76
+ {
77
+ opt . ResponseWriter = writer . WriteMinimalPlaintext ;
78
+ } ) ;
79
+ }
60
80
61
81
services . AddHealthChecks ( ) ; // Registers health checks services
62
82
}
@@ -65,8 +85,8 @@ void EnsureLogger()
65
85
66
86
if ( ! _isForwardedHeadersEnabled )
67
87
{
68
- EnsureLogger ( ) ;
69
- logger ? . LogDebug ( "Attempt to load ForwardedHeadersOptions from configuration" ) ;
88
+ var logger = EnsureLogger ( ) ;
89
+ logger . LogDebug ( "Attempt to load ForwardedHeadersOptions from configuration" ) ;
70
90
71
91
var httpOverridesConfigurationSection = string . IsNullOrEmpty ( httpOverridesSectionName ) ? configuration : configuration . GetSection ( httpOverridesSectionName ) ;
72
92
services . Configure < ForwardedHeadersOptions > ( httpOverridesConfigurationSection ) ;
@@ -89,8 +109,8 @@ void EnsureLogger()
89
109
90
110
if ( _isHttpLoggingEnabled )
91
111
{
92
- EnsureLogger ( ) ;
93
- logger ? . LogDebug ( "Attempt to load HttpLoggingOptions from configuration" ) ;
112
+ var logger = EnsureLogger ( ) ;
113
+ logger . LogDebug ( "Attempt to load HttpLoggingOptions from configuration" ) ;
94
114
95
115
var httpLoggingConfigurationSection = string . IsNullOrEmpty ( httpLoggingSectionName ) ? configuration : configuration . GetSection ( httpLoggingSectionName ) ;
96
116
services . Configure < HttpLoggingOptions > ( httpLoggingConfigurationSection ) ;
@@ -133,6 +153,7 @@ public static WebApplication UseHttpOverrides(this WebApplication app, ILogger?
133
153
134
154
if ( _healthCheckAppOptions . IsEnabled )
135
155
{
156
+ var logLevel = _healthCheckAppOptions . LogLevel ;
136
157
if ( _healthCheckAppOptions . IsAzureAppServiceContainer )
137
158
{
138
159
var mainHealthChecksPath = string . IsNullOrEmpty ( _healthCheckAppOptions . Path ) ? DefaultConfiguration . HealthChecksPath : _healthCheckAppOptions . Path ;
@@ -144,7 +165,7 @@ public static WebApplication UseHttpOverrides(this WebApplication app, ILogger?
144
165
if ( _healthCheckAppOptions . Paths is { } pathArrays && pathArrays . Length > 0 )
145
166
{
146
167
var paths = pathArrays . Select ( p => ( PathString ) p ) . ToArray ( ) ;
147
- logger . LogDebug ( "Use HealthChecks Port:{port} {paths}" , port , paths ) ;
168
+ logger . Log ( logLevel , "Use HealthChecks Port:{port} Paths: {paths}" , port , paths ) ;
148
169
149
170
bool predicate ( HttpContext c )
150
171
{
@@ -167,8 +188,8 @@ bool predicate(HttpContext c)
167
188
}
168
189
else
169
190
{
170
- var healthChecksPath = StringHelper . NormalizeNull ( _healthCheckAppOptions . Path ) ;
171
- logger . LogDebug ( "Use HealthChecks Port:{port} {path}" , port , healthChecksPath ) ;
191
+ var healthChecksPath = _healthCheckAppOptions . Path ;
192
+ logger . LogDebug ( "Use HealthChecks Port:{port} Path: {path}" , port , healthChecksPath ) ;
172
193
if ( port . HasValue )
173
194
{
174
195
app . UseHealthChecks ( healthChecksPath , port . Value ) ;
0 commit comments