Getting the Id of an error in Elmah after calling .Raise()

前端 未结 4 1747
悲&欢浪女
悲&欢浪女 2021-02-15 13:39

I\'m working on an MVC3 application and I\'m using Elmah to handle my error logging. What I want in my application is to carry the Elmah Id onto the custom error page as I will

4条回答
  •  温柔的废话
    2021-02-15 14:09

    The solution above only works only if there is a Session object (website scenario). We needed it to work in an Azure WorkerRole, or a console / desktop app type setup. This solution will also work for web and save some session memory. There isn't a perfect solution, but one that worked for us to be able to log the error and retrieve the stored ID AND fire off an email is to:

    1. Store the error using ErrorLog.Log(error) (see: Using ELMAH in a console application)
    2. Raise the error skipping the logging (SQL or otherwise)

    For the second part, we used the implementation of ElmahExtension given here: https://stackoverflow.com/a/2473580/476400

    and REMOVED the following lines adding the logging:

    (ErrorLog as IHttpModule).Init(httpApplication);
    errorFilter.HookFiltering(ErrorLog);  //removed!
    

    The entire call from our client code looks like this:

    ErrorLog errorLog = ErrorLog.GetDefault(null);
    errorLog.ApplicationName = "YourAppName";
    Error error = new Error(ex);
    string errorResult = errorLog.Log(error);
    Guid errorId = new Guid(errorResult);
    
    ex.LogToElmah(); //this is just going to send the email
    

    You might want to call that extention method something else, like RaiseToElmahNoStorage(), or something to indicate it is skipping the storage component.

提交回复
热议问题