Frage

ich mit ELMAH gerade begonnen und bin ein Fan. Mein Team unterstützt eine große Anzahl von Web-Anwendungen, und ich bin besonders begeistert, dass ELMAH wir Ausnahmen von jeder Anwendung auf die gleiche MS SQL-Datenbanktabelle speichern kann.

Wir unterstützen auch einige Konsolen, DLL und Desktop-Anwendungen. Ist es möglich, die ELMAH DLL zu verwenden, Ausnahmen in diesen Anwendungen auf derselben Stelle anmelden?

War es hilfreich?

Lösung

Wir haben genau die gleiche Situation hier. Laufen ELMAH für alle unsere Web-Anwendungen. Ein paar von ihnen den Konsolen Disponenten haben.

einige graben durch den Quellcode Nach tun, der folgende Code scheint zu funktionieren:

            ErrorLog errorLog = ErrorLog.GetDefault(null);
            errorLog.ApplicationName = "/LM/W3SVC/1/ROOT/AppName";
            errorLog.Log(new Error(ex));

Das einzige wirkliche Problem mit der oben ist, dass Sie den Namen der Anwendung irgendwo in der Config zu halten brauchen, um die Einträge auf dem ELMAH.axd Zuschauer sehen zu können.

So in unserer generischen Fehlerbehandlung Code, den wir tun:

        if (HttpContext.Current != null)
            ErrorSignal.FromCurrentContext().Raise(ex);
        else
        {
            ErrorLog errorLog = ErrorLog.GetDefault(null);
            errorLog.ApplicationName = ErrorHandling.Application;
            errorLog.Log(new Error(ex));
        }

Andere Tipps

Wir mussten in der Lage von einer Konsole App und Windows-Dienst zusätzlich zu unserer ASP.NET-Site anmelden. Früher habe ich die Antwort (ErrorLog.GetDefault(null);), die gut funktioniert, bis ich auch eine E-Mail erforderlich.

So, hier ist meine Lösung. Es behandelt das Protokoll, E-Mail, tweeten und Filterung (beide in der Konfigurationsdatei und in Code). Ich habe auch den Haupt-Aufruf als Erweiterung Ausnahme gewickelt so kann es wie genannt werden: catch(Exception ex) { ex.LogToElmah(); }

in Code zu filtern, haken Sie das entsprechende .Filtering Ereignis: ElmahExtension.ErrorLog.Filtering += new ExceptionFilterEventHandler(ErrorLog_Filtering);

Code:

using System;
using System.Web;
using Elmah;
namespace System
{
    public static class ElmahExtension
    {
        public static void LogToElmah(this Exception ex)
        {
            if (HttpContext.Current != null)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
            }
            else
            {
                if (httpApplication == null) InitNoContext();
                ErrorSignal.Get(httpApplication).Raise(ex);
            }
        }

            private static HttpApplication httpApplication = null;
            private static ErrorFilterConsole errorFilter = new ErrorFilterConsole();

            public static ErrorMailModule ErrorEmail = new ErrorMailModule();
            public static ErrorLogModule ErrorLog = new ErrorLogModule();
            public static ErrorTweetModule ErrorTweet = new ErrorTweetModule();

            private static void InitNoContext()
            {
                httpApplication = new HttpApplication();
                errorFilter.Init(httpApplication);

                (ErrorEmail as IHttpModule).Init(httpApplication);
                errorFilter.HookFiltering(ErrorEmail);

                (ErrorLog as IHttpModule).Init(httpApplication);
                errorFilter.HookFiltering(ErrorLog);                

                (ErrorTweet as IHttpModule).Init(httpApplication);
                errorFilter.HookFiltering(ErrorTweet);
            }

            private class ErrorFilterConsole : ErrorFilterModule
            {
                public void HookFiltering(IExceptionFiltering module)
                {
                    module.Filtering += new ExceptionFilterEventHandler(base.OnErrorModuleFiltering);
                }
            }
    }
}

Darüber hinaus müssen Sie hierfür einen Verweis auf die System.Web.dll in einem Projekt hinzuzufügen zu arbeiten.

Bearbeiten : Richtet sich nach den Kommentaren, wird dieser Code E-Mails nur dann, wenn Ihre Konfigurationsdatei <errorMail async="false"/> hat. Siehe href="http://pastebin.com/L2xTT71K" zum dieser Code-Snippet sollten Sie <errorMail async="true"/> in Ihrer Konfigurationsdatei behalten möchten (zu verwenden, wenn HttpContext.Current verfügbar ).

Wenn Sie nur wollen, ohne das Protokoll per E-Mail http Sie so tun können:

    public class MyElmahMail: ErrorMailModule
    {
        public MyElmahMail()
        {
//this basically just gets config from errorMail  (app.config)
            base.OnInit(new HttpApplication());
        }
        public void Log(Error error)
        {
//just send the email pls
            base.ReportError(error);
        }
    }

