/// <summary> /// Create telemetry client logger /// </summary> /// <param name="config"></param> /// <param name="log"></param> /// <param name="addConsole"></param> public ApplicationInsightsLogger(IDiagnosticsConfig config, LoggerConfiguration log = null, bool addConsole = true) { Logger = (log ?? new LoggerConfiguration()).Configure((c, m) => c .WriteTo.ApplicationInsights(config?.InstrumentationKey, TelemetryConverter.Traces), addConsole) .CreateLogger(); }
/// <summary> /// Add dependency tracking for Application Insights. This method should be used for .NET /// Core applications. ASP.NET Core applicatoins should rely on AddApplicationInsightsTelemetry() /// extension method for IServiceCollection. /// </summary> /// <param name="builder"></param> /// <param name="diagnosticsConfig"></param> /// <param name="processIdentity"></param> /// <returns></returns> public static ContainerBuilder AddDependencyTracking(this ContainerBuilder builder, IDiagnosticsConfig diagnosticsConfig, IProcessIdentity processIdentity) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (diagnosticsConfig == null) { throw new ArgumentNullException(nameof(diagnosticsConfig)); } if (processIdentity == null) { throw new ArgumentNullException(nameof(processIdentity)); } var telemetryInitializer = new ApplicationInsightsTelemetryInitializer(processIdentity); var telemetryConfig = TelemetryConfiguration.CreateDefault(); telemetryConfig.InstrumentationKey = diagnosticsConfig.InstrumentationKey; telemetryConfig.TelemetryInitializers.Add(telemetryInitializer); var depModule = new DependencyTrackingTelemetryModule(); // Prevent Correlation Id to be sent to certain endpoints. depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net"); depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.chinacloudapi.cn"); depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.cloudapi.de"); depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.usgovcloudapi.net"); depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("localhost"); depModule.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("127.0.0.1"); // Enable known dependency tracking, note that in future versions, we will extend this list. // Please check default settings in https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/develop/WEB/Src/DependencyCollector/DependencyCollector/ApplicationInsights.config.install.xdt depModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus"); depModule.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs"); // Initialize the module. depModule.Initialize(telemetryConfig); builder.RegisterInstance(depModule) .AsImplementedInterfaces() .SingleInstance(); return(builder); }
/// <summary> /// Register trace logger /// </summary> /// <param name="builder"></param> /// <param name="config"></param> /// <param name="log"></param> /// <param name="addConsole"></param> /// <returns></returns> #pragma warning disable IDE0060 // Remove unused parameter public static IModuleRegistrar AddDiagnostics(this ContainerBuilder builder, IDiagnosticsConfig config = null, LoggerConfiguration log = null, bool addConsole = true) { #pragma warning restore IDE0060 // Remove unused parameter if (builder == null) { throw new ArgumentNullException(nameof(builder)); } // Register metrics logger builder.RegisterType <MetricsLogger>() .AsImplementedInterfaces(); builder.RegisterType <HealthCheckRegistrar>() .AsImplementedInterfaces().SingleInstance(); return(builder.RegisterModule( new LoggerProviderModule(new TraceLogger(log, addConsole)))); }
/// <summary> /// Register telemetry client diagnostics logger /// </summary> /// <param name="builder"></param> /// <param name="config"></param> /// <param name="log"></param> /// <param name="addConsole"></param> /// <returns></returns> public static IModuleRegistrar AddDiagnostics(this ContainerBuilder builder, IDiagnosticsConfig config, LoggerConfiguration log = null, bool addConsole = true) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } // Register metrics logger builder.RegisterType <ApplicationInsightsMetrics>() .AsImplementedInterfaces().SingleInstance(); builder.RegisterType <HealthCheckRegistrar>() .AsImplementedInterfaces().SingleInstance(); return(builder.RegisterModule( new LoggerProviderModule(new ApplicationInsightsLogger(config, log, addConsole)))); }
/// <summary> /// Create telemetry client logger /// </summary> /// <param name="config"></param> /// <param name="log"></param> /// <param name="addConsole"></param> public ApplicationInsightsLogger(IDiagnosticsConfig config, LoggerConfiguration log = null, bool addConsole = true) { LogEventLevel logEventLevel; if (config?.LogLevel != null && Enum.IsDefined(typeof(LogEventLevel), config.LogLevel)) { logEventLevel = (LogEventLevel)Enum.Parse(typeof(LogEventLevel), config.LogLevel); } else { logEventLevel = LogEventLevel.Information; } Logger = (log ?? new LoggerConfiguration()).Configure((c, m) => c .WriteTo.ApplicationInsights(config?.InstrumentationKey, TelemetryConverter.Traces, logEventLevel), addConsole) .CreateLogger(); }
/// <summary> /// Create metric logger /// </summary> /// <param name="config"></param> public ApplicationInsightsMetrics(IDiagnosticsConfig config) { _telemetryClient = new TelemetryClient( new TelemetryConfiguration(config.InstrumentationKey)); }