/// <summary> /// Adds a sink that sends log events via email. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="fromEmail">The email address emails will be sent from</param> /// <param name="toEmail">The email address emails will be sent to</param> /// <param name="mailServer">The SMTP email server to use</param> /// <param name="networkCredential">The network credentials to use to authenticate with mailServer</param> /// <param name="outputTemplate">A message template describing the format used to write to the sink. /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration Email( this LoggerSinkConfiguration loggerConfiguration, string fromEmail, string toEmail, string mailServer, ICredentialsByHost networkCredential, string outputTemplate = DefaultOutputTemplate, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, int batchPostingLimit = EmailSink.DefaultBatchPostingLimit, TimeSpan? period = null, IFormatProvider formatProvider = null) { if (loggerConfiguration == null) throw new ArgumentNullException("loggerConfiguration"); if (fromEmail == null) throw new ArgumentNullException("fromEmail"); if (toEmail == null) throw new ArgumentNullException("toEmail"); if (mailServer == null) throw new ArgumentNullException("mailServer"); var connectionInfo = new EmailConnectionInfo { FromEmail = fromEmail, ToEmail = toEmail, MailServer = mailServer, NetworkCredentials = networkCredential }; return Email(loggerConfiguration, connectionInfo, outputTemplate, restrictedToMinimumLevel, batchPostingLimit, period, formatProvider); }
/// <summary> /// /// </summary> /// <param name="batchSizeLimit"></param> /// <param name="period"></param> public SendGridEmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter, ITextFormatter subjectLineFormatter) : base(batchSizeLimit, period) { _connectionInfo = connectionInfo ?? throw new ArgumentNullException(nameof(connectionInfo)); _textFormatter = textFormatter; _subjectLineFormatter = subjectLineFormatter; _client = connectionInfo.SendGridClient; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) throw new ArgumentNullException(nameof(connectionInfo)); _connectionInfo = connectionInfo; _textFormatter = textFormatter; _smtpClient = CreateSmtpClient(); _smtpClient.SendCompleted += SendCompletedCallback; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> /// <param name="subjectLineFormatter">Supplies culture-specific formatting information, or null.</param> /// <exception cref="System.ArgumentNullException">connectionInfo</exception> public EmailSink(EmailConnectionInfo connectionInfo, ITextFormatter textFormatter, ITextFormatter subjectLineFormatter) { if (connectionInfo == null) { throw new ArgumentNullException(nameof(connectionInfo)); } _connectionInfo = connectionInfo; _textFormatter = textFormatter; _subjectLineFormatter = subjectLineFormatter; _smtpClient = CreateSmtpClient(); _smtpClient.SendCompleted += SendCompletedCallback; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } _connectionInfo = connectionInfo; _textFormatter = textFormatter; _smtpClient = CreateSmtpClient(); _smtpClient.SendCompleted += SendCompletedCallback; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); _connectionInfo = connectionInfo; _textFormatter = textFormatter; _smtpClient = CreateSmtpClient(); _smtpClient.SendCompleted += SendCompletedCallback; if (_connectionInfo.MaxNumberOfSentMailsPerHour.HasValue) { _lastMailSentTimes = new Queue<DateTime>(_connectionInfo.MaxNumberOfSentMailsPerHour.Value); } }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); _connectionInfo = connectionInfo; _textFormatter = textFormatter; _smtpClient = new SmtpClient(connectionInfo.MailServer) { Credentials = _connectionInfo.NetworkCredentials, Port = _connectionInfo.Port, EnableSsl = _connectionInfo.EnableSsl }; _smtpClient.SendCompleted += SendCompletedCallback; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> /// <param name="subjectLineFormatter">The subject line formatter.</param> /// <exception cref="System.ArgumentNullException">connectionInfo</exception> public EmailSink(EmailConnectionInfo connectionInfo, ITextFormatter textFormatter, ITextFormatter subjectLineFormatter) { if (connectionInfo == null) { throw new ArgumentNullException(nameof(connectionInfo)); } _connectionInfo = connectionInfo; _fromAddress = MimeKit.MailboxAddress.Parse(_connectionInfo.FromEmail); _toAddresses = connectionInfo .ToEmail .Split(",;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(MimeKit.MailboxAddress.Parse) .ToArray(); _textFormatter = textFormatter; _subjectFormatter = subjectLineFormatter; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) { throw new ArgumentNullException(nameof(connectionInfo)); } _connectionInfo = connectionInfo; _fromAddress = MimeKit.MailboxAddress.Parse(_connectionInfo.FromEmail); _toAddresses = connectionInfo .ToEmail .Split(",;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .Select(MimeKit.MailboxAddress.Parse) .ToArray(); _textFormatter = textFormatter; }
/// <summary> /// Construct a sink emailing with the specified details. /// </summary> /// <param name="connectionInfo">Connection information used to construct the SMTP client and mail messages.</param> /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param> public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter) : base(batchSizeLimit, period) { if (connectionInfo == null) { throw new ArgumentNullException("connectionInfo"); } _connectionInfo = connectionInfo; _textFormatter = textFormatter; _smtpClient = new SmtpClient(connectionInfo.MailServer) { Credentials = _connectionInfo.NetworkCredentials, Port = _connectionInfo.Port, EnableSsl = _connectionInfo.EnableSsl }; _smtpClient.SendCompleted += SendCompletedCallback; }
/// <summary> /// Adds a sink that sends log events via email. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="connectionInfo">The connection info used for </param> /// <param name="outputTemplate">A message template describing the format used to write to the sink. /// the default is "{Timestamp} [{Level}] {Message}{NewLine}{Exception}".</param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration Email( this LoggerSinkConfiguration loggerConfiguration, EmailConnectionInfo connectionInfo, string outputTemplate = DefaultOutputTemplate, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, int batchPostingLimit = EmailSink.DefaultBatchPostingLimit, TimeSpan? period = null, IFormatProvider formatProvider = null) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); var defaultedPeriod = period ?? EmailSink.DefaultPeriod; var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider); return loggerConfiguration.Sink( new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, formatter), restrictedToMinimumLevel); }
/// <summary> /// Adds a sink that sends log events via email. /// </summary> /// <param name="loggerConfiguration">The logger configuration.</param> /// <param name="connectionInfo">The connection info used for </param> /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> /// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> /// <param name="period">The time to wait between checking for event batches.</param> /// <param name="textFormatter">ITextFormatter implementation to write log entry to email.</param> /// <param name="mailSubject">The subject used in error mails</param> /// <returns>Logger configuration, allowing configuration to continue.</returns> /// <exception cref="ArgumentNullException">A required parameter is null.</exception> public static LoggerConfiguration Email( this LoggerSinkConfiguration loggerConfiguration, EmailConnectionInfo connectionInfo, ITextFormatter textFormatter, LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum, int batchPostingLimit = EmailSink.DefaultBatchPostingLimit, TimeSpan? period = null, string mailSubject = EmailConnectionInfo.DefaultSubject) { if (connectionInfo == null) throw new ArgumentNullException("connectionInfo"); if (textFormatter == null) throw new ArgumentNullException("textFormatter"); var defaultedPeriod = period ?? EmailSink.DefaultPeriod; return loggerConfiguration.Sink( new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, textFormatter), restrictedToMinimumLevel); }