//to call it
var mail = new MyElmahMail();
mail.Log(new Error(new NullReferenceException()));//whatever exception u want to log

Und in Bezug auf app.config

//Under configSections
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
    </sectionGroup>

Und

  <elmah>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\Elmah.Error" applicationName="MyAwesomeApp" />
    <errorMail from="from@email.com" to="to@email.com" />
  </elmah>

Und SMTP- Einstellungen Ihrer Wahl.

Alles erledigt. : -)

Bearbeiten : Die CAN getan werden - Siehe diese Antwort.


Ich bin mir ziemlich sicher, dass Sie dies nicht tun können. Ich werde versuchen, das entsprechende Material ausgraben.

http: // Gruppen .google.com / Gruppe / Elmah / browse_thread / thread / f214c4f782dc2bf4 / d96fe43b60765f0c? lnk = gst & q = app # d96fe43b60765f0c

So, was ich finden kann, die Google-Gruppe suchen ist, dass es nicht möglich ist ... Da ELMAH weg von Httphandlers arbeitet (ein asp.net-Konstrukt) ist es ASP.NET nur.

Mit diesem wird gesagt, es gibt Möglichkeiten, wie Sie es auf einer Konsole-Anwendung nutzen können. ELMAH stellt ein Verfahren Fehler zu erhöhen, so dass Sie könnte ELMAH in Ihrer Ausnahmebehandlung wickelt und dann ein Fehlersignal über:

ErrorSignal.FromCurrentContext().Raise(new NotSupportedException());

Dies würde bedeuten, dass Ihre gesamte Anwendung in einem Ausnahmehandler und Signalgebung Einwickeln. Es könnte Ihnen einige Optimierungen nehmen, um es nach unten, aber ich denke, es ist durchaus möglich ist.

Falls Sie es benötigen, ist dies der Link auf die

Für diejenigen, die Brian Chance Antwort müssen VB.NET portiert:

Imports System
Imports System.Web
Imports Elmah
Namespace System
    Public NotInheritable Class ElmahExtension
        Private Sub New()
        End Sub
        <System.Runtime.CompilerServices.Extension> _
        Public Shared Sub LogToElmah(ex As Exception)
            If HttpContext.Current IsNot Nothing Then
                ErrorSignal.FromCurrentContext().Raise(ex)
            Else
                If httpApplication Is Nothing Then
                    InitNoContext()
                End If
                ErrorSignal.[Get](httpApplication).Raise(ex)
            End If
        End Sub

        Private Shared httpApplication As HttpApplication = Nothing
        Private Shared errorFilter As New ErrorFilterConsole()

        Public Shared ErrorEmail As New ErrorMailModule()
        Public Shared ErrorLog As New ErrorLogModule()
        Public Shared ErrorTweet As New ErrorTweetModule()

        Private Shared Sub InitNoContext()
            httpApplication = New HttpApplication()
            errorFilter.Init(httpApplication)

            TryCast(ErrorEmail, IHttpModule).Init(httpApplication)
            errorFilter.HookFiltering(ErrorEmail)

            TryCast(ErrorLog, IHttpModule).Init(httpApplication)
            errorFilter.HookFiltering(ErrorLog)

            TryCast(ErrorTweet, IHttpModule).Init(httpApplication)
            errorFilter.HookFiltering(ErrorTweet)
        End Sub



    Private Class ErrorFilterConsole
        Inherits Elmah.ErrorFilterModule


        Public Sub HookFiltering([module] As Elmah.IExceptionFiltering)
            AddHandler [module].Filtering, New Elmah.ExceptionFilterEventHandler(AddressOf MyBase.OnErrorModuleFiltering)
        End Sub

    End Class


    End Class
End Namespace

Allerdings nur für Fehler in der Datenbank anmelden, wird dies ausreichen, um:

If System.Web.HttpContext.Current Is Nothing Then
    Dim req As System.Web.HttpRequest = New System.Web.HttpRequest(String.Empty, "https://www.domain.tld", Nothing)
    Dim res As System.Web.HttpResponse = New System.Web.HttpResponse(Nothing)
    System.Web.HttpContext.Current = New System.Web.HttpContext(req, res)

    'Dim request As System.Web.Hosting.SimpleWorkerRequest = New System.Web.Hosting.SimpleWorkerRequest("/blah", "c:\inetpub\wwwroot\blah", "blah.html", Nothing, New System.IO.StringWriter())
    'System.Web.HttpContext.Current = New System.Web.HttpContext(request)

    System.Web.HttpContext.Current.ApplicationInstance = New System.Web.HttpApplication()

    Dim ErrorLog As New Elmah.ErrorLogModule()
    TryCast(ErrorLog, System.Web.IHttpModule).Init(System.Web.HttpContext.Current.ApplicationInstance)
End If

Als Komplettlösung:

Public parent As Elmah.ServiceProviderQueryHandler = Nothing




