Using ELMAH in ASP.NET Core

March 20, 2016 by Anuraj

ASP.NET ELMAH ELM ASP.NET Core Logging

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment. ELMAH does not work with ASP.NET 5 because ELMAH (given its name) is based on ASP.NET 4.x’s Modules and Handlers (the ‘MAH’ of ‘ELMAH’). There is a prototype in ASP.NET 5 called ‘ELM’ (Error Logging Middleware) that has some features similar to ELMAH. This post is about using ELM in ASP.NET 5.

This post is about configuring ELMAH in ASP.NET Core.

  • First you need to update project.json file and add reference of ELM.
{
    "dependencies": {
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
        "Microsoft.AspNet.Diagnostics.Elm": "1.0.0-rc1-final"
    },
    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004"
    },
    "frameworks": {
         "dnx451": { },
         "dnxcore50": { }
    }
}
  • Next you need to configure the Startup file ConfigureServices and Configure methods to support ELM.
public void ConfigureServices(IServiceCollection services)
{
    services.AddElm();
    services.ConfigureElm(options => {
        options.Path = new PathString("/elm");
        options.Filter = (name, level) => level >= LogLevel.Verbose;
    });
    
    services.AddMvc();
}
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    app.Map("/HelloMVC6", map => 
    {
        app.UseElmPage();
        app.UseElmCapture();
        app.UseIISPlatformHandler();
        app.UseDeveloperExceptionPage();
        app.UseMvcWithDefaultRoute();
    });
}

Now if you browse http://localhost:5004/elm, you can see the logging information. Due to line break characters in style, the CSS is not working properly.

ASP.NET Core ELM Log

Happy Coding

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub