How to use ELMAH to manually log errors
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.
Elmah error logging, can I just log a message?
Yes, you can use ErrorSignal without throwing an exception.
ErrorSignal.FromCurrentContext().Raise(new NotSupportedException());
For the custom message, you can create a custom exception.
var customEx = new Exception("Hello I am testing Elmah", new NotSupportedException());
ErrorSignal.FromCurrentContext().Raise(customEx);
ELMAH Log handled exceptions
Of course it's only getting triggered for unhandled exceptions. Those filters only run for errors that are allowed to bubble up (meaning, unhandled). If you want to log a handled exception, then you need to put that ErrorSignal.FromCurrentContext().Raise()
logic inside of your catch block.
catch (Exception e)
{
ModelState.AddModelError("", "Something unexpected happened, please try again.");
ErrorSignal.FromCurrentContext().Raise(e);
return View(model);
}
If you find yourself doing this a lot, then I suggest you switch off using Elmah. Elmah isn't a general logging framework, it's geared for unhandled errors. It would be better to use a logging system such as Serilog or Nlog and then have those log to a specialized system such as Seq.
Manually logged ELMAH errors aren't sending email
I spoke too soon.
This doesn't send email:
Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Elmah.Error(ex));
But this does:
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
Manual Elmah Logging dosnt work properly in MVC
I have also seen this problem when calling ELMAH logger without any HttpContext in class library projects.
I used the old manual way to get around this:
Elmah.ErrorLog.GetDefault(null).Log(new Error(ex));
Log an exception without throwing it when using ELMAH
Your code logs the thrown exception (ex) in ELMAH and returns successful to the caller. In other words, Raise does not throw an exception.
If your catch block re-throws ex or throw a new exception, both ex and the new exception are logged to ELMAH and a status code 500 is returned to the caller.
Manual Elmah logging
I understand why filter ignores error - because when using Elmah.ErrorSignal.FromCurrentContext().Raise()
we are "emulate" process of getting error and this errors fall under the filter.
Instead of this method we should use Elmah logger method to add error to log:
Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log()
This method will add error to log and filter below will ignore all others:
<errorFilter>
<test>
<regex binding="Context.Request.Url" pattern="." />
</test>
</errorFilter>
After all this I have one question - name tag inside <errorFilter>
can only be test
?
Related Topics
Send Push to Android by C# Using Fcm (Firebase Cloud Messaging)
Posting JSON to Url via Webclient in C#
How to Manage Files on an Mtp Portable Device
Xamarin iOS Memory Leaks Everywhere
How to Add a Custom View to a Xib File Defined View in Monotouch
Executing R Script Programmatically
How to Request Administrator Permissions When the Program Starts
Upload File Through C# Using JSON Request and Restsharp
Unity: Apple Game Center iOS7 Registers the Scores for Only 30 Minutes in the Leaderboard
How to Call Code Behind Server Method from a Client Side JavaScript Function
Extending an Enum via Inheritance
C# Equivalent to Java's Charat()
How Does Hashset Compare Elements for Equality
MVC Which Submit Button Has Been Pressed
How to Intercept All Key Events, Including Ctrl+Alt+Del and Ctrl+Tab
Simple Injector Unable to Inject Dependencies in Web API Controllers