Exemplo n.º 1
0
 public ValidateOrderService(
     IQuoteCacheService quoteCashService,
     IAccountUpdateService accountUpdateService,
     IAccountsCacheService accountsCacheService,
     ITradingInstrumentsCacheService accountAssetsCacheService,
     IAssetPairsCache assetPairsCache,
     OrdersCache ordersCache,
     IAssetPairDayOffService assetDayOffService,
     IIdentityGenerator identityGenerator,
     IDateService dateService,
     MarginTradingSettings marginSettings,
     ICfdCalculatorService cfdCalculatorService,
     IFeatureManager featureManager,
     CorrelationContextAccessor correlationContextAccessor)
 {
     _quoteCashService           = quoteCashService;
     _accountUpdateService       = accountUpdateService;
     _accountsCacheService       = accountsCacheService;
     _tradingInstrumentsCache    = accountAssetsCacheService;
     _assetPairsCache            = assetPairsCache;
     _ordersCache                = ordersCache;
     _assetDayOffService         = assetDayOffService;
     _identityGenerator          = identityGenerator;
     _dateService                = dateService;
     _marginSettings             = marginSettings;
     _cfdCalculatorService       = cfdCalculatorService;
     _featureManager             = featureManager;
     _correlationContextAccessor = correlationContextAccessor;
 }
        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                services
                .AddApplicationInsightsTelemetry()
                .AddControllers()
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                    options.SerializerSettings.Converters.Add(new StringEnumConverter());
                });

                _mtSettingsManager = Configuration.LoadSettings <AppSettings>(
                    throwExceptionOnCheckError: !Configuration.NotThrowExceptionsOnServiceValidation());

                services.AddApiKeyAuth(_mtSettingsManager.CurrentValue.MarginTradingAccountManagementServiceClient);

                services.AddSwaggerGen(options =>
                {
                    options.DefaultLykkeConfiguration("v1", ServiceName + " API");
                    var contractsXmlPath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,
                                                        "MarginTrading.AccountsManagement.Contracts.xml");
                    options.IncludeXmlComments(contractsXmlPath);
                    options.OperationFilter <CustomOperationIdOperationFilter>();
                    if (!string.IsNullOrWhiteSpace(_mtSettingsManager.CurrentValue
                                                   .MarginTradingAccountManagementServiceClient?.ApiKey))
                    {
                        options.AddApiKeyAwareness();
                    }
                }).AddSwaggerGenNewtonsoftSupport();

                services.AddStackExchangeRedisCache(o =>
                {
                    o.Configuration = _mtSettingsManager.CurrentValue.MarginTradingAccountManagement.Cache.RedisConfiguration;
                    o.InstanceName  = "AccountManagement:";
                });

                services.AddSingleton <AccountsCache>();

                var correlationContextAccessor = new CorrelationContextAccessor();
                services.AddSingleton(correlationContextAccessor);
                services.AddSingleton <RabbitMqCorrelationManager>();
                services.AddSingleton <CqrsCorrelationManager>();
                services.AddTransient <HttpCorrelationHandler>();

                Log = CreateLog(Configuration, _mtSettingsManager, services, correlationContextAccessor);

                services.AddSingleton <ILoggerFactory>(x => new WebHostLoggerFactory(Log));

                services.AddFeatureManagement(_mtSettingsManager.CurrentValue.MarginTradingAccountManagement.BrokerId);
                services.AddProductComplexity(_mtSettingsManager.CurrentValue);
                services.AddBrokerSettings(_mtSettingsManager.CurrentValue);
            }
            catch (Exception ex)
            {
                Log?.WriteFatalErrorAsync(nameof(Startup), nameof(ConfigureServices), "", ex).Wait();
                throw;
            }
        }
