Implementing email logging in Azure Blob Storage
Most of the business applications for automated emails are likely to involve sending emails containing various notifications and alerts to the end user. At times, it is not uncommon for users to not receive any emails, even though we, as developers, don't see any error in the application while sending such notification alerts.
There might be multiple reasons why such users might not have received the email. Each of the email service providers has different spam filters that can block the emails from the end user's inbox. As these emails may have important information to convey, it makes sense to store the email content of all the emails that are sent to the end users, so that we can retrieve the data at a later stage for troubleshooting any unforeseen issues.
In this recipe, you will learn how to create a new email log file with the .log extension for each new registration. This log file can be used as redundancy for the data stored in Table storage. You will also learn how to store email log files as a blob in a storage container, alongside the data entered by the end user during registration.
How to do it...
Perform the following steps:
- Navigate to the Integrate tab of the SendNotifications function, click on New Output, and choose Azure Blob Storage. If prompted, you will have to install Storage Extensions, so please install the extensions to continue forward.
- Provide the requisite parameters in the Azure Blob Storage output section, as shown in Figure 2.19. Note the .log extension in the Path field:
Figure 2.19: Adding details in the Azure Blob Storage output
- Navigate to the code editor of the run.csx file of the SendNotifications function and make the following changes:
Add a new parameter, outputBlob, of the TextWriter type to the Run method.
Add a new string variable named emailContent. This variable is used to frame the content of the email. We will also use the same variable to create the log file content that is finally stored in the blob.
Frame the email content by appending the required static text and the input parameters received in the request body, as follows:
public static void Run(string myQueueItem,out SendGridMessage message, TextWriter outputBlob, ILogger log)
....
....
string FirstName=null, LastName=null, Email = null;
string emailContent;
....
....
emailContent = "Thank you <b>" + FirstName + " " + LastName +"</b> for your registration.<br><br>" + "Below are the details that you have provided
us<br> <br>"+ "<b>First name:</b> " +
FirstName + "<br>" + "<b>Last name:</b> " +
LastName + "<br>" + "<b>Email Address:</b> " +
inputJson.Email + "<br><br> <br>" + "Best
Regards," + "<br>" + "Website Team";
message.AddContent(new Content("text/html",emailContent));
outputBlob.WriteLine(emailContent);
- In the RegisterUser function, run a test using the same request payload that we used in the previous recipe.
- After running the test, the log file will be created in the container named userregistrationemaillogs:
Figure 2.20: Displaying the log file created in userregistrationemaillogs
How it works…
We have created new Azure Blob Storage output bindings. As soon as a new request is received, the email content is created and written to a new .log file that is stored as a blob in the container specified in the Path field of the output bindings.