- Azure Serverless Computing Cookbook
- Praveen Kumar Sreeram
- 560字
- 2021-03-26 16:09:02
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.
- 零起點學Linux系統管理
- Containerization with LXC
- 精解Windows 8
- 嵌入式Linux系統開發:基于Yocto Project
- 新手易學:系統安裝與重裝
- 異質結原理與器件
- 嵌入式操作系統(Linux篇)(微課版)
- 移動應用UI設計模式(第2版)
- Microsoft Operations Management Suite Cookbook
- 從實踐中學習Kali Linux無線網絡滲透測試
- 云原生落地:產品、架構與商業模式
- OpenSolaris設備驅動原理與開發
- Building Telephony Systems With Asterisk
- Linux內核API完全參考手冊(第2版)
- Android應用性能優化最佳實踐