我有以下方法发送电子邮件和方法来包装异常:
public void SendEmail(SendEmailRequest emailRequest)
{
...
catch (SmtpFailedRecipientsException e)
{
var failedRecipients = e.InnerExceptions.Select(x => x.FailedRecipient);
LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailDeliveryFailed, emailRequest.Subject, serverSettings, failedRecipients);
}
catch (SmtpFailedRecipientException e)
{
LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailDeliveryFailed, emailRequest.Subject, serverSettings, new[] { e.FailedRecipient });
}
catch (SmtpException e)
{
LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailSendFailedInvalidSettings, emailRequest.Subject, serverSettings);
}
catch (Exception e)
{
LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailSendFailedGeneralIssues, emailRequest.Subject, serverSettings);
}
...
}
private static void LogAndReThrowWithValidMessage(Exception e, string message, string logSubject, SmtpServerSettings logSettings, IEnumerable<string> failedRecipients = null)
{
_logger.ErrorException(e,
"Exception while trying to send message with subject '{0}' using SMTP server {1}:{2}",
logSubject,
logSettings.SmtpServerName,
logSettings.SmtpPort);
var sendingFailedException = new EmailSendingFailedException(message, e);
var smtpException = e as SmtpException;
if (smtpException != null)
{
var isGmailServerName = logSettings.SmtpServerName.Contains("gmail.com");
if (isGmailServerName && smtpException.InnerException.Message.Contains("net_io_connectionclosed"))
{
sendingFailedException = new EmailSendingFailedException(EmailsLocalization.EmailSendFailedWrongPortNumber, e);
}
// Wrapping authentication exceptions
if (smtpException.StatusCode == SmtpStatusCode.MustIssueStartTlsFirst ||
smtpException.Message.Contains("AUTH"))
{
sendingFailedException = isGmailServerName ?
new EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblemForGmail, e) :
new EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblem, e);
}
// According to Yahoo's spam policy
if (smtpException.StatusCode == SmtpStatusCode.MailboxNameNotAllowed)
{
sendingFailedException = new EmailSendingFailedException(EmailsLocalization.EmailSendFailedWrongFromAddress, e);
}
}
if (failedRecipients != null)
{
sendingFailedException.FailedRecipients = failedRecipients;
}
throw sendingFailedException;
}
如何为SmtpException
创建实现此逻辑的函数的重载版本,然后调用原始方法来记录ant wrap。此外,现在我在日志中缺少我的改进信息,如何解决这个问题?