Udostępnij za pośrednictwem


How to: Send E-mail for Health Monitoring Notifications

You can configure an ASP.NET application to send an e-mail notification when an ASP.NET health-monitoring Web event occurs. To do this, you can configure one of the available e-mail Web-event providers.

The ASP.NET health-monitoring system comes with several event providers that consume health-monitoring Web-event data. Only the EventLogWebEventProvider, SqlWebEventProvider, and WmiWebEventProvider event providers are preconfigured in the root Web.config file. This topic describes how to add configuration settings for the e-mail Web-event providers, and how to enable those providers to listen for certain events.

For a list of Web events that your provider can listen for, see the <eventMappings> element in your root Web.config file, or see the "Web Events" section of ASP.NET Health Monitoring Overview.

Sending E-mail from Your Web Application

The code examples in this topic require your Web application to be configured to send e-mail messages. The following procedure shows you how to configure your application if you already have the Simple Mail Transfer Protocol (SMTP) service installed and configured on your Web server, and have access to an SMTP server that can deliver the e-mail messages. For more information, see How to: Install and Configure SMTP Virtual Servers in IIS 6.0.

To configure your ASP.NET application to send e-mail

  • Open the Web.config file for your application and configure the <mailSettings> element within the <system.net> section. You need to specify a delivery method in the deliveryMethod attribute of the <smtp> element, and the name of the SMTP server in the host attribute of the <network> element. It is also recommended that you set the defaultCredentials attribute to true. (Other attributes are available and are documented in the element topics.)

    Your configuration settings might look like the following example.

    <configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">
      <system.net>
        <mailSettings>
          <smtp deliveryMethod="Network">
            <network 
              defaultCredentials="true" 
              host="smtpservername" 
              />
          </smtp>
        </mailSettings>
      </system.net>
      <!-- Other configuration settings. -->
    </configuration>
    

Configuring the Mail Providers

To configure the SimpleMailWebEventProvider Web event provider

  1. Open the Web.config file for your application and add a new <providers> element within the <healthMonitoring> element of the <system.web> section. This new providers element is where you will configure the SimpleMailWebEventProvider Web event provider. This provider is included with the ASP.NET health-monitoring system, but it is not preconfigured for you in the root Web.config file.

    Your configuration settings might look like the following example.

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <providers>
            <add 
              name="exampleMailWebEventProvider" 
              type="System.Web.Management.SimpleMailWebEventProvider"
              to="someone@contoso.com"
              from="someone@contoso.com"
              buffer="false"
              subjectPrefix="WebEvent has fired"
              />
          </providers>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    For the purposes of this procedure, disable event buffering by setting the buffer attribute to false. The name attribute is arbitrary, and it is used to identify the provider in the next step. Change the to and from attributes to your own e-mail address to complete the testing steps later in this procedure.

  2. Add a new <rules> element within the <healthMonitoring> element. This new rules element configures the e-mail providers to listen for all Web events.

    Your configuration settings might look like the following example.

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <!-- <providers> element from the previous step -->
          <rules>
            <add 
              name="Testing Mail Event Providers" 
              eventName="All Events" 
              provider="exampleMailWebEventProvider"
              profile="Default" 
              minInstances="1" 
              maxLimit="Infinite" 
              minInterval="00:01:00"
              custom="" 
              />
          </rules>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    The provider attribute is the same as the name attribute of your providers element from the previous step. The eventName attribute of this rules element is the same as the name attribute of the preconfigured eventMappings element in the root Web.config file. This configuration will send an e-mail message for every Web event that occurs in your ASP.NET application. This includes, but is not limited to, application starting and stopping, configuration changes, and requests.

    For the purposes of this procedure, define your rules element and your provider element from the previous step in the same configuration file. Alternatively, your provider element can exist in a configuration file that is higher in the chain of configuration files, toward the root Web.config file.

  3. Test your configuration by requesting a page from your application. It is possible that you have already received an e-mail message if you changed configuration settings, which would raise an application restart event. The e-mail you receive might have a subject line and message body similar to the following example message.

    Subject: Event Notification 1, part 1: WebEvent has fired <event>
    Body: 
    

Application Information --------------- Application domain: /LM/w3svc/1/ROOT/ Trust level: Full Application Virtual Path: / Application Path: Machine name:

Events --------------- ---------------

