ASP.NET Health Monitoring 404 Event

2019-05-22 02:36发布

问题:

Does HealthMonitoring have a built-in event that catches 404 errors? I have tried setting up all events (by using webBaseEvent) and I've searched for two days, but I cannot find or trigger an event for a file not found.

I could create my own, but was hoping there was a built in event.

回答1:

No, it doesn't. You'd have to create a custom event (from webrequesterrorevent) to have HM track it for you.

How to: Something like this (from memory) in Application_Error in global.asax -

public void Application_Error()
{
    var exception = Server.GetLastError() as HttpException;
    if (exception != null && exception.GetHttpCode() == 404)
    {
       //custom error
       new Http404Event(this, exception).Raise();
    }
}