Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Debug Logs when Sending Emails #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions LeaderboardBackend/Services/Impl/AccountRecoveryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ public class AccountRecoveryService : IAccountRecoveryService
private readonly IEmailSender _emailSender;
private readonly IClock _clock;
private readonly AppConfig _appConfig;
private readonly ILogger _logger;

public AccountRecoveryService(
ApplicationContext applicationContext,
IEmailSender emailSender,
IClock clock,
IOptions<AppConfig> appConfig
IOptions<AppConfig> appConfig,
ILogger<AccountRecoveryService> logger
)
{
_applicationContext = applicationContext;
_emailSender = emailSender;
_clock = clock;
_appConfig = appConfig.Value;
_logger = logger;
}

public async Task<CreateRecoveryResult> CreateRecoveryAndSendEmail(User user)
Expand Down Expand Up @@ -55,8 +58,9 @@ await _emailSender.EnqueueEmailAsync(
GenerateAccountRecoveryEmailBody(user, recovery)
);
}
catch
catch (Exception e)
{
_logger.LogWarning("Failed to send recovery email: {err}", e.Message);
return new EmailFailed();
}

Expand Down
25 changes: 22 additions & 3 deletions LeaderboardBackend/Services/Impl/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,34 @@ private async Task SendEmail(MimeMessage message)

if (!_smtpClient.IsConnected)
{
await _smtpClient.ConnectAsync(_config.Smtp!.Host, _config.Smtp.Port, _config.Smtp.UseSsl);
_logger.LogInformation("Connecting to client");
try
{
await _smtpClient.ConnectAsync(_config.Smtp!.Host, _config.Smtp.Port, _config.Smtp.UseSsl);
_logger.LogError("Client connected");
}
catch (Exception e)
{
_logger.LogError("Connecting failed: {message}", e.Message);
}
}

if ((_config.Smtp!.Username is not null || _config.Smtp.Password is not null)
&& !_smtpClient.IsAuthenticated)
{
await _smtpClient.AuthenticateAsync(_config.Smtp.Username, _config.Smtp.Password);
_logger.LogInformation("Logging into client");
try
{
await _smtpClient.AuthenticateAsync(_config.Smtp.Username, _config.Smtp.Password);
}
catch (Exception e)
{
_logger.LogError("Auth failed: {message}", e.Message);
}
}

await _smtpClient.SendAsync(message);
_logger.LogInformation("Calling SendAsync");
string res = await _smtpClient.SendAsync(message);
_logger.LogInformation("SendAsync done: {res}", res);
}
}