private static void SetupLoggers(IConfiguration configuration, IServiceCollection services, IReloadingManager <MtBackendSettings> mtSettings, CorrelationContextAccessor correlationContextAccessor) { var settings = mtSettings.Nested(x => x.MtBackend); const string requestsLogName = "MarginTradingBackendRequestsLog"; const string logName = "MarginTradingBackendLog"; var consoleLogger = new LogToConsole(); #region Logs settings validation if (!settings.CurrentValue.UseSerilog && string.IsNullOrWhiteSpace(settings.CurrentValue.Db.LogsConnString)) { throw new Exception("Either UseSerilog must be true or LogsConnString must be set"); } #endregion Logs settings validation #region Slack registration IMtSlackNotificationsSender slackService = null; if (mtSettings.CurrentValue.SlackNotifications != null) { var azureQueue = new AzureQueueSettings { ConnectionString = mtSettings.CurrentValue.SlackNotifications.AzureQueue.ConnectionString, QueueName = mtSettings.CurrentValue.SlackNotifications.AzureQueue.QueueName }; var commonSlackService = services.UseSlackNotificationsSenderViaAzureQueue(azureQueue, consoleLogger); slackService = new MtSlackNotificationsSender(commonSlackService, "MT Backend", settings.CurrentValue.Env); } #endregion Slack registration if (settings.CurrentValue.UseSerilog) { LogLocator.RequestsLog = LogLocator.CommonLog = new SerilogLogger(typeof(Startup).Assembly, configuration, new List <Func <(string Name, object Value)> > { () => ("BrokerId", settings.CurrentValue.BrokerId) }, new List <ILogEventEnricher> { new CorrelationLogEventEnricher("CorrelationId", correlationContextAccessor) }); } else if (settings.CurrentValue.Db.StorageMode == StorageMode.SqlServer) { LogLocator.RequestsLog = new AggregateLogger( new LogToSql(new SqlLogRepository(requestsLogName, settings.CurrentValue.Db.LogsConnString)), new LogToConsole()); LogLocator.CommonLog = new AggregateLogger( new LogToSql(new SqlLogRepository(logName, settings.CurrentValue.Db.LogsConnString)), new LogToConsole()); } else if (settings.CurrentValue.Db.StorageMode == StorageMode.Azure) { if (slackService == null) { slackService = new MtSlackNotificationsSenderLogStub("MT Backend", settings.CurrentValue.Env, consoleLogger); } LogLocator.RequestsLog = services.UseLogToAzureStorage(settings.Nested(s => s.Db.LogsConnString), slackService, requestsLogName, consoleLogger); LogLocator.CommonLog = services.UseLogToAzureStorage(settings.Nested(s => s.Db.LogsConnString), slackService, logName, consoleLogger); } if (slackService == null) { slackService = new MtSlackNotificationsSenderLogStub("MT Backend", settings.CurrentValue.Env, LogLocator.CommonLog); } services.AddSingleton <ISlackNotificationsSender>(slackService); services.AddSingleton <IMtSlackNotificationsSender>(slackService); services.AddSingleton <ILoggerFactory>(x => new WebHostLoggerFactory(LogLocator.CommonLog)); }
protected virtual ILog CreateLogWithSlack(IServiceCollection services, IReloadingManager <TApplicationSettings> settings, CurrentApplicationInfo applicationInfo, CorrelationContextAccessor correlationContextAccessor) { var logTableName = ApplicationName + applicationInfo.EnvInfo + "Log"; var aggregateLogger = new AggregateLogger(); var consoleLogger = new LogToConsole(); aggregateLogger.AddLog(consoleLogger); #region Logs settings validation if (!settings.CurrentValue.MtBrokersLogs.UseSerilog && string.IsNullOrWhiteSpace(settings.CurrentValue.MtBrokersLogs.LogsConnString)) { throw new Exception("Either UseSerilog must be true or LogsConnString must be set"); } #endregion Logs settings validation #region Slack registration IMtSlackNotificationsSender slackService = null; if (settings.CurrentValue.SlackNotifications != null) { var azureQueue = new AzureQueueSettings { ConnectionString = settings.CurrentValue.SlackNotifications.AzureQueue.ConnectionString, QueueName = settings.CurrentValue.SlackNotifications.AzureQueue.QueueName }; var commonSlackService = services.UseSlackNotificationsSenderViaAzureQueue(azureQueue, consoleLogger); slackService = new MtSlackNotificationsSender(commonSlackService, ApplicationName, Environment.EnvironmentName); } else { slackService = new MtSlackNotificationsSenderLogStub(ApplicationName, Environment.EnvironmentName, consoleLogger); } services.AddSingleton <ISlackNotificationsSender>(slackService); services.AddSingleton <IMtSlackNotificationsSender>(slackService); #endregion Slack registration if (settings.CurrentValue.MtBrokersLogs.UseSerilog) { aggregateLogger.AddLog(new SerilogLogger(applicationInfo.GetType().Assembly, Configuration, new List <ILogEventEnricher> { new CorrelationLogEventEnricher("CorrelationId", correlationContextAccessor) })); } else if (settings.CurrentValue.MtBrokersLogs.StorageMode == StorageMode.SqlServer) { aggregateLogger.AddLog(new LogToSql(new SqlLogRepository(logTableName, settings.CurrentValue.MtBrokersLogs.LogsConnString))); } else if (settings.CurrentValue.MtBrokersLogs.StorageMode == StorageMode.Azure) { var dbLogConnectionStringManager = settings.Nested(x => x.MtBrokersLogs.LogsConnString); var dbLogConnectionString = dbLogConnectionStringManager.CurrentValue; if (string.IsNullOrEmpty(dbLogConnectionString)) { consoleLogger.WriteWarningAsync(ApplicationName, nameof(CreateLogWithSlack), "Table logger is not initialized").Wait(); return(aggregateLogger); } if (dbLogConnectionString.StartsWith("${") && dbLogConnectionString.EndsWith("}")) { throw new InvalidOperationException($"LogsConnString {dbLogConnectionString} is not filled in settings"); } // Creating azure storage logger, which logs own messages to console log var azureStorageLogger = services.UseLogToAzureStorage(settings.Nested(s => s.MtBrokersLogs.LogsConnString), slackService, logTableName, consoleLogger); azureStorageLogger.Start(); aggregateLogger.AddLog(azureStorageLogger); } return(aggregateLogger); }