Jeremy Davis
Jeremy Davis
Sitecore, C# and web development
Article printed from: https://blog.jermdavis.dev/posts/2015/getting-sitecore-log-messages-by-email

Getting Sitecore log messages by email

Published 16 March 2015
Updated 25 August 2016
Logging Sitecore ~2 min. read

Sitecore manages its logging through the Log4Net framework. Out of the box, this is configured to write log data to disk files for you to review. But sometimes you might want to receive your log messages in different ways. Commonly people do stuff like change this to record messages in databases, but when I was asked how to get exception messages via email in Sitecore 6.6 recently, I failed to find any helpful posts.

So if you want to receive log messages via emails, here's one way of setting it up:

To configure a new mechanism for recording log data you need to add a new <appender/> element to the Log4Net config. This goes inside the <log4net/> element of your web.config file. Note that this isn't part of the core Sitecore configuration, so you can't do this with a configuration patch. XDT is one approach to managing these changes.

To send log messages via email you need to set up an instance of the SMTPAppender class. This starts off with the following settings:

<appender name="EmailAppender" type="log4net.Appender.SMTPAppender, Sitecore.Logging">
	<to value="admin.user@yourdomain.com"/>
	<from value="noreply.sitecore@yourdomain.com"/>
	<subject value="Sitecore Error log from Test Site" />
	<smtpHost value="smtpserver.yourdomain.com" />
	<bufferSize value="64" />
	<layout type="log4net.Layout.PatternLayout">
		<conversionPattern value="%4t %d{ABSOLUTE} %-5p %m%n" />
	</layout>
	<filter type="log4net.Filter.LevelRangeFilter">
		<acceptOnMatch value="true" />
		<levelMin value="WARN" />
		<levelMax value="FATAL" />
	</filter>
</appender>

					

Note that the type attribute here specifies a type inside a Sitecore DLL. This is different from pure Log4Net examples you may find on the internet, since Sitecore has compiled these classes into its own DLLs.

The <to/> and <from/> elements describe the recipient and sender for the email. The <subject/> is whatever you want the title for the email to be.

You have to give Log4Net an SMTP server to send the message through with the <smtpHost/> element. If your server requires custom ports or authentication then there are further elements you can add to configure the security. These extra bits of configuration are described on the Log4Net website's page about SMTPAppender.

The <bufferSize/> element describes the size of the buffer that logging events will be recorded in. If you set it to be 1 or less, events will be emailed as they are generated. Otherwise Log4Net will collect the number you specify before emailing.

The <layout/> element lets you describe a pattern for formatting the log messages in the email. This works in the same way as the patterns used for disk logging.

And finally, the <filter/> element lets you specify rules for what elements should go into the log emails. Here I've specified that warning, error and fatal messages are being delivered. However there are also rules available for matching text. The specific filtering process to use is specified by the type attribute.

Once you've set up the appender, you need to tell Log4Net to use it. To ensure that our appender sees all of the messages that normally go into the Sitecore log, we can add a reference to our appender into the <root/> element that defines the defaults:

<root>
	<priority value="INFO" />
	<appender-ref ref="LogFileAppender" />
	<appender-ref ref="EmailAppender"/>
</root>

					

The first <appender/> is for the Sitecore log, the second is for our emails.

Now, at this point you might find yourself scratching your head about why emails haven't started appearing in your inbox. If your SMTP settings aren't quite right, messages won't get through. Luckily you can get Log4Net to tell you what's going on. You need to add an appSetting for the log4net.Internal.Debug flag and then add a trace listener to record these internal debug messages to disk. You can set this up by adding something like this into your web.config:

<appSettings>
	<add key="log4net.Internal.Debug" value="true" />
</appSettings>

<system.diagnostics>
	<trace autoflush="true">
		<listeners>
			<add name="textWriterTraceListener"  type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\inetpub\wwwroot\TEST\Data\logs\log4net.txt" />
		</listeners>
	</trace>
</system.diagnostics>

					

Check through this log and look for errors and warnings about the SMTPAppender's configuration to see what's not right. Then update your configuration to fix the problem.

Once you've done that, generate some errors on your site, and you should start to see email messages appearing describing those problems. Having requested a page that deliberately throws exceptions, my webmail account gets the following message:

Error Email

Bingo.

↑ Back to top