Search code examples
sql-serverasp.net-mvcnlogelmahelmah.mvc

Elmah.MVC + NLog - store all errors in one place


I'm working on an ASP.NET MVC 5 app in Visual Studio 2015. We use NLog to write some errors and other information to the database in our try/catch blocks. However, it would be nice to also implement ELMAH.MVC, so that any/all uncaught exceptions get caught/logged and the user redirected to a friendly page.

Here's our NLog table structure; note that ApplicationId is not equivalent to Application on the ELMAH_Error table; people apply for things through the app, and this is the ID we assign when they start the process. And RouteId is not an MVC route.

CREATE TABLE [dbo].[Log](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ApplicationId] [int] NULL,
    [RouteId] [int] NULL,
    [MachineName] [varchar](50) NULL,
    [TimeStamp] [datetime2](7) NULL,
    [LogLevel] [varchar](5) NULL,
    [Logger] [nvarchar](max) NULL,
    [Message] [nvarchar](max) NULL,
    [Exception] [nvarchar](max) NULL,
    [StackTrace] [nvarchar](max) NULL,
 CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

The SQL for ELMAH's table looks like this (from the script here):

CREATE TABLE [dbo].[ELMAH_Error]
(
    [ErrorId]     UNIQUEIDENTIFIER NOT NULL,
    [Application] NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Host]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Type]        NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Source]      NVARCHAR(60)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Message]     NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [User]        NVARCHAR(50)  COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [StatusCode]  INT NOT NULL,
    [TimeUtc]     DATETIME NOT NULL,
    [Sequence]    INT IDENTITY (1, 1) NOT NULL,
    [AllXml]      NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL 
) 
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Is it possible to have the ELMAH_Error table contain the additional columns found on the NLog Log table, or vice versa? How can I write errors from NLog to the ELMAH table?

Thank you.

Update 1: Came across this: https://github.com/ccellar/nlog-elmah-target, but it lacks any documentation on how you use it. Would this redirect all NLog exceptions to ELMAH?

Update 2: A related question has been asked about adding the Exception.Data dictionary to ELMAH. This way, any number of key/value pairs can be added to what Elmah stored. This way, all the data now stored with NLog would be stored on ELMAH.


Solution

  • Found a solution that works very nicely: https://github.com/NLog/NLog.Elmah/

    To install,

    1. Install-Package NLog.Elmah
    2. Update-Package NLog
    3. I also installed NLog.Web package, but isn't required for this.
    4. Modify NLog.config to include:
    <targets><target name="target1" xsi:type="Elmah" LogLevelAsType"false" /></targets>
        <rules><logger name="*" minlevel="Info" writeTo="target1" /></rules>
    

    Now just decorate any controller/class that uses NLog with the target:

    [Target("Elmah")]
    public class HomeController : Controller
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        public ActionResult Index()
        {
            // Other code...
    
            var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, "Some information...");
            _logger.Log(logEvent);
    
            // Other code...
        }
    }
    

    You can also throw the attribute on a base controller/class and cover all your application.