' http://stackoverflow.com/questions/5981750/configuring-elmah-with-sql-server-logging-with-encrypted-connection-string
Public Function Elmah_MS_SQL_Callback(objContext As Object) As System.IServiceProvider
    Dim container As New System.ComponentModel.Design.ServiceContainer(parent(objContext))
    Dim strConnectionString As String = COR.SQL.MS_SQL.GetConnectionString()

    Dim log As Elmah.SqlErrorLog = New Elmah.SqlErrorLog(strConnectionString)
    'Dim strApplicationName = System.Web.Compilation.BuildManager.GetGlobalAsaxType().BaseType.Assembly().FullName
    Dim strApplicationName As String = System.Reflection.Assembly.GetExecutingAssembly().FullName
    If Not String.IsNullOrEmpty(strApplicationName) Then
        log.ApplicationName = strApplicationName.Substring(0, strApplicationName.IndexOf(","))
    End If

    container.AddService(GetType(Elmah.ErrorLog), log)
    Return container
End Function ' Elmah_MS_SQL_Callback




Public Function Elmah_PG_SQL_Callback(objContext As Object) As System.IServiceProvider
    Dim container As New System.ComponentModel.Design.ServiceContainer(parent(objContext))
    Dim strConnectionString As String = COR.SQL.MS_SQL.GetConnectionString()

    Dim log As Elmah.PgsqlErrorLog = New Elmah.PgsqlErrorLog(strConnectionString)
    'Dim strApplicationName = System.Web.Compilation.BuildManager.GetGlobalAsaxType().BaseType.Assembly().FullName
    Dim strApplicationName As String = System.Reflection.Assembly.GetExecutingAssembly().FullName
    If Not String.IsNullOrEmpty(strApplicationName) Then
        log.ApplicationName = strApplicationName.Substring(0, strApplicationName.IndexOf(","))
    End If

    container.AddService(GetType(Elmah.ErrorLog), log)
    Return container
End Function ' Elmah_PG_SQL_Callback


' http://weblogs.asp.net/stevewellens/archive/2009/02/01/debugging-a-deployed-site.aspx
Public Sub Initialize()

    If System.Web.HttpContext.Current Is Nothing Then
        Dim req As System.Web.HttpRequest = New System.Web.HttpRequest(String.Empty, "https://www.domain.tld", Nothing)
        Dim res As System.Web.HttpResponse = New System.Web.HttpResponse(Nothing)
        System.Web.HttpContext.Current = New System.Web.HttpContext(req, res)

        'Dim request As System.Web.Hosting.SimpleWorkerRequest = New System.Web.Hosting.SimpleWorkerRequest("/blah", "c:\inetpub\wwwroot\blah", "blah.html", Nothing, New System.IO.StringWriter())
        'System.Web.HttpContext.Current = New System.Web.HttpContext(request)

        System.Web.HttpContext.Current.ApplicationInstance = New System.Web.HttpApplication()

        Dim ErrorLog As New Elmah.ErrorLogModule()
        TryCast(ErrorLog, System.Web.IHttpModule).Init(System.Web.HttpContext.Current.ApplicationInstance)
    End If



    parent = Elmah.ServiceCenter.Current

    If SQL.IsMsSql Then
        Elmah.ServiceCenter.Current = AddressOf Elmah_MS_SQL_Callback
    End If

    If SQL.IsPostGreSql Then
        Elmah.ServiceCenter.Current = AddressOf Elmah_PG_SQL_Callback
    End If
End Sub ' InitializeElmah

Und

Elmah.ErrorSignal.FromCurrentContext().Raise(New NotImplementedException("Test"))

wird funktionieren, wenn es nach dem Initialisieren genannt wird ()

Nun, da ich nicht kommentieren kann ich diese hier posten unten und vielleicht jemand wird es sehen.

Nach Brians Methode folgen und die commentors ich war in der Lage E-Mail zum Laufen zu bringen, aber ich war noch nicht zu sehen, die SQL-Nachrichten protokolliert werden, obwohl ich den Anwendungsnamen gesetzt hatte. Was ich wusste nicht, dass sie tatsächlich protokollieren ich war einfach nicht sie zu sehen, weil der Anwendungsname die gleichen wie web.config, um es sehen zu können, sein muß.

Meine web.config haben nicht angegeben application, so war es in Verzug zu „/ LM / W3SVC / 2 / ROOT“, die im Grunde ist das, was „asgeo1“ kommentierte, obwohl ich es war nicht klar, das musste sein gleich.

Da ich nicht wirklich irgendwelche Fehler habe ich mit angehen, ich konfigurierte application in meinem web.config und meine app.config das gleiche zu sein und jetzt ist alles zeigt wie ein Champion auf.

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ELMAH" applicationName="MyAppName" />

ELMAH steht für Fehlerprotokollierung Module und Handlers - Bezug genommen wird, natürlich auf IHttpModule und IHttpHandler

.

Konsolenanwendungen nicht HTTP verwenden, so würden in der Regel nicht in der Lage sein, viel profitieren von Modulen und für HTTP gebaut Handlers.

scroll top