public void DefaultConstructorTest() { var options = new SlackSinkOptions(); Assert.IsTrue(options.SlackAttachmentColors.Count == 6); Assert.IsTrue(options.SlackAttachmentFooterIcon.Count == 6); Assert.IsTrue(options.PeriodicBatchingSinkOptionsBatchSizeLimit >= 0); Assert.IsTrue(!options.PeriodicBatchingSinkOptionsPeriod.Equals(TimeSpan.Zero)); Assert.IsTrue(options.PeriodicBatchingSinkOptionsQueueLimit >= 0); }
private Serilog.ILogger CreatePipelineLogger(ILogPipelineProvider extraPipelineProvider = null) { var logSettings = extraPipelineProvider == null ? GlobalLogSettings : _extraPipelinesLogSettings[extraPipelineProvider]; var pipelineSwitchKey = GetLogLevelSwitchKey(extraPipelineProvider); var pipelineLevelSwitch = _pipelineLevelSwitches[pipelineSwitchKey]; var pipelineConfiguration = new LoggerConfiguration() .MinimumLevel.ControlledBy(pipelineLevelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit();; const string extraPipelineFilter = "IsExtraLogPipeline = true"; if (extraPipelineProvider != null) { var pipelineProviderTypeName = extraPipelineProvider.GetType().Name; pipelineConfiguration .Filter.ByIncludingOnly($"{extraPipelineFilter} and {pipelineProviderTypeName}.Id = '{extraPipelineProvider.Id}'"); } else { pipelineConfiguration .Filter.ByExcluding(extraPipelineFilter); } if (logSettings.MongoDb.IsEnabled) { const LogSinkType sinkType = LogSinkType.MongoDb; var switchKey = GetLogLevelSwitchKey(sinkType, extraPipelineProvider); var levelSwitch = _pipelineLevelSwitches[switchKey]; pipelineConfiguration .WriteTo.Logger(mongoDbLogConfiguration => { mongoDbLogConfiguration .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit(); mongoDbLogConfiguration .WriteTo.MongoDBCapped(logSettings.MongoDb.ConnectionString, cappedMaxSizeMb: 512L, collectionName: logSettings.MongoDb.CollectionName, formatProvider: GetFormatProvider(logSettings.MongoDb.Culture ?? logSettings.Culture)); }); } if (logSettings.Slack.IsEnabled) { const LogSinkType sinkType = LogSinkType.Slack; var settings = logSettings.Slack; var switchKey = GetLogLevelSwitchKey(sinkType, extraPipelineProvider); var levelSwitch = _pipelineLevelSwitches[switchKey]; var slackOptions = new SlackSinkOptions { WebHookUrl = settings.WebHookUrl, CustomChannel = settings.CustomChannel, CustomUserName = settings.CustomUsername, CustomIcon = settings.CustomIcon, BatchSizeLimit = settings.BatchSizeLimit, Period = settings.Period }; pipelineConfiguration .WriteTo.Logger(slackLogConfiguration => { slackLogConfiguration .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit(); slackLogConfiguration .WriteTo.Slack(slackOptions, formatProvider: GetFormatProvider(logSettings.Slack.Culture ?? logSettings.Culture)); }); } if (logSettings.Email.IsEnabled) { const LogSinkType sinkType = LogSinkType.Email; var settings = logSettings.Email; var switchKey = GetLogLevelSwitchKey(sinkType, extraPipelineProvider); var levelSwitch = _pipelineLevelSwitches[switchKey]; var emailOptions = new EmailConnectionInfo { EmailSubject = settings.Subject, EnableSsl = settings.EnableSsl, FromEmail = settings.From, IsBodyHtml = settings.IsBodyHtml, MailServer = settings.SmtpHost, ToEmail = string.Join(";", settings.ToList) }; emailOptions.Port = settings.SmtpPort ?? emailOptions.Port; if (!string.IsNullOrWhiteSpace(settings.SmtpUser)) { emailOptions.NetworkCredentials = new NetworkCredential(settings.SmtpUser, settings.SmtpPass); } pipelineConfiguration .WriteTo.Logger(emailLogConfiguration => { emailLogConfiguration .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit(); emailLogConfiguration .WriteTo.Email(emailOptions, mailSubject: settings.Subject, formatProvider: GetFormatProvider(logSettings.Email.Culture ?? logSettings.Culture)); }); } if (logSettings.Seq.IsEnabled) { const LogSinkType sinkType = LogSinkType.Seq; var settings = logSettings.Seq; var switchKey = GetLogLevelSwitchKey(sinkType, extraPipelineProvider); var levelSwitch = _pipelineLevelSwitches[switchKey]; pipelineConfiguration .WriteTo.Logger(seqLogConfiguration => { seqLogConfiguration .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit(); seqLogConfiguration .WriteTo.Seq(settings.Host, apiKey: settings.ApiKey); }); } if (logSettings.ElasticSearch.IsEnabled) { const LogSinkType sinkType = LogSinkType.ElasticSearch; var settings = logSettings.ElasticSearch; var switchKey = GetLogLevelSwitchKey(sinkType, extraPipelineProvider); var levelSwitch = _pipelineLevelSwitches[switchKey]; pipelineConfiguration .WriteTo.Logger(elasticSearchLogConfiguration => { elasticSearchLogConfiguration .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .Enrich.WithEnvironmentVariables(EnvironmentVariablesToLog) .Enrich.WithRebusCorrelationId(TransactionIdLogPropertyName) .Enrich.FromMassTransit(); elasticSearchLogConfiguration .WriteTo.Elasticsearch(settings.Host, settings.IndexName); }); } // When in Development Mode, // Every and all logging will also be written to the trace and literate console, no matter what. // And since each logging pipeline excludes each other logging pipeline, // this should garantee that we will not have dupes into the Literate/Trace logging. if (IsDevelopment) { pipelineConfiguration .WriteTo.Trace(formatProvider: GetFormatProvider(logSettings.Culture)) .WriteTo.Console(formatProvider: GetFormatProvider(logSettings.Culture)); } return(pipelineConfiguration.CreateLogger()); }