Should I log more than exceptions with Elmah?

168 Views Asked by At

I wonder what exactly should I log. I mean, I can log exceptions and this is pretty standard, but can I use Elmah to log other things?

I actually don't use that many try, catch blocks. I read that these should be used only for special cases exceptions not for "standard behavior" because they can slow down performance.

Usually I prevent exceptions from occurring using simple if clause.

For example in simple division by 0.

int z = x/y;

I will write this as follows:

int z;
if(y!=0)
{
  z = x/y;
}
else
{
   z=0;
}

Now this is perfectly acceptable for me.

I don't consider this to be an exception but still... I would like to know that somewhere the y parameter equals 0.

Is it ok to log this information using Elmah ? Or should I use something else?

2

There are 2 best solutions below

2
On BEST ANSWER

You can easy log with elmah manually. Just call:

 ErrorSignal.FromCurrentContext().Raise(new Exception());

For custom message just run:

 var customEx = new Exception("Hey y!=0!!!"); 
 ErrorSignal.FromCurrentContext().Raise(customEx);

You can also decide to use log4net or nlog for more complex logging and use Elmah appender

0
On

You also can do a helper to log your own errors:

public static void OwnError(Exception e, String myMsg)
{
     var exception = new Exception(myMsg, e);
     Elmah.ErrorSignal.FromCurrentContext().Raise(exception);    
}