Пример #1
0
        /// <summary>
        /// Configures the server validating all configuration.
        /// If server cannot be configured, all errors are written to ILog
        /// provided and the method returns null.
        /// </summary>
        /// <param name="log">The log for logging output.</param>
        /// <returns>An instance of the server.</returns>
        public StockServer SetupServer(ILogQueue log)
        {
            // Validate all configuration settings
            if (!this.ValidateConfiguration())
            {
                this.ErrorMessages.ForEach(e => log.QueueLogEntry(new LogEntry(LogType.Error, e)));
                return(null);
            }

            // Now instantiate the correct site type based on the configuration.
            StockServer server = new StockServer();

            server.EmailServer           = this.emailServer;
            server.EmailFromAddress      = this.emailFromAddress;
            server.EmailUsername         = this.emailUsername;
            server.EmailPassword         = this.emailPassword;
            server.EmailPort             = this.emailPort.Value;
            server.EmailSSL              = this.emailSSL.Value;
            server.EmailRecipient        = this.emailRecipient;
            server.EnableVolume          = this.enableVolume.Value;
            server.AlertThreshold        = this.alertThreshold.Value;
            server.MarketCapMin          = this.marketCapMin.Value;
            server.MarketCapMax          = this.marketCapMax.Value;
            server.PriceMax              = this.priceMax.Value;
            server.PriceMin              = this.priceMin.Value;
            server.HourToRun             = this.hourToRun.Value;
            server.NumberOfDaysToSilence = this.numberOfDaysToSilence.Value;

            return(server);
        }
Пример #2
0
        public DbLogTask(ILogQueue logQueue, IServiceProvider serviceProvider)
        {
            _logQueue = logQueue;
            var scope = serviceProvider.CreateScope();

            _loggerDbContext = scope.ServiceProvider.GetRequiredService <LoggerDbContext>();
        }
Пример #3
0
 public QueueLoggerProvider(ILogQueue logQueue, ILogServiceClient logServiceClient, LogOptions options)
 {
     _logQueue                = logQueue;
     _logServiceClient        = logServiceClient;
     _options                 = options;
     _filter                  = (category, logLevel) => logLevel >= options.MinLevel && category.StartsWith("");
     _cancellationTokenSource = new CancellationTokenSource();
     _logHandlerTask          = Task.Factory.StartNew(ProcessLogQueue, null, TaskCreationOptions.LongRunning);
 }
Пример #4
0
        /// <summary>
        /// Initialises a new instance of the <see cref="EmailQueue" /> class.
        /// </summary>
        /// <param name="emailServerName">The hostname of the email server</param>
        /// <param name="emailPort">The access port of the email server</param>
        /// <param name="emailUsername">The username of the email server</param>
        /// <param name="emailPassword">The password of the email server</param>
        /// <param name="emailFromAddress">The address where message will come from</param>
        /// <param name="emailSSL">A flag if the email server should use SSL</param>
        /// <param name="sleepMilliseconds">The number of milliseconds between checking for new messages in the queue.</param>
        /// <param name="logQueue">An instance of the logging engine</param>
        public EmailQueue(string emailServerName, int emailPort, string emailUsername, string emailPassword, string emailFromAddress, bool emailSSL, int sleepMilliseconds, ILogQueue logQueue)
        {
            this.logQueue              = logQueue;
            this.fromAddress           = emailFromAddress;
            this.emailMessageQueue     = new Queue <EmailQueueItem>();
            this.emailServerConnection = new EmailServerConnection(emailServerName, emailPort, emailUsername, emailPassword, emailFromAddress, emailSSL);

            this.emailThread = new Thread(delegate() { this.ProcessEmailQueue(sleepMilliseconds); });
            this.emailThread.Start();
        }
Пример #5
0
 public LogUploader(
     LoggingServiceV2Client client,
     IScheduler scheduler,
     IClock clock,
     ILogQueue logQ,
     LogEntry logsLostWarningTemplate,
     int maxUploadBatchSize,
     BackoffSettings serverErrorBackoffSettings)
 {
     _client    = client;
     _scheduler = scheduler;
     _clock     = clock;
     _logQ      = logQ;
     _logsLostWarningTemplate    = logsLostWarningTemplate;
     _maxUploadBatchSize         = maxUploadBatchSize;
     _serverErrorBackoffSettings = serverErrorBackoffSettings;
     _uploaderTask = Task.Run(RunUploader);
 }
