private static void StartAgent() { Agent.Setup(new AgentComponents()); var logger = StartupHookLogger.Create(); LoadDiagnosticSubscriber(new HttpDiagnosticsSubscriber(), logger); LoadDiagnosticSubscriber(new AspNetCoreDiagnosticSubscriber(), logger); LoadDiagnosticSubscriber(new EfCoreDiagnosticsSubscriber(), logger); LoadDiagnosticSubscriber(new SqlClientDiagnosticSubscriber(), logger); LoadDiagnosticSubscriber(new ElasticsearchDiagnosticsSubscriber(), logger); LoadDiagnosticSubscriber(new GrpcClientDiagnosticSubscriber(), logger); HostBuilderExtensions.UpdateServiceInformation(Agent.Instance.Service);
/// <summary> /// The Initialize method called by DOTNET_STARTUP_HOOKS /// </summary> public static void Initialize() { var startupHookEnvVar = Environment.GetEnvironmentVariable("DOTNET_STARTUP_HOOKS"); var startupHookDirectory = Path.GetDirectoryName(startupHookEnvVar); if (string.IsNullOrEmpty(startupHookEnvVar) || !File.Exists(startupHookEnvVar)) { return; } _logger = StartupHookLogger.Create(); _logger.WriteLine($"Check if {SystemDiagnosticsDiagnosticsource} is loaded"); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); _logger.WriteLine($"Assemblies loaded:{Environment.NewLine}{string.Join(Environment.NewLine, assemblies.Select(a => a.GetName()))}"); var diagnosticSourceAssemblies = assemblies .Where(a => a.GetName().Name.Equals(SystemDiagnosticsDiagnosticsource, StringComparison.Ordinal)) .ToList(); Assembly diagnosticSourceAssembly; switch (diagnosticSourceAssemblies.Count) { case 0: _logger.WriteLine($"No {SystemDiagnosticsDiagnosticsource} loaded. Load using AssemblyLoadContext.Default"); diagnosticSourceAssembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(SystemDiagnosticsDiagnosticsource)); break; case 1: diagnosticSourceAssembly = diagnosticSourceAssemblies[0]; break; default: _logger.WriteLine($"Found {diagnosticSourceAssemblies.Count} {SystemDiagnosticsDiagnosticsource} assemblies loaded in the app domain"); diagnosticSourceAssembly = diagnosticSourceAssemblies.First(); break; } Assembly loaderAssembly = null; string loaderDirectory; if (diagnosticSourceAssembly is null) { // use agent compiled against the highest version of System.Diagnostics.DiagnosticSource var highestAvailableAgent = Directory.EnumerateDirectories(startupHookDirectory) .Where(d => VersionRegex.IsMatch(d)) .OrderByDescending(d => VersionRegex.Match(d).Groups["major"].Value) .First(); loaderDirectory = Path.Combine(startupHookDirectory, highestAvailableAgent); loaderAssembly = AssemblyLoadContext.Default .LoadFromAssemblyPath(Path.Combine(loaderDirectory, LoaderDll)); } else { var diagnosticSourceAssemblyName = diagnosticSourceAssembly.GetName(); var diagnosticSourcePublicKeyToken = diagnosticSourceAssemblyName.GetPublicKeyToken(); if (!diagnosticSourcePublicKeyToken.SequenceEqual(SystemDiagnosticsDiagnosticSourcePublicKeyToken)) { _logger.WriteLine($"{SystemDiagnosticsDiagnosticsource} public key token " + $"{PublicKeyTokenBytesToString(diagnosticSourcePublicKeyToken)} did not match expected " + $"public key token {PublicKeyTokenBytesToString(SystemDiagnosticsDiagnosticSourcePublicKeyToken)}"); return; } var diagnosticSourceVersion = diagnosticSourceAssemblyName.Version; _logger.WriteLine($"{SystemDiagnosticsDiagnosticsource} {diagnosticSourceVersion} loaded"); loaderDirectory = Path.Combine(startupHookDirectory, $"{diagnosticSourceVersion.Major}.0.0"); if (Directory.Exists(loaderDirectory)) { loaderAssembly = AssemblyLoadContext.Default .LoadFromAssemblyPath(Path.Combine(loaderDirectory, LoaderDll)); } else { _logger.WriteLine( $"No compatible agent for {SystemDiagnosticsDiagnosticsource} {diagnosticSourceVersion}. Agent not loaded"); } } if (loaderAssembly is null) { _logger.WriteLine( $"No {LoaderDll} assembly loaded. Agent not loaded"); } LoadAssembliesFromLoaderDirectory(loaderDirectory); InvokerLoaderMethod(loaderAssembly); }