Skip to content

Extension destination for TacitusLogger that sends logs as emails using SMTP protocol.

License

Notifications You must be signed in to change notification settings

hanlarmammadov/TacitusLogger.Destinations.Email

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TacitusLogger.Destinations.Email

Extension destination for TacitusLogger that sends logs as emails using SMTP protocol.

Dependencies:

  • NET Standard >= 1.3
  • TacitusLogger >= 0.2.0
  • jstedfast/MailKit >= 2.0.0

Attention: TacitusLogger.Destinations.Email is currently in Alpha phase. This means you should not use it in any production code.

Installation

The NuGet package:

PM> Install-Package TacitusLogger.Destinations.Email

Examples

Adding email destination with minimal configuration

Using builders:

var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .Add()
                          .BuildLogger();

Directly:

MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient, fromAddress, "recipient@example.com");

Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With several recipients

Using builders:

var recipients = new string[] 
{
    "recipient@example.com",
    "recipient@example.com",
    "recipient@example.com"
};
var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients(recipients)
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .Add()
                          .BuildLogger(); 

Directly:

var recipients = new string[]
{
    "recipient@example.com",
    "recipient@example.com",
    "recipient@example.com"
};
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient, fromAddress, recipients);

Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With recipients function

Using builders:

LogModelFunc<ICollection<string>> recipientsFunc = (logModel) =>
{
    if (logModel.LogTypeIsIn(LogType.Error, LogType.Failure, LogType.Critical))
        return new string[] { "recipient1@example.com" };
    else
        return new string[] { "recipient2@example.com" };
};
var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients(recipientsFunc)
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .Add()
                          .BuildLogger();

Directly:

FactoryMethodRecipientProvider factoryMethodRecipientProvider = new FactoryMethodRecipientProvider((logModel) =>
{
    if (logModel.LogTypeIsIn(LogType.Error, LogType.Failure, LogType.Critical))
        return new string[] { "recipient1@example.com" };
    else
        return new string[] { "recipient2@example.com" };
});
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient, fromAddress, factoryMethodRecipientProvider);

Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With custom recipient provider

Using builders:

IRecipientProvider customRecipientProvider = new Mock<IRecipientProvider>().Object;
var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients(customRecipientProvider)
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .Add()
                          .BuildLogger();

Directly:

IRecipientProvider customRecipientProvider = new Mock<IRecipientProvider>().Object;
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient, fromAddress, customRecipientProvider);

Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With mailbox address

Using builders:

MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, fromAddress)
                              .Add()
                          .BuildLogger();

With custom subject body and attachment templates

Using builders:

var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .WithSubject("Notification from logger $Source: $LogType - $Description")
                              .WithBody(customBodyTemplate)
                              .WithAttachment(customAttachmentTemplate)
                              .Add()
                          .BuildLogger();

Directly:

EmailListRecipientProvider emailListRecipientProvider = new EmailListRecipientProvider("recipient@example.com");
SimpleTemplateLogSerializer subjectTextSerializer = new SimpleTemplateLogSerializer("Notification from logger $Source: $LogType - $Description");
ExtendedTemplateLogSerializer bodyLogSerializer = new ExtendedTemplateLogSerializer(customBodyTemplate);
ExtendedTemplateLogSerializer attachmentLogSerializer = new ExtendedTemplateLogSerializer(customAttachmentTemplate);
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");

EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient,
                                                            fromAddress,
                                                            emailListRecipientProvider,
                                                            subjectTextSerializer,
                                                            bodyLogSerializer,
                                                            attachmentLogSerializer);
Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With custom body and attachment templates and json serializer settings

Using builders:

var jsonSerializerSettings = new JsonSerializerSettings();
var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .WithSubject("Notification from logger $Source: $LogType - $Description")
                              .WithBody(customBodyTemplate, jsonSerializerSettings)
                              .WithAttachment(customAttachmentTemplate, jsonSerializerSettings)
                              .Add()
                          .BuildLogger();

Directly:

var jsonSerializerSettings = new JsonSerializerSettings();
EmailListRecipientProvider emailListRecipientProvider = new EmailListRecipientProvider("recipient@example.com");
SimpleTemplateLogSerializer subjectTextSerializer = new SimpleTemplateLogSerializer("Notification from logger $Source: $LogType - $Description");
ExtendedTemplateLogSerializer bodyLogSerializer = new ExtendedTemplateLogSerializer(customBodyTemplate, jsonSerializerSettings);
ExtendedTemplateLogSerializer attachmentLogSerializer = new ExtendedTemplateLogSerializer(customAttachmentTemplate, jsonSerializerSettings);
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");

EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient,
                                                            fromAddress,
                                                            emailListRecipientProvider,
                                                            subjectTextSerializer,
                                                            bodyLogSerializer,
                                                            attachmentLogSerializer);
Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With custom subject body and attachment serializers

Using builders:

ILogSerializer customSubjectSerializer = new Mock<ILogSerializer>().Object;
ILogSerializer customBodySerializer = new Mock<ILogSerializer>().Object;
ILogSerializer customAttachmentSerializer = new Mock<ILogSerializer>().Object;

var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .WithSubject(customSubjectSerializer)
                              .WithBody(customBodySerializer)
                              .WithAttachment(customAttachmentSerializer)
                              .Add()
                          .BuildLogger();

Directly:

ILogSerializer customSubjectSerializer = new Mock<ILogSerializer>().Object;
ILogSerializer customBodySerializer = new Mock<ILogSerializer>().Object;
ILogSerializer customAttachmentSerializer = new Mock<ILogSerializer>().Object;
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailListRecipientProvider emailListRecipientProvider = new EmailListRecipientProvider("recipient@example.com");

EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient,
                                                            fromAddress,
                                                            emailListRecipientProvider,
                                                            customSubjectSerializer,
                                                            customBodySerializer,
                                                            customAttachmentSerializer);
Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

With custom subject text function

Using builders:

var logger = LoggerBuilder.Logger()
                          .ForAllLogs()
                          .Email()
                              .WithRecipients("recipient@example.com")
                              .WithSmtpClient(mailKitSmtpClient, "sender@example.com")
                              .WithSubject(m => $"Notification from logger {m.Source}: {m.LogType} - {m.Description}")
                              .Add()
                          .BuildLogger();

Directly:

GeneratorFunctionLogSerializer subjectTextFunction = new GeneratorFunctionLogSerializer(m =>
{
    return $"Notification from logger {m.Source}: {m.LogType} - {m.Description}";
}); 
MailboxAddress fromAddress = new MailboxAddress("sender", "sender@example.com");
EmailListRecipientProvider emailListRecipientProvider = new EmailListRecipientProvider("recipient@example.com");

EmailDestination emailDestination = new EmailDestination(mailKitSmtpClient,
                                                            fromAddress,
                                                            emailListRecipientProvider,
                                                            subjectTextFunction);
Logger logger = new Logger();
logger.AddLogDestinations(emailDestination);

License

APACHE LICENSE 2.0

See also

TacitusLogger:

Destinations:

Dependency injection:

  • TacitusLogger.DI.Ninject - Extension for Ninject dependency injection container that helps to configure and add TacitusLogger as a singleton.
  • TacitusLogger.DI.Autofac - Extension for Autofac dependency injection container that helps to configure and add TacitusLogger as a singleton.
  • TacitusLogger.DI.MicrosoftDI - Extension for Microsoft dependency injection container that helps to configure and add TacitusLogger as a singleton.

Log contributors: