1

I am trying to implement ELMAH in a webapi project,since I am new to this elmah technique,I am not able to implement the entire thing.I even tried to follow the sample codes in web but still I am not getting.

can someone please help me to achieve a proper working solution with elmah. I will be very thankful if a working solution is provided for demo purpose which will be really helpful for me to understand

vickey
  • 53
  • 1
  • 12
  • There is already a question concerning this topic: https://stackoverflow.com/questions/10116593/how-to-set-up-elmah-with-asp-net-web-api – tschan Dec 09 '19 at 09:08
  • Please check out the solution from [@ahmed-ahemd](https://stackoverflow.com/users/388788/ahmed-ahmed) here: https://stackoverflow.com/a/21856256/3974799 – tschan Dec 09 '19 at 19:00

2 Answers2

4

Here are the steps to send error emails using Elmah

  1. Install Elmah Nuget Package
  2. Update config file to use proper SMTP settings. Here is the example of config file settings

    < security allowRemoteAccess="false" />
    < errorMail subject="Production Error - {1}: {0}" smtpServer="server address" from="me@you.com" to="you@me.com" />
    
  3. Create ExceptionLogger class. Here is the example of it

    public class ElmahExceptionLogger : ExceptionLogger
    {
      private const string HttpContextBaseKey = "MS_HttpContext";
    
      public override void Log(ExceptionLoggerContext context)
      {
        // Retrieve the current HttpContext instance for this request.
        HttpContext httpContext = GetHttpContext(context.Request);
    
        // Wrap the exception in an HttpUnhandledException so that ELMAH can capture the original error page.
        Exception exceptionToRaise = new HttpUnhandledException(message: null, innerException: context.Exception);
    
        ErrorSignal signal;
        if (httpContext == null)
        {
           signal = ErrorSignal.FromCurrentContext();
           // Send the exception to ELMAH (for logging, mailing, filtering, etc.).
           signal.Raise(exceptionToRaise);
        }
        else
        {
            signal = ErrorSignal.FromContext(httpContext);
            signal.Raise(exceptionToRaise);
        }
    }
    
    private static HttpContext GetHttpContext(HttpRequestMessage request)
    {
        HttpContextBase contextBase = GetHttpContextBase(request);
    
        if (contextBase == null)
        {
            return null;
        }
    
        return ToHttpContext(contextBase);
    }
    
    private static HttpContextBase GetHttpContextBase(HttpRequestMessage request)
    {
        if (request == null)
        {
            return null;
        }
    
        object value;
    
        if (!request.Properties.TryGetValue(HttpContextBaseKey, out value))
        {
            return null;
        }
    
        return value as HttpContextBase;
    }
    
    private static HttpContext ToHttpContext(HttpContextBase contextBase){return contextBase.ApplicationInstance.Context; } }
    
  4. Register ElmahExceptionLogger class with Web API in startup.cs

    config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());
    
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
Paresh
  • 993
  • 1
  • 7
  • 15
3

Even though the answer by @Paresh works, you should use the Elmah.Contrib.WebApi package, since that includes everything needed to use ELMAH with Web API.

I've written a guide to install ELMAH with Web API. Basically you will install the ELMAH and Elmah.Contrib.WebApi packages and then configure it like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...
        config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());
        ...
    }
}

Regarding the mail configuration, you can validate your web.config using the ELMAH Configuration Validator.

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73