Exemplo n.º 3
0
        public async Task CallNextMiddlewareWithHttpContextTraceIdentifierSet(bool withLogger)
        {
            // Arrange
            var generatedId = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);

            HttpContext context         = new DefaultHttpContext();
            var         responseFeature = new MockHttpResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            var next = new MockMiddleware(async(_) => await responseFeature.CompleteAsync().ConfigureAwait(false));

            var accessor = new CorrelationContextAccessor();
            ILogger <CorrelationMiddleware> logger  = withLogger ? new MockLogger() : null;
            IOptions <CorrelationOptions>   options = Options.Create(new CorrelationOptions()
            {
                CorrelationIdGenerator = (_) => generatedId
            });

            var middleware = new CorrelationMiddleware(next, accessor, logger, options);

            // Act
            await middleware.Invoke(context).ConfigureAwait(false);

            // Assert
            next.WasCalled.Should().Be(true, "the next middleware should be called");
            next.WithTraceIdentifier.Should().Be(generatedId, "the TraceIdentifier should have been set to the generared correlation id");
            context.Response.Headers.Should().Contain(Constants.XCorrelationID, generatedId);
        }
Exemplo n.º 4
0
        public void GenerateANewCorrelationIdIfTheRequestDoesNotContainOne(bool withLogger)
        {
            // Arrange
            var generatedId = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);

            HttpContext context         = new DefaultHttpContext();
            var         responseFeature = new MockHttpResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);
            var next = new MockMiddleware(async(_) => await responseFeature.CompleteAsync());

            var accessor = new CorrelationContextAccessor();
            ILogger <CorrelationMiddleware> logger  = withLogger ? new MockLogger() : null;
            IOptions <CorrelationOptions>   options = Options.Create(new CorrelationOptions()
            {
                CorrelationIdGenerator = (_) => generatedId
            });

            var middleware = new CorrelationMiddleware(next, accessor, logger, options);

            // Invoke middleware
            middleware.Invoke(context);

            // Assert
            next.WasCalled.Should().Be(true, "the next middleware should be called");
            next.WithTraceIdentifier.Should().Be(generatedId, "the TraceIdentifier should have been set");
            accessor.CorrelationContext.Should().NotBeNull("the middleware should initialize the CorrelationContext");
            accessor.CorrelationContext.CorrelationId.Should().Be(generatedId, "the middleware should initialize the CorrelationContext.CorrelatioId correctly with the generated value form the options provider function.");
            accessor.CorrelationContext.Header.Should().Be(options.Value.Header, "the middleware should initialize the CorrelationContext.Header correctly with the header from the options provided");
            context.Response.Headers.Should().Contain(Constants.XCorrelationID, generatedId);
        }
        public virtual void ConfigureServices(IServiceCollection services)
        {
            services
            .AddControllers()
            .AddApplicationPart(typeof(Hosting).Assembly)
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });

            services.AddSingleton(Configuration);

            _appSettings = Configuration.LoadSettings <TApplicationSettings>(
                throwExceptionOnCheckError: !Configuration.NotThrowExceptionsOnServiceValidation())
                           .Nested(s =>
            {
                var settings = s.MtBrokerSettings;
                if (!string.IsNullOrEmpty(Configuration["Env"]))
                {
                    settings.Env = Configuration["Env"];
                }
                SetSettingValues(settings, Configuration);
                return(s);
            });

            var clientSettings = new ClientSettings
            {
                ApiKey = _appSettings.CurrentValue.MtBrokerSettings.ApiKey
            };

            services.AddApiKeyAuth(clientSettings);

            services.AddSwaggerGen(options =>
            {
                options.DefaultLykkeConfiguration("v1", ApplicationName + " API");

                if (!string.IsNullOrWhiteSpace(clientSettings.ApiKey))
                {
                    options.AddApiKeyAwareness();
                }
            });

            var correlationContextAccessor = new CorrelationContextAccessor();

            services.AddSingleton(correlationContextAccessor);
            services.AddSingleton <RabbitMqCorrelationManager>();
            services.AddSingleton <CqrsCorrelationManager>();
            services.AddTransient <HttpCorrelationHandler>();

            Log = CreateLogWithSlack(
                services,
                _appSettings,
                new CurrentApplicationInfo(
                    PlatformServices.Default.Application.ApplicationVersion,
                    ApplicationName),
                correlationContextAccessor);

            services.AddSingleton <ILoggerFactory>(x => new WebHostLoggerFactory(Log));
        }
 public WeatherForecastController(
     ILogger <WeatherForecastController> logger,
     ConnectionFactory connectionFactory,
     CorrelationContextAccessor correlationContextAccessor)
 {
     _logger                     = logger;
     _connectionFactory          = connectionFactory;
     _correlationContextAccessor = correlationContextAccessor;
 }
        private static ILog CreateLog(
            IConfiguration configuration,
            IReloadingManager <AppSettings> settings,
            IServiceCollection services,
            CorrelationContextAccessor correlationContextAccessor)
        {
            var aggregateLogger = new AggregateLogger();
            var consoleLogger   = new LogToConsole();

            aggregateLogger.AddLog(consoleLogger);

            if (settings.CurrentValue.MarginTradingAccountManagement.UseSerilog)
            {
                aggregateLogger.AddLog(new SerilogLogger(typeof(Startup).Assembly, configuration, new List <ILogEventEnricher>
                {
                    new CorrelationLogEventEnricher("CorrelationId", correlationContextAccessor)
                }));
            }
            else if (settings.CurrentValue.MarginTradingAccountManagement.Db.StorageMode == StorageMode.SqlServer.ToString())
            {
                var sqlLogger = new LogToSql(new SqlLogRepository("AccountManagementLog",
                                                                  settings.CurrentValue.MarginTradingAccountManagement.Db.LogsConnString));

                aggregateLogger.AddLog(sqlLogger);
            }
            else if (settings.CurrentValue.MarginTradingAccountManagement.Db.StorageMode == StorageMode.Azure.ToString())
            {
                var dbLogConnectionStringManager = settings.Nested(x => x.MarginTradingAccountManagement.Db.LogsConnString);
                var dbLogConnectionString        = dbLogConnectionStringManager.CurrentValue;

                if (string.IsNullOrEmpty(dbLogConnectionString))
                {
                    consoleLogger.WriteWarningAsync(nameof(Startup), nameof(CreateLog), "Table logger is not initialized").Wait();
                    return(aggregateLogger);
                }

                if (dbLogConnectionString.StartsWith("${") && dbLogConnectionString.EndsWith("}"))
                {
                    throw new InvalidOperationException($"LogsConnString {dbLogConnectionString} is not filled in settings");
                }

                var persistenceManager = new LykkeLogToAzureStoragePersistenceManager(
                    AzureTableStorage <Lykke.Logs.LogEntity> .Create(dbLogConnectionStringManager, "AccountManagementLog", consoleLogger),
                    consoleLogger);

                // Creating azure storage logger, which logs own messages to console log
                var azureStorageLogger = new LykkeLogToAzureStorage(persistenceManager, null, consoleLogger);

                azureStorageLogger.Start();

                aggregateLogger.AddLog(azureStorageLogger);
            }

            LogLocator.Log = aggregateLogger;

            return(aggregateLogger);
        }
 public OrderBookService(IOrderBookCache orderBookCache,
                         IFakeOrderBookPublisher fakeOrderBookPublisher,
                         FakeExchangeConnectorSettings fakeExchangeConnectorSettings,
                         CorrelationContextAccessor correlationContextAccessor,
                         ILog log)
 {
     _orderBookCache                = orderBookCache;
     _fakeOrderBookPublisher        = fakeOrderBookPublisher;
     _fakeExchangeConnectorSettings = fakeExchangeConnectorSettings;
     _correlationContextAccessor    = correlationContextAccessor;
     _log = log;
 }
        protected HangfireIntegrationTests(ITestOutputHelper testOutputHelper)
        {
            // Output type + timestamp
            _testOutputHelper = testOutputHelper ?? throw new ArgumentNullException(nameof(testOutputHelper));
            _testOutputHelper.WriteLine(GetType().Name + "," + DateTime.Now.Ticks);

            MockHttp.Fallback.Respond(HttpStatusCode.OK);

            var correlationContextAccessor = new CorrelationContextAccessor();

            _correlationManager = new CorrelationManager(
                new CorrelationContextFactory(correlationContextAccessor),
                new GuidCorrelationIdFactory(),
                correlationContextAccessor,
                new TestLogger <CorrelationManager>()
                );
        }
Exemplo n.º 10
0
 public Application(
     CorrelationContextAccessor correlationContextAccessor,
     RabbitMqCorrelationManager correlationManager,
     ILoggerFactory loggerFactory,
     IOrdersHistoryRepository ordersHistoryRepository,
     ITradesRepository tradesRepository,
     ILog logger,
     Settings settings, CurrentApplicationInfo applicationInfo,
     ISlackNotificationsSender slackNotificationsSender) : base(correlationManager,
                                                                loggerFactory, logger, slackNotificationsSender, applicationInfo)
 {
     _correlationContextAccessor = correlationContextAccessor;
     _ordersHistoryRepository    = ordersHistoryRepository;
     _tradesRepository           = tradesRepository;
     _log      = logger;
     _settings = settings;
 }
        public CorrelatingMessageHandlerTests()
        {
            _contextAccessor = new CorrelationContextAccessor
            {
                CorrelationContext = new CorrelationContext
                {
                    CorrelationId = Guid.NewGuid().ToString()
                }
            };

            _mockHttp = new MockHttpMessageHandler();

            _sut        = new CorrelatingHttpMessageHandler(_contextAccessor, new OptionsWrapper <CorrelateClientOptions>(_correlateClientOptions), _mockHttp);
            _httpClient = new HttpClient(_sut)
            {
                BaseAddress = BaseUri
            };
        }
 public Application(
     CorrelationContextAccessor correlationContextAccessor,
     RabbitMqCorrelationManager correlationManager,
     ILoggerFactory loggerFactory,
     IAccountHistoryRepository accountHistoryRepository,
     ILog log,
     Settings settings,
     CurrentApplicationInfo applicationInfo,
     ISlackNotificationsSender slackNotificationsSender,
     IAccountsApi accountsApi)
     : base(correlationManager, loggerFactory, log, slackNotificationsSender, applicationInfo, MessageFormat.MessagePack)
 {
     _correlationContextAccessor = correlationContextAccessor;
     _accountHistoryRepository   = accountHistoryRepository;
     _log         = log;
     _settings    = settings;
     _accountsApi = accountsApi;
 }