To configure the TemplatedMailWebEventProvider Web event provider

  1. Open the Web.config file for your application and add a new <providers> element within the <healthMonitoring> element of the <system.web> section. This new providers element is where you will configure the TemplatedMailWebEventProvider Web event provider. This provider is included with the ASP.NET health-monitoring system, but it is not pre-configured for you in the root Web.config file.

    Your configuration settings might look like the following example.

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <providers>
            <add 
              name="exampleTemplatedMailWebEventProvider" 
              type="System.Web.Management.TemplatedMailWebEventProvider"
              template="template.aspx" 
              detailedTemplateErrors="true"
              to="someone@contoso.com"
              from="someone@contoso.com"
              buffer="true"
              bufferMode="Notification"
              maxMessagesPerNotification="1"
              maxEventsPerMessage="1" 
              />
          </providers>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    For the purposes of this procedure, enable event buffering by setting the buffer attribute to false. The bufferMode attribute value, Notification, is predefined in the root Web.config file. For more information about buffering, see Extending ASP.NET Health Monitoring Events.

    The name attribute is arbitrary, and is used to identify the provider in the next step. Change the to and from attributes to your own e-mail address to complete the testing steps later in this procedure.

  2. Add a new <rules> element within the <healthMonitoring> element. This new rules element configures the e-mail providers to listen for all Web events.

    Your configuration settings might look like the following example.

    <configuration>
      <system.web>
        <healthMonitoring enabled="true" heartbeatInterval="0">
          <!-- <providers> element from the previous step -->
          <rules>
            <add 
              name="Testing Templated Mail Event Providers" 
              eventName="Request Processing Events" 
              provider="exampleTemplatedMailWebEventProvider"
              profile="Default" 
              minInstances="1" 
              maxLimit="Infinite" 
              minInterval="00:00:00"
              custom="" 
              />
          </rules>
        </healthMonitoring>
      </system.web>
      <!-- Other configuration settings. -->
    </configuration>
    

    The provider attribute is the same as the name attribute of your providers element from the previous step. The eventName attribute of this rules attribute is the same as the name attribute of the preconfigured eventMappings element in the root Web.config file. This configuration will send an e-mail message for every request made to a page or other resource in your ASP.NET application.

  3. Create an e-mail template file named Template.aspx and place it in the root of your application. This file name is the value of the template attribute of the providers element. When a request event occurs in your application, the template file is populated with data using string expressions and sent as the notification e-mail message. For information about using expressions, see ASP.NET Expressions Overview.

    Your template file might look like the following example.

    <%@ Page Language="cs" %>
    <%@ Import Namespace="System.Web.Management" %>
    
    <script runat="server">
    
    void Page_Load() 
    {
        MailEventNotificationInfo info = TemplatedMailWebEventProvider.CurrentNotification;
        Label0.Text = "EventsDiscardedByBuffer: " + info.EventsDiscardedByBuffer + '\n';
        Label1.Text = "EventsInBuffer: " + info.EventsInBuffer + '\n';
        Label2.Text = "NotificationSequence: " + info.NotificationSequence + '\n';
        Label3.Text = "NotificationType: " + info.NotificationType + '\n';
        Label4.Text = "EventsInNotification: " + info.EventsInNotification + '\n';
        Label5.Text = "EventsRemaining: " + info.EventsRemaining + '\n';
        Label6.Text = "MessagesInNotification: " + info.MessagesInNotification + '\n';
        Label7.Text = "eventsDiscardedDueToMessageLimit: " + info.EventsDiscardedDueToMessageLimit + '\n';
        Label8.Text = "messageSequence: " + info.MessageSequence + '\n';
        Label9.Text = "LastNotificationUtc: " + info.LastNotificationUtc.ToLocalTime().ToString() + '\n';
    
        EventList.DataSource = info.Events;
        EventList.DataBind();
    }
    </script>
    
    <asp:Label runat="server" id="Label0" /><p />
    <asp:Label runat="server" id="Label1" /><p />
    <asp:Label runat="server" id="Label2" /><p />
    <asp:Label runat="server" id="Label3" /><p />
    <asp:Label runat="server" id="Label4" /><p />
    <asp:Label runat="server" id="Label5" /><p />
    <asp:Label runat="server" id="Label6" /><p />
    <asp:Label runat="server" id="Label7" /><p />
    <asp:Label runat="server" id="Label8" /><p />
    <asp:Label runat="server" id="Label9" /><p />
    
    <asp:DataList id="EventList" runat="server">
        <ItemTemplate>
          Event Received 
          Sequence: <%# DataBinder.Eval(Container.DataItem, "EventSequence") %><br>
          Details: <%# Container.DataItem.ToString() %>
        </ItemTemplate>
    </asp:DataList>
    
  4. Test your configuration by requesting a page from your application. You should quickly receive an e-mail message that includes a subject line and message body similar to the following example message.

    Subject: Event Notification 1, part 1: <event>
    Body: 
    EventsDiscardedByBuffer: 0 
    EventsInBuffer: 0 
    NotificationSequence: 1 
    NotificationType: Flush 
    EventsInNotification: 1 
    EventsRemaining: 0 
    MessagesInNotification: 1 
    eventsDiscardedDueToMessageLimit: 0 
    messageSequence: 1 
    LastNotificationUtc: 1/1/0001 12:00:00 AM 
    Event Received Sequence: 1
    Details: 
    <event information>
    

See Also

Concepts

Extending ASP.NET Health Monitoring Events

Web events

Reference

healthMonitoring Element (ASP.NET Settings Schema)

eventMappings Element for healthMonitoring (ASP.NET Settings Schema)

providers Element for healthMonitoring (ASP.NET Settings Schema)

rules Element for healthMonitoring (ASP.NET Settings Schema)

<smtp> Element (Network Settings)

Other Resources

ASP.NET Health Monitoring Overview