Is it possible to do the following using ELMAH?
logger.Log(" something");
I'm doing something like this:
try { // Code that might throw an exception } catch(Exception ex) { // I need to log error here... }
This exception will not be automatically logged by ELMAH, because it was handled.
Direct log writing method, working since ELMAH 1.0:
try { some code } catch(Exception ex) { Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex)); }
ELMAH 1.2 introduces a more flexible API:
try { some code } catch(Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); }
There is a difference between the two solutions:
Raise
method applies ELMAH filtering rules to the exception. Log
method does not.Raise
is subscription based and is able to log one exception into the several loggers.I'd recommend wrapping the call to Elmah in a simple wrapper class of your own.
using Elmah; public static class ErrorLog { /// <summary> /// Log error to Elmah /// </summary> public static void LogError(Exception ex, string contextualMessage=null) { try { // log error to Elmah if (contextualMessage != null) { // log exception with contextual information that's visible when // clicking on the error in the Elmah log var annotatedException = new Exception(contextualMessage, ex); ErrorSignal.FromCurrentContext().Raise(annotatedException, HttpContext.Current); } else { ErrorSignal.FromCurrentContext().Raise(ex, HttpContext.Current); } // send errors to ErrorWS (my own legacy service) // using (ErrorWSSoapClient client = new ErrorWSSoapClient()) // { // client.LogErrors(...); // } } catch (Exception) { // uh oh! just keep going } } }
Then just call it whenever you need to log an error.
try { ... } catch (Exception ex) { // log this and continue ErrorLog.LogError(ex, "Error sending email for order " + orderID); }
This has the following benefits:
Note: I've added a 'contextualMessage' property for contextual information. You can omit this if you prefer but I find it very useful. Elmah automatically unwraps exceptions so the underlying exception will still be reported in the log but the contextualMessage will be visible when you click on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With