Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging unhandled exceptions using NLog? Should ELMAH and NLog be used together?

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:

  1. Is using both ELMAH and NLog overkill? Using ELMAH for unhandled exceptions and NLog for everything else?
  2. Is it possible to configure NLog to log unhandled exceptions in the nlog.config or is custom code needed?
  3. 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?

like image 528
Thomas Avatar asked Nov 01 '10 01:11

Thomas


People also ask

How do I start logging with NLog?

Create a Console Application project in Visual Studio. Install NLog and its dependencies. Create and configure the NLog logger. Integrate the logger into the C# Console Application.

What is NLog used for?

NLog is “a free logging platform for . NET, Silverlight and Windows Phone with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.”

What is NLog config in C#?

NLog is a . Net Library that enables you to add high-quality logs for your application. NLog can also be downloaded using Nugget in Visual Studio. Targets are used to display, store, or pass log messages to another destination.


2 Answers

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);
}
like image 187
Felix Martinez Avatar answered Oct 12 '22 02:10

Felix Martinez


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.

like image 38
edosoft Avatar answered Oct 12 '22 02:10

edosoft