Exemplo n.º 13
0
        public CorrelateOutgoingMessageStepTests()
        {
            _correlationContextAccessor = new CorrelationContextAccessor();
            _correlationIdFactoryMock   = new Mock <ICorrelationIdFactory>();

            var txItems = new ConcurrentDictionary <string, object>();

            _transactionContextMock = new Mock <ITransactionContext>();
            _transactionContextMock
            .Setup(m => m.Items)
            .Returns(txItems);

            _messageHeaders = new Dictionary <string, string>();
            _stepContext    = new OutgoingStepContext(new Message(_messageHeaders, new { }), _transactionContextMock.Object, new DestinationAddresses(Enumerable.Empty <string>()));

            _next = () => Task.CompletedTask;

            _sut = new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock.Object, new NullLoggerFactory());
        }
 public Application(
     CorrelationContextAccessor correlationContextAccessor,
     RabbitMqCorrelationManager correlationManager,
     ILoggerFactory loggerFactory,
     IPositionsHistoryRepository positionsHistoryRepository,
     IDealsRepository dealsRepository,
     ILog logger,
     IConvertService convertService,
     Settings settings,
     CurrentApplicationInfo applicationInfo,
     ISlackNotificationsSender slackNotificationsSender)
     : base(correlationManager, loggerFactory, logger, slackNotificationsSender, applicationInfo)
 {
     _correlationContextAccessor = correlationContextAccessor;
     _positionsHistoryRepository = positionsHistoryRepository;
     _dealsRepository            = dealsRepository;
     _log            = logger;
     _settings       = settings;
     _convertService = convertService;
 }
Exemplo n.º 15
0
        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                services
                .AddControllers()
                .AddNewtonsoftJson(options =>
                {
                    options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                });

                _mtSettingsManager = Configuration.LoadSettings <AppSettings>(
                    throwExceptionOnCheckError: !Configuration.NotThrowExceptionsOnServiceValidation());

                services.AddApiKeyAuth(_mtSettingsManager.CurrentValue.TradingHistoryClient);

                services.AddSwaggerGen(options =>
                {
                    options.DefaultLykkeConfiguration("v1", "TradingHistory API");
                    if (!string.IsNullOrWhiteSpace(_mtSettingsManager.CurrentValue.TradingHistoryClient?.ApiKey))
                    {
                        options.AddApiKeyAwareness();
                    }
                });

                var correlationContextAccessor = new CorrelationContextAccessor();
                services.AddSingleton <CorrelationContextAccessor>();
                services.AddSingleton <RabbitMqCorrelationManager>();
                services.AddSingleton <CqrsCorrelationManager>();
                services.AddTransient <HttpCorrelationHandler>();

                Log = CreateLogWithSlack(Configuration, services, _mtSettingsManager, correlationContextAccessor);

                services.AddSingleton <ILoggerFactory>(x => new WebHostLoggerFactory(Log));
            }
            catch (Exception ex)
            {
                Log?.WriteFatalError(nameof(Startup), nameof(ConfigureServices), ex);
                throw;
            }
        }
