Esempio n. 1
0
        protected virtual ILog CreateLogWithSlack(IServiceCollection services, IReloadingManager <TApplicationSettings> settings, bool isLive)
        {
            var logToConsole    = new LogToConsole();
            var aggregateLogger = new AggregateLogger();

            aggregateLogger.AddLog(logToConsole);

            var commonSlackService =
                services.UseSlackNotificationsSenderViaAzureQueue(settings.CurrentValue.SlackNotifications.AzureQueue,
                                                                  aggregateLogger);

            var slackService =
                new MtSlackNotificationsSender(commonSlackService, ApplicationName, Configuration.ServerType());

            services.AddSingleton <ISlackNotificationsSender>(slackService);

            // Creating azure storage logger, which logs own messages to concole log
            var dbLogConnectionString = settings.CurrentValue.MtBrokersLogs?.DbConnString;

            if (!string.IsNullOrEmpty(dbLogConnectionString) &&
                !(dbLogConnectionString.StartsWith("${") && dbLogConnectionString.EndsWith("}")))
            {
                var logToAzureStorage = services.UseLogToAzureStorage(
                    settings.Nested(s => s.MtBrokersLogs.DbConnString), slackService,
                    ApplicationName + Configuration.ServerType() + "Log",
                    aggregateLogger);

                aggregateLogger.AddLog(logToAzureStorage);
            }

            return(aggregateLogger);
        }
Esempio n. 2
0
        private static void SetupLoggers(IServiceCollection services, IReloadingManager <MtBackendSettings> mtSettings,
                                         IReloadingManager <MarginSettings> settings)
        {
            var consoleLogger = new LogToConsole();

            var azureQueue = new AzureQueueSettings
            {
                ConnectionString = mtSettings.CurrentValue.SlackNotifications.AzureQueue.ConnectionString,
                QueueName        = mtSettings.CurrentValue.SlackNotifications.AzureQueue.QueueName
            };

            var commonSlackService =
                services.UseSlackNotificationsSenderViaAzureQueue(azureQueue, consoleLogger);

            var slackService =
                new MtSlackNotificationsSender(commonSlackService, "MT Backend", settings.CurrentValue.Env);

            services.AddSingleton <ISlackNotificationsSender>(slackService);
            services.AddSingleton <IMtSlackNotificationsSender>(slackService);

            // Order of logs registration is important - UseLogToAzureStorage() registers ILog in container.
            // Last registration wins.
            LogLocator.RequestsLog = services.UseLogToAzureStorage(settings.Nested(s => s.Db.LogsConnString),
                                                                   slackService, "MarginTradingBackendRequestsLog", consoleLogger);

            LogLocator.CommonLog = services.UseLogToAzureStorage(settings.Nested(s => s.Db.LogsConnString),
                                                                 slackService, "MarginTradingBackendLog", consoleLogger);
        }
Esempio n. 3
0
        private static void SetupLoggers(IServiceCollection services, IReloadingManager <AppSettings> mtSettings,
                                         IReloadingManager <DataReaderSettings> settings)
        {
            var consoleLogger = new LogToConsole();

            var commonSlackService =
                services.UseSlackNotificationsSenderViaAzureQueue(mtSettings.CurrentValue.SlackNotifications.AzureQueue,
                                                                  new LogToConsole());

            var slackService =
                new MtSlackNotificationsSender(commonSlackService, "MT DataReader", settings.CurrentValue.Env);

            var log = services.UseLogToAzureStorage(settings.Nested(s => s.Db.LogsConnString), slackService,
                                                    "MarginTradingDataReaderLog", consoleLogger);

            LogLocator.CommonLog = log;
        }
Esempio n. 4
0
        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);
        }