ELMAH configuration when registering SQL Server with an encrypted connection string

I am trying to configure ELMAH error logging in an ASP.NET 4 application using SQL Server 2008 R2. Is there any way to tell ELMAH about the call of our internal decryption function in the connection string that we provide? Do I need to change the source of ELMAH and rebuild?

<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>
</configSections>

<elmah>
  <security allowRemoteAccess="1" />
  <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ELMAH" />
</elmah>

<connectionStrings>
  <add name="ELMAH" connectionString="EncryptedConnectionString" providerName="System.Data.SqlClient" />
</connectionStrings>

<system.webServer>
  <handlers>
    <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
  <modules>
    <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  </modules>
</system.webServer>
+5
source share
2 answers

ELMAH - . , ELMAH , ErrorLog, . , SqlErrorLog.

, ServiceProviderQueryHandler . :

public delegate IServiceProvider ServiceProviderQueryHandler(object context); 

, IServiceProvider. , .NET Framework. . System.ComponentModel.Design.ServiceContainer. GetService ErrorLog, , , SqlErrorLog, , . :

var parent = ServiceCenter.Current;
ServiceCenter.Current = context => { 
    var container = new ServiceContainer(parent(context)); 
    var connectionSettings = ConfigurationManager.ConnectionStrings["FOOBAR"];
    var connectionString = Decrypt(connectionSettings.ConnectionString);
    var log = new SqlErrorLog(connectionString); 
    container.AddService(typeof(ErrorLog), log); 
    return container; 
  } ;

. / , , . ELMAH , ServiceCenter.Current - , , .

, , , , .

1.2 - . ELMAH ELMAH .

+12

Atif Aziz, VB.NET(InitializeElmah_VB9 .NET 2.0 [ lamdda], InitializeElmah VB.NET .NET 4.0)

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication


    Public Overrides Sub Init()
        MyBase.Init()

        InitializeElmah_VB9()
        'InitializeElmah()'
    End Sub

    Public parent As Elmah.ServiceProviderQueryHandler = Nothing

    Sub InitializeElmah_VB9()
        ' TODO: Create Table + Functions '
        parent = Elmah.ServiceCenter.Current
        Elmah.ServiceCenter.Current = AddressOf ElmahCallback
    End Sub

    Function ElmahCallback(objContext As Object) As System.IServiceProvider
        Dim container As New System.ComponentModel.Design.ServiceContainer(parent(objContext))
        Dim connectionSettings As System.Configuration.ConnectionStringSettings = ConfigurationManager.ConnectionStrings("FOOBAR")

        Dim strConnectionString As String = connectionSettings.ConnectionString

        Dim x As New System.Data.SqlClient.SqlConnectionStringBuilder(strConnectionString)
        x.Password = CryptStrings.DeCrypt(x.Password)

        strConnectionString = x.ConnectionString

        Dim log As Elmah.SqlErrorLog = New Elmah.SqlErrorLog(strConnectionString)
        container.AddService(GetType(Elmah.ErrorLog), log)
        Return container
    End Function

    Sub InitializeElmah()
        ' TODO: Create Table + Functions '
        Dim parent As Elmah.ServiceProviderQueryHandler = Elmah.ServiceCenter.Current
        Elmah.ServiceCenter.Current = Function(context)
              Dim container As New System.ComponentModel.Design.ServiceContainer(parent(context))
              Dim connectionSettings As System.Configuration.ConnectionStringSettings = ConfigurationManager.ConnectionStrings("Foobar")
              Dim connectionString As String = connectionSettings.ConnectionString

              Dim x As New System.Data.SqlClient.SqlConnectionStringBuilder(connectionString)

              x.Password = CryptStrings.DeCrypt(x.Password)

              connectionString = x.ConnectionString

              Dim log As Elmah.SqlErrorLog = New Elmah.SqlErrorLog(connectionString)
              container.AddService(GetType(Elmah.ErrorLog), log)

              Return container
          End Function

    End Sub


    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird beim Starten der Anwendung ausgelöst
    End Sub


    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird beim Starten der Sitzung ausgelöst
    End Sub


    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird zu Beginn jeder Anforderung ausgelöst
    End Sub


    Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird beim Versuch der Benutzerauthentifizierung ausgelöst
    End Sub


    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird bei einem Fehler ausgelöst
    End Sub


    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird beim Beenden der Sitzung ausgelöst
    End Sub


    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Wird beim Beenden der Anwendung ausgelöst
    End Sub


End Class
+3

All Articles