Skip to content

Commit 6468b87

Browse files
committed
Exception handling in email sender
1 parent 83e48d7 commit 6468b87

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

LeaveManagement.Application/Repositories - Services/LeaveRequestRepository.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ await emailSender.SendEmailAsync(user.Email, "Leave Request Submitted", $"Your l
112112
}
113113
catch (Exception ex)
114114
{
115-
logger.LogInformation("Error with email sender", ex);
115+
logger.LogInformation("Error with email sender in Create LRRepo", ex);
116116

117117
}
118118

LeaveManagement.Web/Services/EmailSender.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Identity.UI.Services;
2+
using Microsoft.Extensions.Logging.Abstractions;
23
using System.Net.Mail;
34

45
namespace LeaveManagement.Web.Services
@@ -10,12 +11,13 @@ public class EmailSender : IEmailSender
1011
private string fromEmailAddress;
1112
private readonly ILogger<EmailSender> logger;
1213

13-
public EmailSender(string smtpServer, int smtpPort, string fromEmailAddress, ILogger<EmailSender> logger)
14+
public EmailSender(string smtpServer, int smtpPort, string fromEmailAddress,
15+
ILogger<EmailSender> logger)
1416
{
1517
this.smtpServer = smtpServer;
1618
this.smtpPort = smtpPort;
1719
this.fromEmailAddress = fromEmailAddress;
18-
this.logger = logger;
20+
this.logger = logger ?? NullLogger<EmailSender>.Instance;
1921
}
2022

2123
public Task SendEmailAsync(string email, string subject, string htmlMessage)
@@ -39,7 +41,7 @@ public Task SendEmailAsync(string email, string subject, string htmlMessage)
3941
}
4042
} catch (Exception ex)
4143
{
42-
logger.LogInformation("Error sending email", ex);
44+
logger.LogInformation("Error sending emailim Email Sender", ex);
4345
}
4446

4547
return Task.CompletedTask;

0 commit comments

Comments
 (0)