Exemplo n.º 16
0
        public void ConfigureServices(IServiceCollection services)
        {
            var correlationContextAccessor = new CorrelationContextAccessor();

            services.AddSingleton(correlationContextAccessor);
            services.AddSingleton <RabbitMqCorrelationManager>();
            services.AddSingleton <CqrsCorrelationManager>();
            services.AddTransient <HttpCorrelationHandler>();

            services.AddApplicationInsightsTelemetry();

            services.AddSingleton(Configuration);
            services
            .AddControllers()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
            });
            services.AddScoped <MarginTradingEnabledFilter>();
            services.AddAuthentication(KeyAuthOptions.AuthenticationScheme)
            .AddScheme <KeyAuthOptions, KeyAuthHandler>(KeyAuthOptions.AuthenticationScheme, "", options => { });

            services.AddSwaggerGen(options =>
            {
                options.DefaultLykkeConfiguration("v1", $"MarginTradingEngine_Api_{Configuration.ServerType()}");
                options.AddApiKeyAwareness();
            }).AddSwaggerGenNewtonsoftSupport();

            _mtSettingsManager = Configuration.LoadSettings <MtBackendSettings>(
                throwExceptionOnCheckError: !Configuration.NotThrowExceptionsOnServiceValidation())
                                 .Nested(s =>
            {
                s.MtBackend.Env = Configuration.ServerType();
                return(s);
            });

            services.AddFeatureManagement(_mtSettingsManager.CurrentValue.MtBackend.BrokerId);

            SetupLoggers(Configuration, services, _mtSettingsManager, correlationContextAccessor);
        }
Exemplo n.º 17
0
        public void UseTheExistingCorrelationIdIfTheRequestContainsOne(bool withLogger)
        {
            // Arrange
            var incomingCorrelationId = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);

            HttpContext context         = new DefaultHttpContext();
            var         responseFeature = new MockHttpResponseFeature();

            context.Features.Set <IHttpResponseFeature>(responseFeature);

            // Add incoming correlation id
            context.Request.Headers.Add(Constants.XCorrelationID, incomingCorrelationId);

            var next = new MockMiddleware(async(_) => await responseFeature.CompleteAsync());

            var accessor = new CorrelationContextAccessor();
            ILogger <CorrelationMiddleware> logger  = withLogger ? new MockLogger() : null;
            IOptions <CorrelationOptions>   options = Options.Create(new CorrelationOptions()
            {
                CorrelationIdGenerator = (_) => throw new InvalidOperationException("Should not be called because a request correlation id was supplied")
            });
        /// <summary>
        /// Use Correlate with Hangfire to manage the correlation context.
        /// </summary>
        /// <param name="configuration">The global configuration.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        public static IGlobalConfiguration UseCorrelate(this IGlobalConfiguration configuration, ILoggerFactory loggerFactory)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            var correlationContextAccessor = new CorrelationContextAccessor();
            var correlationManager         = new CorrelationManager(
                new CorrelationContextFactory(correlationContextAccessor),
                new GuidCorrelationIdFactory(), correlationContextAccessor,
                loggerFactory.CreateLogger <CorrelationManager>()
                );
            var correlateFilterAttribute = new CorrelateFilterAttribute(correlationContextAccessor, correlationManager);

            return(configuration.UseFilter(correlateFilterAttribute));
        }
 public AccountManagementService(IAccountsRepository accountsRepository,
                                 ITradingConditionsService tradingConditionsService,
                                 ISendBalanceCommandsService sendBalanceCommandsService,
                                 AccountManagementSettings settings,
                                 IEventSender eventSender,
                                 ILog log,
                                 ISystemClock systemClock,
                                 AccountsCache cache,
                                 IAccountBalanceChangesRepository accountBalanceChangesRepository,
                                 IDealsApi dealsApi,
                                 IEodTaxFileMissingRepository taxFileMissingRepository,
                                 IAccountsApi accountsApi,
                                 IPositionsApi positionsApi,
                                 ITradingInstrumentsApi tradingInstrumentsApi,
                                 IFeatureManager featureManager,
                                 IAuditService auditService,
                                 CorrelationContextAccessor correlationContextAccessor,
                                 IBrokerSettingsCache brokerSettingsCache)
 {
     _accountsRepository         = accountsRepository;
     _tradingConditionsService   = tradingConditionsService;
     _sendBalanceCommandsService = sendBalanceCommandsService;
     _settings    = settings;
     _eventSender = eventSender;
     _log         = log;
     _systemClock = systemClock;
     _cache       = cache;
     _accountBalanceChangesRepository = accountBalanceChangesRepository;
     _dealsApi = dealsApi;
     _taxFileMissingRepository = taxFileMissingRepository;
     _accountsApi                = accountsApi;
     _positionsApi               = positionsApi;
     _tradingInstrumentsApi      = tradingInstrumentsApi;
     _featureManager             = featureManager;
     _auditService               = auditService;
     _correlationContextAccessor = correlationContextAccessor;
     _brokerSettingsCache        = brokerSettingsCache;
 }
