Logging unhandled exceptions using NLog? Should ELMAH and NLog be used together?
-
27-09-2019 - |
Question
I am using ELMAH in my ASP.NET MVC projects and I really like its simplicity. But I needed the ability to log certain events in my code in a log.Info("message") manner. Since ELMAH does not provide that ability I started looking at NLog.
A few questions came to mind:
- Is using both ELMAH and NLog overkill? Using ELMAH for unhandled exceptions and NLog for everything else?
- Is it possible to configure NLog to log unhandled exceptions in the nlog.config or is custom code needed?
- If custom code is needed where would be the best place to add it? The OnException method in the MVC framework? The global.asax file?
Anny feedback is greatly appreciated. So far I haven't found any good posts on this matter?
Solution
You can in fact trigger an alert from ELMAH. Like so:
ErrorSignal.FromCurrentContext().Raise(new System.ApplicationException("An error occured in SendEmail()", ex));
Don't forget to add a reference to elmah.dll and add using Elmah;
to the file.
See here for a bit more examples.
OTHER TIPS
Answering question number 2, I just read this article where it explains how to log un-handled exceptions with NLog. Basically is adding in the Global.asax.cs file something like:
protected void Application_Error()
{
Exception lastException = Server.GetLastError();
NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.Fatal(lastException);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow