1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- #nullable disable
5
-
6
4
using System . Globalization ;
7
5
using Microsoft . Build . Framework ;
8
6
using Microsoft . DotNet . Cli . Telemetry ;
@@ -15,7 +13,7 @@ public sealed class MSBuildLogger : INodeLogger
15
13
{
16
14
private readonly IFirstTimeUseNoticeSentinel _sentinel =
17
15
new FirstTimeUseNoticeSentinel ( ) ;
18
- private readonly ITelemetry _telemetry ;
16
+ private readonly ITelemetry ? _telemetry = null ;
19
17
20
18
internal const string TargetFrameworkTelemetryEventName = "targetframeworkeval" ;
21
19
internal const string BuildTelemetryEventName = "build" ;
@@ -54,7 +52,7 @@ public MSBuildLogger()
54
52
{
55
53
try
56
54
{
57
- string sessionId =
55
+ string ? sessionId =
58
56
Environment . GetEnvironmentVariable ( MSBuildForwardingApp . TelemetrySessionIdEnvironmentVariableName ) ;
59
57
60
58
if ( sessionId != null )
@@ -105,34 +103,13 @@ public void Initialize(IEventSource eventSource)
105
103
}
106
104
}
107
105
108
- internal static void FormatAndSend ( ITelemetry telemetry , TelemetryEventArgs args )
106
+ internal static void FormatAndSend ( ITelemetry ? telemetry , TelemetryEventArgs args )
109
107
{
110
108
switch ( args . EventName )
111
109
{
112
110
case TargetFrameworkTelemetryEventName :
113
- {
114
- var newEventName = $ "msbuild/{ TargetFrameworkTelemetryEventName } ";
115
- Dictionary < string , string > maskedProperties = [ ] ;
116
-
117
- foreach ( var key in new [ ] {
118
- TargetFrameworkVersionTelemetryPropertyKey ,
119
- RuntimeIdentifierTelemetryPropertyKey ,
120
- SelfContainedTelemetryPropertyKey ,
121
- UseApphostTelemetryPropertyKey ,
122
- OutputTypeTelemetryPropertyKey ,
123
- UseArtifactsOutputTelemetryPropertyKey ,
124
- ArtifactsPathLocationTypeTelemetryPropertyKey
125
- } )
126
- {
127
- if ( args . Properties . TryGetValue ( key , out string value ) )
128
- {
129
- maskedProperties . Add ( key , Sha256Hasher . HashWithNormalizedCasing ( value ) ) ;
130
- }
131
- }
132
-
133
- telemetry . TrackEvent ( newEventName , maskedProperties , measurements : null ) ;
134
- break ;
135
- }
111
+ TrackEvent ( telemetry , $ "msbuild/{ TargetFrameworkTelemetryEventName } ", args . Properties , [ ] , [ ] ) ;
112
+ break ;
136
113
case BuildTelemetryEventName :
137
114
TrackEvent ( telemetry , $ "msbuild/{ BuildTelemetryEventName } ", args . Properties ,
138
115
toBeHashed : [ "ProjectPath" , "BuildTarget" ] ,
@@ -174,33 +151,44 @@ internal static void FormatAndSend(ITelemetry telemetry, TelemetryEventArgs args
174
151
}
175
152
}
176
153
177
- private static void TrackEvent ( ITelemetry telemetry , string eventName , IDictionary < string , string > eventProperties , string [ ] toBeHashed , string [ ] toBeMeasured )
154
+ private static void TrackEvent ( ITelemetry ? telemetry , string eventName , IDictionary < string , string ? > eventProperties , string [ ] ? toBeHashed , string [ ] ? toBeMeasured )
178
155
{
179
- Dictionary < string , string > properties = null ;
180
- Dictionary < string , double > measurements = null ;
156
+ if ( telemetry == null || ! telemetry . Enabled )
157
+ {
158
+ return ;
159
+ }
160
+
161
+ Dictionary < string , string ? > ? properties = null ;
162
+ Dictionary < string , double > ? measurements = null ;
181
163
182
- foreach ( var propertyToBeHashed in toBeHashed )
164
+ if ( toBeHashed is not null )
183
165
{
184
- if ( eventProperties . TryGetValue ( propertyToBeHashed , out string value ) )
166
+ foreach ( var propertyToBeHashed in toBeHashed )
185
167
{
186
- // Lets lazy allocate in case there is tons of telemetry
187
- properties ??= new Dictionary < string , string > ( eventProperties ) ;
188
- properties [ propertyToBeHashed ] = Sha256Hasher . HashWithNormalizedCasing ( value ) ;
168
+ if ( eventProperties . TryGetValue ( propertyToBeHashed , out var value ) )
169
+ {
170
+ // Lets lazy allocate in case there is tons of telemetry
171
+ properties ??= new Dictionary < string , string ? > ( eventProperties ) ;
172
+ properties [ propertyToBeHashed ] = Sha256Hasher . HashWithNormalizedCasing ( value ! ) ;
173
+ }
189
174
}
190
175
}
191
176
192
- foreach ( var propertyToBeMeasured in toBeMeasured )
177
+ if ( toBeMeasured is not null )
193
178
{
194
- if ( eventProperties . TryGetValue ( propertyToBeMeasured , out string value ) )
179
+ foreach ( var propertyToBeMeasured in toBeMeasured )
195
180
{
196
- // Lets lazy allocate in case there is tons of telemetry
197
- properties ??= new Dictionary < string , string > ( eventProperties ) ;
198
- properties . Remove ( propertyToBeMeasured ) ;
199
- if ( double . TryParse ( value , CultureInfo . InvariantCulture , out double realValue ) )
181
+ if ( eventProperties . TryGetValue ( propertyToBeMeasured , out string ? value ) )
200
182
{
201
183
// Lets lazy allocate in case there is tons of telemetry
202
- measurements ??= [ ] ;
203
- measurements [ propertyToBeMeasured ] = realValue ;
184
+ properties ??= new Dictionary < string , string ? > ( eventProperties ) ;
185
+ properties . Remove ( propertyToBeMeasured ) ;
186
+ if ( double . TryParse ( value , CultureInfo . InvariantCulture , out double realValue ) )
187
+ {
188
+ // Lets lazy allocate in case there is tons of telemetry
189
+ measurements ??= [ ] ;
190
+ measurements [ propertyToBeMeasured ] = realValue ;
191
+ }
204
192
}
205
193
}
206
194
}
@@ -226,6 +214,5 @@ public void Shutdown()
226
214
}
227
215
228
216
public LoggerVerbosity Verbosity { get ; set ; }
229
-
230
- public string Parameters { get ; set ; }
217
+ public string ? Parameters { get ; set ; }
231
218
}
0 commit comments