Hi, Does anyone have sample code, running under IIS7, for sending out emails every time an exception is generated in the application? In previous versions of IIS, all you had to do was call Server.GetLastError to get exception details. In my IIS7 custom error page, Server.GetLastError is always null. Here's what I have in my Web.config: HTML: <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" defaultResponseMode="ExecuteURL" defaultPath="/Error.aspx"> <remove statusCode="500" /> <error statusCode="500" path="/Error.aspx" responseMode="ExecuteURL" /> </httpErrors> In my file Error.aspx, I do this: Code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim oEx As Exception = Server.GetLastError() End Sub But oEx is always equal to Nothing (null). How do I get exception details in a custom error page running under IIS7?
If you want a completely automated approach, then it is simply web.config setup - there is no actual code required. Is this what you're after? :- Code: <healthMonitoring enabled="true"> <providers> <add name="AllErrorsMonitoringEmailProvider" type="System.Web.Management.SimpleMailWebEventProvider" from="[email protected]" to="[email protected]" subjectPrefix="A Monitoring Error " buffer="true" bufferMode="Notification"/> <add name="InfrastructureErrorsMonitoringEmailProvider" type="System.Web.Management.SimpleMailWebEventProvider" from="[email protected]" to="[email protected]" subjectPrefix="A Monitoring Infrastructure Error " buffer="true" bufferMode="Notification"/> <add name="FailureAuditsMonitoringEmailProvider" type="System.Web.Management.SimpleMailWebEventProvider" from="[email protected]" to="[email protected]" subjectPrefix="A Monitoring Audit Failure " buffer="true" bufferMode="Notification"/> <add name="ApplicationEventsMonitoringEmailProvider" type="System.Web.Management.SimpleMailWebEventProvider" from="[email protected]" to="[email protected]" subjectPrefix="A Monitoring Application Event " buffer="true" bufferMode="Notification"/> </providers> <rules> <add provider="AllErrorsMonitoringEmailProvider" name="All Errors" eventName="All Errors"/> <add provider="InfrastructureErrorsMonitoringEmailProvider" name="Infrastructure Errors" eventName="Infrastructure Errors"/> <add provider="FailureAuditsMonitoringEmailProvider" name="Failure Audits" eventName="Failure Audits"/> <!-- <add provider="ApplicationEventsMonitoringEmailProvider" name="Application Lifetime Events" eventName="Application Lifetime Events"/>--> </rules> </healthMonitoring> ... <system.net> <mailSettings> <smtp from="[email protected]" deliveryMethod="Network"> <network host="smtp.youraccount.co.uk" password="yourpassword" userName="[email protected]"/> </smtp> </mailSettings> </system.net>
I'm not familiar with the healthMonitoring config section. Is that the only way to generate error emails in IIS7? This was so simple in previous versions of IIS. All you had to do was modify Global.asax and add code like this: Code: Protected Sub Application_Error(ByVal sender As Object, ByVal e As System.EventArgs) Dim oEx as Exception = Server.GetLastError() ' -- write exception properties to HTML string ' -- send email with HTML string as the body End Sub Unfortunately, this doesn't work in IIS7, because of the way page requests are handled. An exception can be generated in an external module (e.g. StaticFileModule) and the code in Global.asax will never be reached. There must be a simple way to send error emails in IIS7 the same way I could send them in IIS6. Anyone?
Thanks CrystalCMS for this tip, I successfully configure it on my server.. would have taken me days to figure it out if not because of your sample.