private static string GetLogLevelSwitchKey(LogSinkType sinkType = LogSinkType.None, ILogPipelineProvider extraPipelineProvider = null) { var sinkTypeKey = $"{(sinkType == LogSinkType.None ? string.Empty : "::" + sinkType)}"; return($"{extraPipelineProvider?.Id.ToString("N") ?? "Global"}{sinkTypeKey}"); }
private static string GetOverrideMapKey(string sourceName, LogSinkType sinkType = LogSinkType.None, ILogPipelineProvider extraPipelineProvider = null) { var pipelineKey = extraPipelineProvider?.Id.ToString("N") ?? "Global"; var sinkTypeKey = $"{(sinkType == LogSinkType.None ? string.Empty : "::" + sinkType)}"; return($"{pipelineKey}{sinkTypeKey}::{sourceName}"); }
ILogSink BuildLogSink(LogSinkType sinkType, string name) { switch (sinkType) { case LogSinkType.FileName: return new FileLogSink(name); case LogSinkType.TableName: return new FileLogSink(name); //todo } return new FileLogSink(name); //todo }
ILogSink BuildLogSink(LogSinkType sinkType, string name) { switch (sinkType) { case LogSinkType.FileName: return(new FileLogSink(name)); case LogSinkType.TableName: return(new FileLogSink(name)); //todo } return(new FileLogSink(name)); //todo }
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()); }