Example #1
0
        private static ILog CreateLogWithSlack(IServiceCollection services, AppSettings settings)
        {
            LykkeLogToAzureStorage logToAzureStorage = null;

            var logToConsole = new LogToConsole();
            var logAggregate = new LogAggregate();

            logAggregate.AddLogger(logToConsole);

            var dbLogConnectionString = settings.LykkeServiceService.Db.LogsConnString;

            // Creating azure storage logger, which logs own messages to concole log
            if (!string.IsNullOrEmpty(dbLogConnectionString) && !(dbLogConnectionString.StartsWith("${") && dbLogConnectionString.EndsWith("}")))
            {
                logToAzureStorage = new LykkeLogToAzureStorage("Lykke.Service.LykkeService", new AzureTableStorage <LogEntity>(
                                                                   dbLogConnectionString, "LykkeServiceLog", logToConsole));

                logAggregate.AddLogger(logToAzureStorage);
            }

            // Creating aggregate log, which logs to console and to azure storage, if last one specified
            var log = logAggregate.CreateLogger();

            // Creating slack notification service, which logs own azure queue processing messages to aggregate log
            var slackService = services.UseSlackNotificationsSenderViaAzureQueue(new AzureQueueIntegration.AzureQueueSettings
            {
                ConnectionString = settings.SlackNotifications.AzureQueue.ConnectionString,
                QueueName        = settings.SlackNotifications.AzureQueue.QueueName
            }, log);

            // Finally, setting slack notification for azure storage log, which will forward necessary message to slack service
            logToAzureStorage?.SetSlackNotification(slackService);

            return(log);
        }
        private static ILog CreateLog(IServiceCollection services, ApplicationSettings settings)
        {
            var appSettings = settings.MarketProfileService;

            LykkeLogToAzureStorage logToAzureStorage = null;
            var logToConsole = new LogToConsole();
            var logAggregate = new LogAggregate();

            logAggregate.AddLogger(logToConsole);

            if (!string.IsNullOrEmpty(appSettings.Db.LogsConnectionString) &&
                !(appSettings.Db.LogsConnectionString.StartsWith("${") && appSettings.Db.LogsConnectionString.EndsWith("}")))
            {
                logToAzureStorage = new LykkeLogToAzureStorage("Lykke.Service.MarketProfile", new AzureTableStorage <LogEntity>(
                                                                   appSettings.Db.LogsConnectionString, "MarketProfileService", logToConsole));

                logAggregate.AddLogger(logToAzureStorage);
            }

            var log = logAggregate.CreateLogger();

            var slackService = services.UseSlackNotificationsSenderViaAzureQueue(new AzureQueueSettings
            {
                ConnectionString = settings.SlackNotifications.AzureQueue.ConnectionString,
                QueueName        = settings.SlackNotifications.AzureQueue.QueueName
            }, log);

            logToAzureStorage?.SetSlackNotification(slackService);
            return(log);
        }
Example #3
0
        private static ILog ConfigureLog(ApplicationSettings settings, LogAggregate logAggregate, ILog log)
        {
            log.Info("Initializing azure/slack logger.", Program.Name);
            var services = new ServiceCollection(); // only used for azure logger

            logAggregate.ConfigureAzureLogger(services, Program.Name, settings);
            return(logAggregate.CreateLogger());
        }
Example #4
0
        public static void ConfigureAzureLogger(this LogAggregate logAggregate, IServiceCollection services, string appName, AppSettings appSettings)
        {
            var log         = logAggregate.CreateLogger();
            var slackSender = services.UseSlackNotificationsSenderViaAzureQueue(appSettings.SlackNotifications.AzureQueue, log);
            var azureLog    = new LykkeLogToAzureStorage(appName,
                                                         new AzureTableStorage <LogEntity>(appSettings.BrokerQuoteFeed.ConnectionStrings.LogsConnectionString, appName + "Logs", log),
                                                         slackSender);

            logAggregate.AddLogger(azureLog);
        }
Example #5
0
        public static void Main(string[] args)
        {
            // Initialize logger
            var consoleLog   = new LogToConsole();
            var logAggregate = new LogAggregate()
                               .AddLogger(consoleLog);
            var log = logAggregate.CreateLogger();

            try
            {
                log.Info("Reading application settings.");
                var config = new ConfigurationBuilder()
                             //.AddJsonFile("appsettings.json", optional: true)
                             .AddEnvironmentVariables()
                             .Build();

                var settingsUrl = config.GetValue <string>("BROKER_SETTINGS_URL");

                log.Info("Loading app settings from web-site.");
                var appSettings = LoadSettings(settingsUrl);

                log.Info("Initializing azure/slack logger.");
                var services = new ServiceCollection(); // only used for azure logger
                logAggregate.ConfigureAzureLogger(services, Startup.ApplicationName, appSettings);

                log = logAggregate.CreateLogger();

                // After log is configured
                //
                log.Info("Creating Startup.");
                var startup = new Startup(appSettings, log);

                log.Info("Configure startup services.");
                startup.ConfigureServices(Application.Instance.ContainerBuilder, log);

                log.Info("Starting application.");
                var scope = Application.Instance.Start();

                log.Info("Configure startup.");
                startup.Configure(scope);

                log.Info("Running application.");
                Application.Instance.Run();

                log.Info("Exit application.");
            }
            catch (Exception ex)
            {
                log.WriteErrorAsync("Program", string.Empty, string.Empty, ex).Wait();
            }
        }
