1+ using System . Runtime . InteropServices ;
12using System . Text . Json ;
23
34using Microsoft . ApplicationInsights ;
@@ -13,45 +14,15 @@ public class AzTrace
1314 /// </summary>
1415 internal static string InstallationId { get ; private set ; }
1516
17+ /// <summary>
18+ /// OS platform the application is running on.
19+ /// </summary>
20+ internal static string Platform { get ; private set ; }
21+
1622 internal static void Initialize ( )
1723 {
18- InstallationId = null ;
19-
20- string azCLIProfilePath , azPSHProfilePath ;
21- string azureConfigDir = Environment . GetEnvironmentVariable ( "AZURE_CONFIG_DIR" ) ;
22- string userProfile = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
23-
24- if ( string . IsNullOrEmpty ( azureConfigDir ) )
25- {
26- azCLIProfilePath = Path . Combine ( userProfile , ".azure" , "azureProfile.json" ) ;
27- azPSHProfilePath = Path . Combine ( userProfile , ".Azure" , "AzureRmContextSettings.json" ) ;
28- }
29- else
30- {
31- azCLIProfilePath = Path . Combine ( azureConfigDir , "azureProfile.json" ) ;
32- azPSHProfilePath = Path . Combine ( azureConfigDir , "AzureRmContextSettings.json" ) ;
33- }
34-
35- try
36- {
37- if ( File . Exists ( azCLIProfilePath ) )
38- {
39- using var stream = File . OpenRead ( azCLIProfilePath ) ;
40- var jsonElement = JsonSerializer . Deserialize < JsonElement > ( stream ) ;
41- InstallationId = jsonElement . GetProperty ( "installationId" ) . GetString ( ) ;
42- }
43- else if ( File . Exists ( azPSHProfilePath ) )
44- {
45- using var stream = File . OpenRead ( azPSHProfilePath ) ;
46- var jsonElement = JsonSerializer . Deserialize < JsonElement > ( stream ) ;
47- InstallationId = jsonElement . GetProperty ( "Settings" ) . GetProperty ( nameof ( InstallationId ) ) . GetString ( ) ;
48- }
49- }
50- catch
51- {
52- // Something wrong when reading the config file.
53- InstallationId = null ;
54- }
24+ InstallationId = GetInstallationId ( ) ;
25+ Platform = GetOSPlatform ( ) ;
5526 }
5627
5728 /// <summary>
@@ -157,6 +128,67 @@ internal static AzTrace Exception(object details)
157128 // Don't create an object when telemetry is disabled.
158129 return null ;
159130 }
131+
132+ private static string GetInstallationId ( )
133+ {
134+ string azCLIProfilePath , azPSHProfilePath ;
135+ string azureConfigDir = Environment . GetEnvironmentVariable ( "AZURE_CONFIG_DIR" ) ;
136+ string userProfile = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) ;
137+
138+ if ( string . IsNullOrEmpty ( azureConfigDir ) )
139+ {
140+ azCLIProfilePath = Path . Combine ( userProfile , ".azure" , "azureProfile.json" ) ;
141+ azPSHProfilePath = Path . Combine ( userProfile , ".Azure" , "AzureRmContextSettings.json" ) ;
142+ }
143+ else
144+ {
145+ azCLIProfilePath = Path . Combine ( azureConfigDir , "azureProfile.json" ) ;
146+ azPSHProfilePath = Path . Combine ( azureConfigDir , "AzureRmContextSettings.json" ) ;
147+ }
148+
149+ try
150+ {
151+ if ( File . Exists ( azCLIProfilePath ) )
152+ {
153+ using var stream = File . OpenRead ( azCLIProfilePath ) ;
154+ var jsonElement = JsonSerializer . Deserialize < JsonElement > ( stream ) ;
155+ return jsonElement . GetProperty ( "installationId" ) . GetString ( ) ;
156+ }
157+ else if ( File . Exists ( azPSHProfilePath ) )
158+ {
159+ using var stream = File . OpenRead ( azPSHProfilePath ) ;
160+ var jsonElement = JsonSerializer . Deserialize < JsonElement > ( stream ) ;
161+ return jsonElement . GetProperty ( "Settings" ) . GetProperty ( nameof ( InstallationId ) ) . GetString ( ) ;
162+ }
163+ }
164+ catch
165+ {
166+ // Something wrong when reading the config file.
167+ return null ;
168+ }
169+
170+ return null ;
171+ }
172+
173+ private static string GetOSPlatform ( )
174+ {
175+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
176+ {
177+ return "Windows" ;
178+ }
179+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
180+ {
181+ return "Linux" ;
182+ }
183+ else if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
184+ {
185+ return "macOS" ;
186+ }
187+ else
188+ {
189+ return "Unknown" ;
190+ }
191+ }
160192}
161193
162194internal class Telemetry
0 commit comments