Пример #6
0
 public LogUploader(
     LoggingServiceV2Client client,
     IScheduler scheduler,
     IClock clock,
     ILogQueue logQ,
     LogEntry logsLostWarningTemplate,
     int maxUploadBatchSize,
     RetrySettings serverErrorRetrySettings)
 {
     _client    = client;
     _scheduler = scheduler;
     _clock     = clock;
     _logQ      = logQ;
     _logsLostWarningTemplate  = logsLostWarningTemplate;
     _maxUploadBatchSize       = maxUploadBatchSize;
     _serverErrorRetrySettings = serverErrorRetrySettings;
     _uploaderTaskCancellation = new CancellationTokenSource();
     _uploaderTask             = Task.Run(() => RunUploader(_uploaderTaskCancellation.Token));
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="YahooHistoricWebStockEngine" /> class.
 /// </summary>
 /// <param name="logQueue">An instance of the logging engine</param>
 public YahooHistoricWebStockEngine(ILogQueue logQueue)
 {
     this.logQueue = logQueue;
 }
Пример #8
0
        /// <inheritdoc/>
        public override void ActivateOptions()
        {
            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? LoggingServiceV2Client.Create();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;
            _platform  = _platform ?? Platform.Instance();

            // Normalize string configuration
            ResourceType = string.IsNullOrWhiteSpace(ResourceType) ? null : ResourceType;
            ProjectId    = string.IsNullOrWhiteSpace(ProjectId) ? null : ProjectId;
            LogId        = string.IsNullOrWhiteSpace(LogId) ? null : LogId;

            // Validate configuration
            GaxPreconditions.CheckState(LogId != null, $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxMemorySize)} must be configured to be > 0");
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            ActivateLogIdAndResource();
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            _initIdTask = Task.Run(_logQ.GetPreviousExecutionIdAsync);
            var labels = new Dictionary <string, string>();

            foreach (var customLabel in _customLabels)
            {
                labels.Add(customLabel.Key, customLabel.Value);
            }
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                Labels      = { _customLabels.ToDictionary(x => x.Key, x => x.Value) },
            };
            var serverErrorBackoffSettings = new BackoffSettings(
                delay: TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                delayMultiplier: ServerErrorBackoffMultiplier,
                maxDelay: TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds)
                );

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorBackoffSettings);
            _isActivated = true;
        }
        /// <inheritdoc/>
        public override void ActivateOptions()
        {
            // Validate configuration
            GaxPreconditions.CheckState(string.IsNullOrWhiteSpace(CredentialFile) || string.IsNullOrWhiteSpace(CredentialJson),
                                        $"{nameof(CredentialFile)} and {nameof(CredentialJson)} must not both be set.");
            GaxPreconditions.CheckState(LogId != null, $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));

            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? BuildLoggingServiceClient();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;
            _platform  = _platform ?? Platform.Instance();

            // Normalize string configuration
            ResourceType = string.IsNullOrWhiteSpace(ResourceType) ? null : ResourceType;
            ProjectId    = string.IsNullOrWhiteSpace(ProjectId) ? null : ProjectId;
            LogId        = string.IsNullOrWhiteSpace(LogId) ? null : LogId;

            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxMemorySize)} must be configured to be > 0");
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            ActivateLogIdAndResource();
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            default:
                throw new InvalidOperationException($"Invalid {nameof(Log4Net.LocalQueueType)}: '{LocalQueueType}'");
            }
            _initIdTask = Task.Run(_logQ.GetPreviousExecutionIdAsync);
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                // Patterns included in custom labels will not be used in this "logs lost" entry.
                // The pattern itself will be logged, regardless of the "UsePatternWithinCustomLabels" setting.
                // This is acceptable as most patterns will be irrelevant in this context.
                Labels = { _customLabels.ToDictionary(x => x.Key, x => x.Value) },
            };
            var serverErrorRetrySettings = RetrySettings.FromExponentialBackoff(maxAttempts: int.MaxValue,
                                                                                initialBackoff: TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                                                                                maxBackoff: TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds),
                                                                                backoffMultiplier: ServerErrorBackoffMultiplier,
                                                                                retryFilter: _ => true); // Ignored

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorRetrySettings);
            if (_usePatternWithinCustomLabels)
            {
                // Initialize a pattern layout for each custom label.
                _customLabelsPatterns = _customLabels.Select(x => new CustomLabelPattern(x.Key, new PatternLayout(x.Value))).ToArray();
            }
            _isActivated = true;
        }