Exemplo n.º 20
0
        /// <summary>
        /// Configures Rebus to use Correlate as the Correlation ID provider.
        /// </summary>
        /// <param name="configurer">The options configurer.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <returns>The <see cref="OptionsConfigurer"/> instance.</returns>
        public static OptionsConfigurer EnableCorrelate(this OptionsConfigurer configurer, ILoggerFactory loggerFactory)
        {
            if (configurer == null)
            {
                throw new ArgumentNullException(nameof(configurer));
            }

            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            // Singletons
            var correlationIdFactory       = new GuidCorrelationIdFactory();
            var correlationContextAccessor = new CorrelationContextAccessor();

            configurer.Register <ICorrelationIdFactory>(ctx => correlationIdFactory);
            configurer.Register <ICorrelationContextAccessor>(ctx => correlationContextAccessor);

            // Transient
            configurer.Register <ICorrelationContextFactory>(ctx =>
                                                             new CorrelationContextFactory(
                                                                 ctx.Get <ICorrelationContextAccessor>()
                                                                 )
                                                             );
            configurer.Register <IAsyncCorrelationManager>(ctx =>
                                                           new CorrelationManager(
                                                               ctx.Get <ICorrelationContextFactory>(),
                                                               ctx.Get <ICorrelationIdFactory>(),
                                                               ctx.Get <ICorrelationContextAccessor>(),
                                                               loggerFactory.CreateLogger <CorrelationManager>()
                                                               )
                                                           );

            return(configurer
                   .RegisterSteps()
                   .ConfigurePipeline());
        }
 public CorrelationIdHandler(CorrelationContextAccessor correlationContextAccessor)
 => _correlationContextAccessor = correlationContextAccessor;