Example #6
0
        // This method gets called by the runtime. Use this method to add services to the container
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddMvc(o =>
            {
                o.Filters.Add(new HandleAllExceptionsFilterFactory());
            });

            services.AddSwaggerGen(options =>
            {
                options.SingleApiVersion(new Info
                {
                    Version = "v1",
                    Title   = "Api"
                });
                options.DescribeAllEnumsAsStrings();
            });

            var logAggregate = new LogAggregate().AddLogger(new LogToConsole());
            var log          = logAggregate.CreateLogger();

            var appSettings = Environment.IsEnvironment("Development") ? Configuration.Get <ApplicationSettings>() : LoadSettings(Configuration.GetValue <string>("SETTINGS_URL"), log);

            if (!ApplicationSettings.IsSettingsValid(appSettings, log, Program.Name))
            {
                throw new ArgumentException();
            }

            log = ConfigureLog(appSettings, logAggregate, log);

            var ioc = new ContainerBuilder();

            ioc.RegisterInstance(appSettings).AsSelf();
            ioc.RegisterInstance(log).As <ILog>();
            ioc.RegisterInstance(new LogRepository(new AzureTableStorage <LogEntity>(appSettings.BoxOptionsApi.ConnectionStrings.BoxOptionsApiStorage, "ClientEventLogs", log))).As <ILogRepository>();

            ioc.Populate(services);
            return(new AutofacServiceProvider(ioc.Build()));
        }
Example #7
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ILog log     = new LogToConsole();
            var  appName = nameof(TradingBot);
            // Register dependencies, populate the services from
            // the collection, and build the container. If you want
            // to dispose of the container at the end of the app,
            // be sure to keep a reference to it as a property or field.
            var builder = new ContainerBuilder();

            try
            {
                // Add framework services.
                services.AddMvc()
                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                });

                services.AddSwaggerGen(options =>
                {
                    options.DefaultLykkeConfiguration("v1", "ExchangeConnectorAPI");
                    options.AddSecurityDefinition("CustomScheme", new ApiKeyScheme {
                        In = "header", Description = "Please insert API key into field", Name = ApiKeyAuthAttribute.HeaderName, Type = "apiKey"
                    });
                });

                var settingsManager = Configuration.LoadSettings <TradingBotSettings>("SettingsUrl");

                builder.RegisterInstance(settingsManager)
                .As <IReloadingManager <TradingBotSettings> >();

                var topSettings = settingsManager.CurrentValue;
                var settings    = topSettings.TradingBot;
                builder.RegisterInstance(settings)
                .As <AppSettings>()
                .SingleInstance();


                if (settings.AzureStorage.Enabled)
                {
                    var slackService = services.UseSlackNotificationsSenderViaAzureQueue(topSettings.SlackNotifications.AzureQueue, log);
                    var tableStorage = AzureTableStorage <LogEntity> .Create(
                        settingsManager.ConnectionString(i => i.TradingBot.AzureStorage.LogsConnString), settings.AzureStorage.LogTableName, log);

                    builder.RegisterInstance(tableStorage).As <INoSQLTableStorage <LogEntity> >().SingleInstance();
                    var persistenceManager        = new LykkeLogToAzureStoragePersistenceManager(appName, tableStorage, log);
                    var slackNotificationsManager = new LykkeLogToAzureSlackNotificationsManager(appName, slackService, log);
                    var logToTable = new LykkeLogToAzureStorage(
                        appName,
                        persistenceManager,
                        slackNotificationsManager,
                        log);
                    logToTable.Start();

                    log = new LogAggregate()
                          .AddLogger(log)
                          .AddLogger(logToTable)
                          .CreateLogger();
                }

                ApiKeyAuthAttribute.ApiKey = settings.AspNet.ApiKey;
                //   SignedModelAuthAttribute.ApiKey = settings.AspNet.ApiKey; //TODO use it somewhere

                builder.RegisterInstance(log).As <ILog>().SingleInstance();


                var signalsStorage = AzureTableStorage <TranslatedSignalTableEntity> .Create(
                    settingsManager.ConnectionString(i => i.TradingBot.AzureStorage.EntitiesConnString), settings.AzureStorage.TranslatedSignalsTableName, log);

                builder.RegisterInstance(signalsStorage).As <INoSQLTableStorage <TranslatedSignalTableEntity> >().SingleInstance();

                builder.RegisterModule(new ServiceModule(settings, log));

                builder.Populate(services);


                ApplicationContainer = builder.Build();

                _log = log;
                // Create the IServiceProvider based on the container.
                return(new AutofacServiceProvider(ApplicationContainer));
            }
            catch (Exception exc)
            {
                log.WriteErrorAsync(
                    appName,
                    nameof(Startup),
                    nameof(ConfigureServices),
                    exc,
                    DateTime.UtcNow)
                .Wait();
                throw;
            }
        }