@@ -25,6 +25,37 @@ internal readonly struct ConfigurationBuilder(IConfigurationSource source, IConf
25
25
26
26
public HasKeys WithKeys ( string key , string fallbackKey1 , string fallbackKey2 , string fallbackKey3 ) => new ( _source , _telemetry , key , fallbackKey1 , fallbackKey2 , fallbackKey3 ) ;
27
27
28
+ private static bool TryHandleResult < T > (
29
+ IConfigurationTelemetry telemetry ,
30
+ string key ,
31
+ ConfigurationResult < T > result ,
32
+ bool recordValue ,
33
+ Func < T > ? getDefaultValue ,
34
+ [ NotNullIfNotNull ( nameof ( getDefaultValue ) ) ] out T ? value )
35
+ where T : notnull
36
+ {
37
+ if ( result is { Result : { } ddResult , IsValid : true } )
38
+ {
39
+ value = ddResult ;
40
+ return true ;
41
+ }
42
+
43
+ // don't have a default value, so caller (which knows what the <T> is needs
44
+ // to return the default value. Necessary because we can't create a generic
45
+ // method that works "correctly" for both value types and reference types.
46
+ if ( getDefaultValue is null )
47
+ {
48
+ value = default ;
49
+ return false ;
50
+ }
51
+
52
+ var defaultValue = getDefaultValue ( ) ;
53
+ RecordTelemetry ( telemetry , key , recordValue , defaultValue ) ;
54
+
55
+ value = defaultValue ;
56
+ return true ;
57
+ }
58
+
28
59
private static bool TryHandleResult < T > (
29
60
IConfigurationTelemetry telemetry ,
30
61
string key ,
@@ -56,6 +87,31 @@ private static bool TryHandleResult<T>(
56
87
return true ;
57
88
}
58
89
90
+ private static void RecordTelemetry < T > ( IConfigurationTelemetry telemetry , string key , bool recordValue , T defaultValue )
91
+ {
92
+ switch ( defaultValue )
93
+ {
94
+ case int intVal :
95
+ telemetry . Record ( key , intVal , ConfigurationOrigins . Default ) ;
96
+ break ;
97
+ case double doubleVal :
98
+ telemetry . Record ( key , doubleVal , ConfigurationOrigins . Default ) ;
99
+ break ;
100
+ case bool boolVal :
101
+ telemetry . Record ( key , boolVal , ConfigurationOrigins . Default ) ;
102
+ break ;
103
+ case string stringVal :
104
+ telemetry . Record ( key , stringVal , recordValue , ConfigurationOrigins . Default ) ;
105
+ break ;
106
+ case null : // can't actually be called in practice
107
+ break ;
108
+ default :
109
+ // TODO: this shouldn't be calleable in practice, we need to revise it
110
+ telemetry . Record ( key , defaultValue . ToString ( ) , recordValue , ConfigurationOrigins . Default ) ;
111
+ break ;
112
+ }
113
+ }
114
+
59
115
private static void RecordTelemetry < T > ( IConfigurationTelemetry telemetry , string key , bool recordValue , DefaultResult < T > defaultValue )
60
116
{
61
117
switch ( defaultValue . Result )
0 commit comments