/// <summary> /// Initializes the telemetry module. /// </summary> /// <param name="configuration">Telemetry configuration to use for initialization.</param> public void Initialize(TelemetryConfiguration configuration) { try { if (!this.IsInitialized) { lock (this.lockObject) { if (!this.IsInitialized) { this.telemetryClient = new TelemetryClient(configuration); // Assume default = 3, as its possible that IWebHostBuilder be removed in future and we hit exception. AspNetCoreMajorVersion aspNetCoreMajorVersion = AspNetCoreMajorVersion.Three; try { var version = typeof(IWebHostBuilder).GetTypeInfo().Assembly.GetName().Version.Major; if (version < 2) { aspNetCoreMajorVersion = AspNetCoreMajorVersion.One; } else if (version == 2) { aspNetCoreMajorVersion = AspNetCoreMajorVersion.Two; } else { aspNetCoreMajorVersion = AspNetCoreMajorVersion.Three; } } catch (Exception e) { AspNetCoreEventSource.Instance.LogError($"Exception occured while attempting to find Asp.Net Core Major version. Assuming {aspNetCoreMajorVersion.ToString()} and continuing. Exception: {e.Message}"); } #pragma warning disable CS0618 // EnableW3CDistributedTracing is obsolete. Ignore because the property inside this constructor is still in use. this.diagnosticListener = new HostingDiagnosticListener( configuration, this.telemetryClient, this.applicationIdProvider, this.CollectionOptions.InjectResponseHeaders, this.CollectionOptions.TrackExceptions, this.CollectionOptions.EnableW3CDistributedTracing, aspNetCoreMajorVersion); #pragma warning restore CS0618 this.subscriptions?.Add(DiagnosticListener.AllListeners.Subscribe(this)); this.IsInitialized = true; } } } } catch (Exception e) { AspNetCoreEventSource.Instance.RequestTrackingModuleInitializationFailed(e.ToInvariantString()); } }
/// <summary> /// Initializes a new instance of the <see cref="HostingDiagnosticListener"/> class. /// </summary> /// <param name="configuration"><see cref="TelemetryConfiguration"/> as a settings source.</param> /// <param name="client"><see cref="TelemetryClient"/> to post traces to.</param> /// <param name="applicationIdProvider">Provider for resolving application Id to be used in multiple instruemntation keys scenarios.</param> /// <param name="injectResponseHeaders">Flag that indicates that response headers should be injected.</param> /// <param name="trackExceptions">Flag that indicates that exceptions should be tracked.</param> /// <param name="enableW3CHeaders">Flag that indicates that W3C header parsing should be enabled.</param> /// <param name="aspNetCoreMajorVersion">Major version of AspNetCore.</param> public HostingDiagnosticListener( TelemetryConfiguration configuration, TelemetryClient client, IApplicationIdProvider applicationIdProvider, bool injectResponseHeaders, bool trackExceptions, bool enableW3CHeaders, AspNetCoreMajorVersion aspNetCoreMajorVersion) : this(client, applicationIdProvider, injectResponseHeaders, trackExceptions, enableW3CHeaders, aspNetCoreMajorVersion) { this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this.proactiveSamplingEnabled = this.configuration.EvaluateExperimentalFeature(ProactiveSamplingFeatureFlagName); this.conditionalAppIdEnabled = this.configuration.EvaluateExperimentalFeature(ConditionalAppIdFeatureFlagName); }
private HostingDiagnosticListener CreateHostingListener(AspNetCoreMajorVersion aspNetCoreMajorVersion) { var hostingListener = new HostingDiagnosticListener( CommonMocks.MockTelemetryClient(telemetry => {}), CommonMocks.GetMockApplicationIdProvider(), injectResponseHeaders: true, trackExceptions: true, enableW3CHeaders: false, aspNetCoreMajorVersion: aspNetCoreMajorVersion); hostingListener.OnSubscribe(); return(hostingListener); }
/// <summary> /// Initializes a new instance of the <see cref="HostingDiagnosticListener"/> class. /// </summary> /// <param name="client"><see cref="TelemetryClient"/> to post traces to.</param> /// <param name="applicationIdProvider">Provider for resolving application Id to be used in multiple instruemntation keys scenarios.</param> /// <param name="injectResponseHeaders">Flag that indicates that response headers should be injected.</param> /// <param name="trackExceptions">Flag that indicates that exceptions should be tracked.</param> /// <param name="enableW3CHeaders">Flag that indicates that W3C header parsing should be enabled.</param> /// <param name="aspNetCoreMajorVersion">Major version of AspNetCore.</param> public HostingDiagnosticListener( TelemetryClient client, IApplicationIdProvider applicationIdProvider, bool injectResponseHeaders, bool trackExceptions, bool enableW3CHeaders, AspNetCoreMajorVersion aspNetCoreMajorVersion) { this.aspNetCoreMajorVersion = aspNetCoreMajorVersion; this.client = client ?? throw new ArgumentNullException(nameof(client)); this.applicationIdProvider = applicationIdProvider; this.injectResponseHeaders = injectResponseHeaders; this.trackExceptions = trackExceptions; this.enableW3CHeaders = enableW3CHeaders; AspNetCoreEventSource.Instance.HostingListenerInformational(this.aspNetCoreMajorVersion, "HostingDiagnosticListener constructed."); }
public void Configure(IWebHostBuilder builder) { // Resolve the ASP.NET Core version var informationalVersion = typeof(IWebHostBuilder).Assembly.GetCustomAttributes <AssemblyInformationalVersionAttribute>().FirstOrDefault()?.InformationalVersion; AspNetCoreVersion = (AspNetCoreMajorVersion)int.Parse((informationalVersion ?? "1.0.0").Split('.')[0]); builder.ConfigureServices(services => { services.AddHostedService <DiagnosticsHosted>(); // 3.0 and up we'll use the built in LoggingEventSource if (AspNetCoreVersion < AspNetCoreMajorVersion.Three) { services.AddLogging(b => { // Add a logger provider to capture logs b.Services.AddSingleton <ILoggerProvider, ApplicationInsightsLoggerProvider>(); }); } }); }
public void HostingListenerInformational(AspNetCoreMajorVersion hostingVersion, string message, string appDomainName = "Incorrect") { this.WriteEvent(19, hostingVersion, message, this.applicationNameProvider.Name); }