public static ILoggingBuilder AddServiceLogging( this ILoggingBuilder builder, WebHostBuilderContext webHostBuilderContext, Action <ServiceLoggingConfiguration> action) { var serviceLoggingConfiguration = new ServiceLoggingConfiguration(webHostBuilderContext); action.Invoke(serviceLoggingConfiguration); builder.ConfigureServices(serviceLoggingConfiguration); return(builder); }
private static ILoggingBuilder ConfigureServices( this ILoggingBuilder builder, ServiceLoggingConfiguration serviceLoggingConfiguration) { if (serviceLoggingConfiguration.ApplicationInsightsTelemetryOn) { builder.Services.AddApplicationInsightsTelemetry(); } builder.ClearProviders(); var configuration = serviceLoggingConfiguration.Configuration; // Add the log level configuration builder.AddConfiguration(configuration.GetSection("Logging")); if (serviceLoggingConfiguration.ProfileProvider != null) { builder.Services.AddSingleton(typeof(IProfileProvider), serviceLoggingConfiguration.ProfileProvider); } if (serviceLoggingConfiguration.RegisterActivityLogger) { builder.Services.Configure <ActivityLoggerOptions>(configuration.GetSection("ActivityLogger")); builder.Services.AddSingleton(serviceLoggingConfiguration.AccountProviderType, serviceLoggingConfiguration.AccountProvider); builder.Services.AddSingleton(typeof(ITenantProvider), serviceLoggingConfiguration.TenantProvider); builder.Services.AddSingleton(typeof(IActivityLogger <,>), typeof(ActivityLogger <,>)); } builder.Services.Configure <LoggingMiddlewareOptions>(configuration.GetSection("Logging")); builder.Services.AddSingleton <LoggingLevelSwitch>(); builder.Services.AddTransient <Action <ServiceContext> >(EnrichLoggerWithContext); builder.Services.AddTransient <LoggerConfiguration>(services => GetLoggerConfiguration(services, configuration)); builder.Services.AddTransient <ActivityLoggerLogConfigurationAdapter>(); builder.Services.AddTransient <ApplicationInsightsLoggerLogConfigurationAdapter>(); builder.Services.AddTransient(serviceProvider => JavaScriptEncoder.Default); builder.Services.AddSingleton <ILoggerProvider, LoggerProvider>(services => GetLoggerProvider(services, serviceLoggingConfiguration)); return(builder); }
private static LoggerProvider GetLoggerProvider(IServiceProvider serviceProvider, ServiceLoggingConfiguration configuration) { var loggerConfig = serviceProvider.GetRequiredService <LoggerConfiguration>(); if (configuration.LogToApplicationInsights) { var profileProvider = serviceProvider.GetService <IProfileProvider>(); var serviceContext = serviceProvider.GetService <StatelessServiceContext>() ?? serviceProvider.GetService <StatefulServiceContext>() as ServiceContext; serviceProvider.GetRequiredService <ApplicationInsightsLoggerLogConfigurationAdapter>() .ApplyConfiguration(loggerConfig, profileProvider, serviceContext); } if (configuration.LogToActivityLogger) { var activityLoggerOptions = serviceProvider.GetService <IOptions <ActivityLoggerOptions> >(); serviceProvider.GetRequiredService <ActivityLoggerLogConfigurationAdapter>() .ApplyConfiguration(loggerConfig, activityLoggerOptions.Value); } return(new LoggerProvider(loggerConfig.CreateLogger())); }