Implementing HTML Formatted Emails in the Enterprise Library Logging Block
The Microsoft Patterns & Practices Enterprise Library contains a number of useful applications blocks for simplifying things like DataAcces, Logging & Exception Handling in your .NET Applications. Recently we had a requirement to add HTML based formatting to the Email TraceListener in the Logging Application Block, something that's unfortunately missing from the base functionality. Thankfully, Enterprise Library is an open source code plex project so implementing a custom solution is a relatively trivial task. The email tracelistener functionality is contained in 3 main files: EmailTraceListener - The actual listener which you add to your configuration EmailTraceListenerData - The object representing the configuration settings EmailMessage - The wrapper object around a LogMessage which gets sent via email. Unfortunately because of the the way these classes are implemented in the EnterpriseLibrary Logging Block, they are not easily extended due to dependencies on Private Variables and Internal classes in the EnterpriseLibaray Logging Assembly so they need to be fully implemented in your own solution.
Implementing a Solution
Step 1 was to take a copy of these three files and place them in my own Library Solution. I prefixed the name of each of them with Html; HtmlEmailTraceListener, HtmlEmailTraceListenerData and HtmlEmailMessage. Other code needed to be cleaned up including removing some dependencies on the internal ResourceDependency attributes used to decorate properties within the class & tidying up the Xml Documentation Comments. The main change was then to enable the IsBodyHtml flag on the mail message itself. This was done in the CreateMailMessage method of the HtmlEmailMessage
protected MailMessage CreateMailMessage()
{
string header = GenerateSubjectPrefix(configurationData.SubjectLineStarter);
string footer = GenerateSubjectSuffix(configurationData.SubjectLineEnder);
string sendToSmtpSubject = header + logEntry.Severity.ToString() + footer;
MailMessage message = new MailMessage();
string[] toAddresses = configurationData.ToAddress.Split(';');
foreach (string toAddress in toAddresses)
{
message.To.Add(new MailAddress(toAddress));
}
message.From = new MailAddress(configurationData.FromAddress);
message.Body = (formatter != null) ? formatter.Format(logEntry) : logEntry.Message;
message.Subject = sendToSmtpSubject;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
return message;
}
Using your new solution
Once implemented it's simply a matter of reconfiguring your app/web.config logging sections to use the new types you've created instead of the original Enterprise Library types. You need to change the type and listenerDataType properties of your Email Listener in the ≷listeners@gt; section of your config.
<listeners>
<!-- Please update the following Settings: toAddress, subjectLineStarter, subjectLineEnder-->
<add name="EmailLog"
toAddress="toAddress@example.com"
subjectLineStarter="Test Console - "
subjectLineEnder=" Alert"
filter="Verbose"
fromAddress="fromAddress@example.com"
formatter="EmailFormatter"
smtpServer="smtp.gmail.com"
smtpPort="587"
authenticationMode="UserNameAndPassword"
useSSL="true"
userName="fromAddress@example.com"
password="Password"
type="YourLibrary.YourNamespace.HtmlEmailTraceListener, YourLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0000000000000000"
listenerDataType="YourLibrary.YourNamespace.HtmlEmailTraceListenerData, YourLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0000000000000000"
traceOutputOptions="Callstack" />
</listeners>
You'll also need to ensure that you've escaped your Html formatted textFormatter template in the formatters section of your code. i.e. replacing <html> with <html>
<formatters>
<add name="EmailFormatter"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="
<html>
<body>
<table border="1" style="border: solid 1px #000000; border-collapse:collapse;">
<tr><td><b>Message</b></td><td><b>{message}</b></td></tr>
<tr><td>Local TimeStamp</td><td>{timestamp(local)}</td></tr>
<tr><td>Timestamp</td><td>{timestamp}</td></tr>
<tr><td>Title</td><td>{title}</td></tr>
<tr><td>Severity</td><td>{severity}</td></tr>
<tr><td>Category</td><td>{category}</td></tr>
<tr><td>Priority</td><td>{priority}</td></tr>
<tr><td>EventId</td><td>{eventid}</td></tr>
<tr><td>Local Machine</td><td>{localMachine}</td></tr>
<tr><td>AppDomain</td><td>{appDomain}</td></tr>
<tr><td>LocalDomain</td><td>{localAppDomain}</td></tr>
<tr><td>Local Process Name</td><td>{localProcessName}</td></tr>
<tr><td>Local Process</td><td>{localProcessId}</td></tr>
<tr><td>Win32ThreadId</td><td>{win32ThreadId}</td></tr>
<tr><td>ThreadName</td><td>{threadName}</td></tr>
<tr><td>Extended Properties</td><td>
<table border="1" style="border: solid 1px #000000; border-collapse:collapse;">
{dictionary(<tr><td>{key}</td><td>{value}</td></tr>)}
</table></td></tr>
</table>
</body>
</html>" />
</formatters>
All done. Now you can happily send email log messages in HTML format via your Application Logging calls.
~Eoin Campbell