Пример #10
0
 public DbLoggerProvider(ILogQueue logQueue)
 {
     _logQueue = logQueue;
 }
        public override void ActivateOptions()
        {
            base.ActivateOptions();

            // Initialise services if not already initialised for testing
            _client    = _client ?? LoggingServiceV2Client.Create();
            _scheduler = _scheduler ?? SystemScheduler.Instance;
            _clock     = _clock ?? SystemClock.Instance;

            // Validate configuration
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(ResourceType), $"{nameof(ResourceType)} must be set.");
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(ProjectId), $"{nameof(ProjectId)} must be set.");
            GaxPreconditions.CheckState(!string.IsNullOrEmpty(LogId), $"{nameof(LogId)} must be set.");
            GaxPreconditions.CheckState(MaxUploadBatchSize > 0, $"{nameof(MaxUploadBatchSize)} must be > 0");
            GaxPreconditions.CheckEnumValue <LocalQueueType>(LocalQueueType, nameof(LocalQueueType));
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                GaxPreconditions.CheckState(MaxMemoryCount > 0 || MaxMemorySize > 0,
                                            $"Either {nameof(MaxMemoryCount)} or {nameof(MaxFileSize)} must be configured to be > 0");
                break;

            case LocalQueueType.Disk:
                GaxPreconditions.CheckState(!string.IsNullOrEmpty(File), $"{nameof(File)} must be set.");
                GaxPreconditions.CheckState(MaxFileSize > 0, $"{nameof(MaxFileSize)} must be > 0");
                GaxPreconditions.CheckState(MaxSizeRollBackups > 0, $"{nameof(MaxSizeRollBackups)} must be > 0");
                break;

            default:
                throw new InvalidOperationException("Inconceivable!");
            }
            GaxPreconditions.CheckState(ServerErrorBackoffDelaySeconds >= 1,
                                        $"{nameof(ServerErrorBackoffDelaySeconds)} must be >= 1 second.");
            GaxPreconditions.CheckState(ServerErrorBackoffMultiplier > 1.1999999,
                                        $"{nameof(ServerErrorBackoffMultiplier)} must be >= 1.2");
            GaxPreconditions.CheckState(ServerErrorBackoffMaxDelaySeconds >= 20,
                                        $"{nameof(ServerErrorBackoffMaxDelaySeconds)} must be >= 20 seconds.");

            // Configure the logger from the given configuration
            _logName  = LoggingServiceV2Client.FormatLogName(ProjectId, LogId);
            _resource = new MonitoredResource
            {
                Type = ResourceType
            };
            switch (LocalQueueType)
            {
            case LocalQueueType.Memory:
                _logQ = new MemoryLogQueue(MaxMemorySize, MaxMemoryCount);
                break;

            case LocalQueueType.Disk:
                throw new NotImplementedException("File-base local queues not implemented.");

            default:
                throw new InvalidOperationException("Inconceivable!");
            }
            _initNextIdTask = Task.Run(InitNextId);
            var labels = new Dictionary <string, string>();

            foreach (var customLabel in _customLabels)
            {
                labels.Add(customLabel.Key, customLabel.Value);
            }
            var logsLostWarningEntry = new LogEntry
            {
                TextPayload = s_logsLostWarningMessage,
                Severity    = LogSeverity.Warning,
                LogName     = _logName,
                Resource    = _resource,
                Labels      = { labels },
            };
            var serverErrorBackoffSettings = new BackoffSettings
            {
                Delay           = TimeSpan.FromSeconds(ServerErrorBackoffDelaySeconds),
                DelayMultiplier = ServerErrorBackoffMultiplier,
                MaxDelay        = TimeSpan.FromSeconds(ServerErrorBackoffMaxDelaySeconds),
            };

            _logUploader = new LogUploader(
                _client, _scheduler, _clock,
                _logQ, logsLostWarningEntry, MaxUploadBatchSize,
                serverErrorBackoffSettings);
        }
Пример #12
0
 /// <summary>
 /// Initialises a new instance of the <see cref="VolumeModel" /> class.
 /// </summary>
 /// <param name="alertThreshold">the threshold at which volume above the average should alert on</param>
 /// <param name="logQueue">Instance of the logging engine</param>
 public VolumeModel(double alertThreshold, ILogQueue logQueue)
 {
     this.alertThreshold = alertThreshold;
     this.logQueue       = logQueue;
 }
Пример #13
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment environment, ILoggerFactory loggerFactory, ILogQueue logQueue)
        {
            if (environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            loggerFactory.AddProvider(new DbLoggerProvider(logQueue));

            app.UseRouting();

            app.UseCors("Default");

            app.UseEndpoints(endpoint =>
            {
                endpoint.MapControllers();
                endpoint.MapHealthChecks("/health");
            });
        }
Пример #14
0
 public Logger(string name, Func <string, LogLevel, bool> filter, ILogQueue logQueue)
 {
     _name     = name;
     _filter   = filter;
     _logQueue = logQueue;
 }