2016-08-01 4 views
1

Ich versuche ELMAH in einem Webapi-Projekt zu implementieren, da ich neu in dieser Elmah-Technik bin, bin ich nicht in der Lage, das gesamte Ding zu implementieren.Ich habe sogar versucht, die Beispielcodes im Web folgen trotzdem bekomme ich nicht.Implementieren Elmah in Web-API-Projekt

kann mir bitte jemand helfen, eine funktionierende lösung mit elmah zu erreichen. Ich bin sehr dankbar, wenn eine funktionierende Lösung für Demo-Zwecke vorgesehen ist, die wirklich hilfreich für mich

Antwort

2

Hier sind die Schritte zu verstehen, Fehler E-Mails mit Elmah

  1. Installieren Elmah Nuget Package
  2. senden Aktualisieren Sie die Konfigurationsdatei, um die richtigen SMTP-Einstellungen zu verwenden. Hier ist das Beispiel der Konfigurationsdatei-Einstellungen

    < security allowRemoteAccess="false" /> 
    < errorMail subject="Production Error - {1}: {0}" smtpServer="server address" from="[email protected]" to="[email protected]" /> 
    
  3. ExceptionLogger Klasse erstellen. Hier ist das Beispiel dafür

    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 Klasse mit Web-API in startup.cs

    config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger()); 
    
2

Auch wenn die Antwort von @Paresh funktioniert, sollten Sie die Elmah.Contrib.WebApi Paket verwenden, da diese enthält alles, was benötigt wird, um ELMAH mit Web API zu verwenden.

Ich habe eine Anleitung zu install ELMAH with Web API geschrieben. Grundsätzlich finden Sie die ELMAH und Elmah.Contrib.WebApi Pakete installieren und konfigurieren Sie es wie folgt aus:

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

In Bezug auf die E-Mail-Konfiguration können Sie Ihre web.config mit dem ELMAH Configuration Validator validieren.