Today we look at a very interesting and useful thing that you can embed in just a minute in any ASP.NET application by changing one file.
To begin with, I will try to define the Health Monitoring System (hereinafter HMS).
HMS is a module that will monitor your site and respond to certain events. In this article we will deal with the implementation of a specific case: we will receive letters with various information in the mail when critical errors occur.
Why is this necessary? I will give a specific example. At the moment, the project that I do at work will be transferred to the customer’s server, and since the contract provides for a site support period, I needed a tool to track errors that would necessarily arise during the use of the site by users. So, HMS is ideally suited for this: for my part, almost no effort is required to implement and, at the same time, I get an ideal-suitable result. If you wish, you can come up with many more examples, but I think you understood the point and my situation is close to many.
So let's get started. As I said, we need to change only one file. Everyone probably already guessed without me that it would be
web.config .
Open it, look for the
<system.web> section and add the following to it:
< healthMonitoring enabled = "true" >
</ healthMonitoring >
Well, actually, we already included HMS. It remains to configure it. All subsequent code should be
inserted between the
healthMonitoring tags.
Let's follow the rules of good tone and give the event a human name:
< eventMappings >
< add
name = "MyEvent"
type = "System.Web.Management.WebErrorEvent"
startEventCode = "0"
endEventCode = "10,000" />
</ eventMappings >
Now we can use the name MyEvent to represent all events of type
System.Web.Management.WebErrorEvent , which represents errors. ASP.NET assigns a unique number to each event, therefore we indicate the range of these numbers (when it reaches 10,000 it will start again from 0). Now we will configure the buffer so that our system will not flood us:
< bufferModes >
< add
name = "MyBuffer"
maxBufferSize = "100"
maxFlushSize = "50"
maxBufferThreads = "1"
regularFlushInterval = "00:10:00"
urgentFlushInterval = "00:01:00"
urgentFlushThreshold = "10" />
</ bufferModes >
The essence of all this can be summarized as follows: HMS will write us letters every ten minutes, if at least one event has accumulated, or immediately after the accumulation of 50 events. You can customize to taste. Another important step is the choice and configuration of the provider. We need a
System.Web.Management.SimpleMailWebEventProvider :
< providers >
< add name = "MyProvider"
type = "System.Web.Management.SimpleMailWebEventProvider"
from = "sender@site.com"
to = "reciever@site.com"
bodyHeader = "Warning!"
bodyFooter = "Please investigate ASAP."
subjectPrefix = "Action required."
buffer = "true"
bufferMode = "MyBuffer"
maxEventLength = "4096"
maxMessagesPerNotification = "1" />
</ providers >
He has a lot of settings and you can see them completely
here , I gave a shortened version. The final step is to add a rule to link everything together:
< rules >
< add
name = "MyRule"
eventName = "MyEvent"
provider = "MyProvider" />
</ rules >
That's all. Our system is already working (if it does not work, you can try to restart the application in IIS). As soon as your site throws an exception, you will receive an email with a bunch of information: about the application, request, exception, user and his IP address, and even a complete trace of the stacks. What else is needed to determine the cause of the error and eliminate it?
PS Naturally, to use SimpleMailWebEventProvider, you need the correct smtp definition in web.config. You can read about it
here . There are a great many types of events and providers that can be used in HMS, everything is written
here .