Exemplo n.º 22
0
        private static ILog CreateLogWithSlack(IConfiguration configuration, IServiceCollection services,
                                               IReloadingManager <AppSettings> settings, CorrelationContextAccessor correlationContextAccessor)
        {
            var consoleLogger   = new LogToConsole();
            var aggregateLogger = new AggregateLogger();

            aggregateLogger.AddLog(consoleLogger);

            #region Logs settings validation

            if (!settings.CurrentValue.TradingHistoryService.UseSerilog &&
                string.IsNullOrWhiteSpace(settings.CurrentValue.TradingHistoryService.Db.LogsConnString))
            {
                throw new Exception("Either UseSerilog must be true or LogsConnString must be set");
            }

            #endregion Logs settings validation

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

                slackNotificationsManager = new LykkeLogToAzureSlackNotificationsManager(slackService, consoleLogger);
            }

            if (settings.CurrentValue.TradingHistoryService.UseSerilog)
            {
                aggregateLogger.AddLog(new SerilogLogger(typeof(Startup).Assembly, configuration, new List <ILogEventEnricher>()
                {
                    new CorrelationLogEventEnricher("CorrelationId", correlationContextAccessor)
                }));
            }
            else if (settings.CurrentValue.TradingHistoryService.Db.StorageMode == StorageMode.Azure)
            {
                var persistenceManager = new LykkeLogToAzureStoragePersistenceManager(
                    AzureTableStorage <LogEntity> .Create(settings.Nested(x => x.TradingHistoryService.Db.LogsConnString),
                                                          "TradingHistoryServiceLog", consoleLogger),
                    consoleLogger);

                // Creating azure storage logger, which logs own messages to concole log
                var azureStorageLogger = new LykkeLogToAzureStorage(
                    persistenceManager,
                    slackNotificationsManager,
                    consoleLogger);

                azureStorageLogger.Start();

                aggregateLogger.AddLog(azureStorageLogger);
            }
            else if (settings.CurrentValue.TradingHistoryService.Db.StorageMode == StorageMode.SqlServer)
            {
                var sqlLogger = new LogToSql(new SqlLogRepository("TradingHistoryAPIsLog",
                                                                  settings.CurrentValue.TradingHistoryService.Db.LogsConnString));

                aggregateLogger.AddLog(sqlLogger);
            }

            LogLocator.Log = aggregateLogger;

            return(aggregateLogger);
        }
        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);
        }
Exemplo n.º 24
0
 public CorrelationContextFactory(CorrelationContextAccessor correlationContextAccessor)
 => _correlationContextAccessor = correlationContextAccessor;
Exemplo n.º 25
0
        void ILogger.Log <TState>(LogLevel logLevel, EventId eventId,
                                  TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if ((this as ILogger).IsEnabled(logLevel))
            {
                CorrelationContextAccessor CorrelationContextAccessor = new CorrelationContextAccessor();
                LogEntry Info = new LogEntry();

                Info.CorrelationId = CorrelationContextAccessor.CorrelationContext == null ? "" : CorrelationContextAccessor.CorrelationContext.CorrelationId;

                Info.Category = this.Category;
                Info.Level    = logLevel;
                // well, the passed default formatter function
                // does not take the exception into account
                // SEE: https://github.com/aspnet/Extensions/blob/master/src/Logging/Logging.Abstractions/src/LoggerExtensions.cs
                Info.Text      = exception?.Message ?? state.ToString(); // formatter(state, exception)
                Info.Exception = exception;
                Info.EventId   = eventId;
                Info.State     = state;

                // well, you never know what it really is
                if (state is string)
                {
                    Info.StateText = state.ToString();
                }
                // in case we have to do with a message template,
                // let's get the keys and values (for Structured Logging providers)
                // SEE: https://docs.microsoft.com/en-us/aspnet/core/
                // fundamentals/logging#log-message-template
                // SEE: https://softwareengineering.stackexchange.com/
                // questions/312197/benefits-of-structured-logging-vs-basic-logging
                else if (state is IEnumerable <KeyValuePair <string, object> > Properties)
                {
                    Info.StateProperties = new Dictionary <string, object>();

                    foreach (KeyValuePair <string, object> item in Properties)
                    {
                        Info.StateProperties[item.Key] = item.Value;
                    }
                }

                // gather info about scope(s), if any
                if (Provider.ScopeProvider != null)
                {
                    Provider.ScopeProvider.ForEachScope((value, loggingProps) =>
                    {
                        if (Info.Scopes == null)
                        {
                            Info.Scopes = new List <LogScopeInfo>();
                        }

                        LogScopeInfo Scope = new LogScopeInfo();
                        Info.Scopes.Add(Scope);

                        if (value is string)
                        {
                            Scope.Text = value.ToString();
                        }
                        else if (value is IEnumerable <KeyValuePair <string, object> > props)
                        {
                            if (Scope.Properties == null)
                            {
                                Scope.Properties = new Dictionary <string, object>();
                            }

                            foreach (var pair in props)
                            {
                                Scope.Properties[pair.Key] = pair.Value;
                            }
                        }
                    },
                                                        state);
                }

                Provider.WriteLog(Info);
            }
        }
Exemplo n.º 26
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));
        }