Search code examples
asp.net-mvchealth-monitoring

How to include POST data in health monitoring email?


I am currently using .NET health monitoring classes to email me when server errors occur. However, I am not getting any information about the POST data nor can I find any way to get the POST data from the raised event.

C#

public class EmailWebEvents : WebEventProvider {

    public override void ProcessEvent(WebBaseEvent raisedEvent) {
        BasicEmail email = new BasicEmail();
        email.To = "[email protected]";
        email.Subject = "Website Error (" + Environment.MachineName + ")";
        email.Body = raisedEvent.Message + "\n\n" +
                     raisedEvent.EventSource.ToString() + "\n\n" +
                     raisedEvent.ToString();
        email.Send();
    }

    public override void Shutdown() {
        // Nothing needed to be done.
    }

    public override void Flush() {
        // Nothing needed to be done.
    }
}

Web Config

<system.web>
    <healthMonitoring enabled="true" heartbeatInterval="0">
        <providers>
            <clear />
            <add name="Custom Email Event Provider" type="MvcRoot.Helpers.EmailWebEvents" />
        </providers>
        <rules>
            <clear />
            <add name="appDevEvents" eventName="All Errors" provider="Custom Email Event Provider" profile="Critical" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom="" />
        </rules>
    </healthMonitoring>
</system.web>

How can I include the POST data in these emails?


Solution

  • Using a decompiler on System.Web.Management.WebBaseEvent. When an event is raised, it makes some direct references to HttpContext.Current without any wrapping or helper classes. Then goes onto call your ProcessEvent method without spooling up another thread or any of the static classes up. So it seems like HttpContext.Current is semi-safe to access from your ProcessEvent method.

    I'd see if HttpContext.Current.Request.Form contains the values you're looking for when you're processing the event. You'll probably want to wrap this in some null checks to ensure the context actually exists before trying to pull the form values.