1

Note that this is not related to getting emails set up in the traditional sense of elmah, I have already successfully done so.

I am attempting to utilize a github project (https://github.com/dylanbeattie/ElmahASP) that was created to send "realish" errors to elmah from classic asp pages.

I have this implemented so that in the project on .net pages, errors are logged/emails sent. On classic asp pages, errors are logged, but no email is sent (here lies my issue)

The errors are logged in classic asp via a custom handler (provided by the github project):

    public void ProcessRequest(HttpContext context) {
        try {
            var jsonData = new StreamReader(context.Request.InputStream).ReadToEnd();
            var aspError = JsonConvert.DeserializeObject<RemoteError>(jsonData);
            var elmahError = aspError.ToElmahError();
            //TODO: might be worth putting some code in here that will determine the username based on the cookie collection in the deserialized error object.
            ErrorLog.GetDefault(HttpContext.Current).Log(elmahError);
            context.Response.Write("Error logged OK!");
        } catch (Exception ex) {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
        context.Response.Write("THis is a response!");
    }

It appears that doing a ErrorLog.Log does in fact log the error, but bypasses the event that would need to be thrown that would cause an error email to be sent out. The elmah class that handles sending out emails has several non public methods for sending out mail which I wanted to utilize, however the following is not working due to me not understanding... something.

I have attempted to write an extension of the mail module, have successfully registered it (as I've confirmed aspx pages are still sending out emails, but am still having problems with classic asp error mails). Here is my class:

public class ErrorMailModule : Elmah.ErrorMailModule
{
    public void SendErrorMail(Elmah.Error error)
    {
        base.ReportError(error);
    }
}

I then change the handler from above to:

    public void ProcessRequest(HttpContext context) {
        try {
            var jsonData = new StreamReader(context.Request.InputStream).ReadToEnd();
            var aspError = JsonConvert.DeserializeObject<RemoteError>(jsonData);
            var elmahError = aspError.ToElmahError();
            //TODO: might be worth putting some code in here that will determine the username based on the cookie collection in the deserialized error object.
            ErrorLog.GetDefault(HttpContext.Current).Log(elmahError);

            ErrorMailModule mail = new ErrorMailModule;
            mail.SendErrorMail(elmahError);

            context.Response.Write("Error logged OK!");
        } catch (Exception ex) {
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
        context.Response.Write("THis is a response!");
    }

This however doesn't work, I'm quite sure it's due to me newing up a error mail module, rather than by utilizing the one that I'm (assuming) is registered at init - but I'm unsure how to do that.

I feel like having a custom event registered and monitored for (then raised in the handler) would be one way to go... but I am unsure on how to proceed as I've never worked with events.

How would I go about implementing this? Would events be the way to go or is there another path I could take?

Keith
  • 20,636
  • 11
  • 84
  • 125
Kritner
  • 13,557
  • 10
  • 46
  • 72
  • See this; http://stackoverflow.com/questions/7441062/how-to-use-elmah-to-manually-log-errors – Ryan O'Neill Apr 07 '15 at 14:24
  • @RyanO'Neill Hey thanks, I was initially going down that route, but `ErrorSignal` requires an `exception` passed to it, because this is classic ASP errors being caught by a .net handler, I was having trouble projecting my classic asp exception to a .net exception... I did have classic asp as a tag in my question, but it was removed yesterday (i think that's probably what bumped this question up for another view) – Kritner Apr 08 '15 at 19:42

1 Answers1

0

Not sure if you still need this, but you could fire it manually inside OnException event handler.

protected override void OnException(ExceptionContext filterContext)
    {
        var jsonData = new StreamReader(filterContext.Request.InputStream).ReadToEnd();
        var aspError = JsonConvert.DeserializeObject<RemoteError>(jsonData);
        var elmahError = aspError.ToElmahError();
        ErrorLog.GetDefault(HttpContext.Current).Log(elmahError);

        ErrorMailModule mail = new ErrorMailModule;
        mail.SendErrorMail(elmahError); 
        Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception); // to store error
        base.OnException(filterContext);
    }
vohrahul
  • 1,123
  • 10
  • 17