如何添加ILogger发送邮件服务?
How to add ILogger to send email service?
我有一个 Blazor 项目并使用 MailKit 发送电子邮件。
我想将 Logger 添加到电子邮件服务中。
即Startup.ConfigureServices
//...
services.AddSingleton<IEmailConfiguration>(Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>());
//...
services.AddTransient<IEmailSender, EmailSender>();
//...
即EmailSender
服务:
public class EmailSender : IEmailSender
{
private readonly IEmailConfiguration _emailConfiguration;
//private readonly ILogger<WHATModel> _logger; //<------------?
public EmailSender(IEmailConfiguration emailConfiguration)
{
_emailConfiguration = emailConfiguration;
}
public Task SendEmailAsync(string recipientEmail, string subject, string message)
{
//....
所以,我可以将 EmailConfiguration
传递给服务,但是如何传递 ILogger
?
需要一些模型
private readonly ILogger<WHATModel> _logger; //<------------?
我应该通过什么模型?
模型通常是被注入的类型,并充当识别和分组记录信息的类别。 ILogger<TCategoryName>
When an ILogger
object is created, a category is specified for it. That category is included with each log message created by that instance of ILogger
. The category may be any string, but the convention is to use the class name, such as "TodoApi.Controllers.TodoController".
引用Logging in .NET Core and ASP.NET Core: Log category
将其作为显式依赖包含在构造函数中,以便它也可以被注入
public class EmailSender : IEmailSender {
private readonly IEmailConfiguration emailConfiguration;
private readonly ILogger<EmailSender> logger;
public EmailSender(IEmailConfiguration emailConfiguration, ILogger<EmailSender> logger) {
this.emailConfiguration = emailConfiguration;
this.logger = logger;
}
public Task SendEmailAsync(string recipientEmail, string subject, string message) {
//...
}
//...
}
容器会在解析时解析并注入依赖到发送方
我有一个 Blazor 项目并使用 MailKit 发送电子邮件。
我想将 Logger 添加到电子邮件服务中。
即Startup.ConfigureServices
//...
services.AddSingleton<IEmailConfiguration>(Configuration.GetSection("EmailConfiguration").Get<EmailConfiguration>());
//...
services.AddTransient<IEmailSender, EmailSender>();
//...
即EmailSender
服务:
public class EmailSender : IEmailSender
{
private readonly IEmailConfiguration _emailConfiguration;
//private readonly ILogger<WHATModel> _logger; //<------------?
public EmailSender(IEmailConfiguration emailConfiguration)
{
_emailConfiguration = emailConfiguration;
}
public Task SendEmailAsync(string recipientEmail, string subject, string message)
{
//....
所以,我可以将 EmailConfiguration
传递给服务,但是如何传递 ILogger
?
需要一些模型
private readonly ILogger<WHATModel> _logger; //<------------?
我应该通过什么模型?
模型通常是被注入的类型,并充当识别和分组记录信息的类别。 ILogger<TCategoryName>
When an
ILogger
object is created, a category is specified for it. That category is included with each log message created by that instance ofILogger
. The category may be any string, but the convention is to use the class name, such as "TodoApi.Controllers.TodoController".
引用Logging in .NET Core and ASP.NET Core: Log category
将其作为显式依赖包含在构造函数中,以便它也可以被注入
public class EmailSender : IEmailSender {
private readonly IEmailConfiguration emailConfiguration;
private readonly ILogger<EmailSender> logger;
public EmailSender(IEmailConfiguration emailConfiguration, ILogger<EmailSender> logger) {
this.emailConfiguration = emailConfiguration;
this.logger = logger;
}
public Task SendEmailAsync(string recipientEmail, string subject, string message) {
//...
}
//...
}
容器会在解析时解析并注入依赖到发送方