使用加密连接字符串配置 ELMAH 和 SQL Server 日志记录

发布于 2024-11-07 01:01:41 字数 1389 浏览 0 评论 0原文

我正在尝试使用 SQL Server 2008 R2 在 ASP.NET 4 应用程序中配置 ELMAH 错误日志记录。有什么方法可以告诉 ELMAH 在我们提供的连接字符串上调用我们的内部解密函数吗?我需要修改 ELMAH 源并重建吗?

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

I'm attempting to configure ELMAH error logging in an ASP.NET 4 application using SQL Server 2008 R2. Is there any way I can tell ELMAH to call our in-house decryption function on the connection string we provide it? Do I need to modify the ELMAH source 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2024-11-14 01:01:41

您不能只是告诉 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; 
  } ;

它捕获当前的服务点并安装您自己的服务点。创建的 lambda/委托在无法传递服务请求时将其传递到捕获的服务点
直接满足它,从而创建一条链。您可以通过在应用程序初始化期间的某个位置设置 ServiceCenter.Current 来告诉 ELMAH 您的实现情况,这就是上面的代码需要放置的位置。

请记住,这是一个非常简单的实现,但它应该足以帮助您入门并在需要时进行优化。

在此1.2 中的新增内容之前,执行某些操作的唯一方法类似的需要子类化和其他体操,但仍然产生了部分结果。现在您只需实现一个方法并将其交给 ELMAH,该方法只需根据对象的服务类型响应 ELMAH 对对象的查询。

You can't just tell ELMAH to do something with your connection string. What you can do, however, is tell ELMAH to call you back when it needs an ErrorLog, giving you more control at run-time. You can then read the encrypted connection string, decrypt it with your in-house function and return an SqlErrorLog initialized with it.

To do this, you need to provide a method that is compatible with
the ServiceProviderQueryHandler delegate. Here's the definition:

public delegate IServiceProvider ServiceProviderQueryHandler(object context); 

The implementation of the method must return an instance of an object that implements IServiceProvider. If you don't feel like writing one yourself to start with, you can get one for free from the .NET Framework. See System.ComponentModel.Design.ServiceContainer. The service provider's GetService must respond to requests for the ErrorLog type and you can then, for example, return a SqlErrorLog object that has been initialized with a connection string manipulated at runtime. Here's a possible implementation:

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; 
  } ;

This captures the current service point and installs your own instead. The lambda/delegate created pass on service requests to the captured service point when it cannot
satisfy it directly, thus creating a chain. You tell ELMAH about your implementation by setting ServiceCenter.Current somewhere during the initialization of your application so that's where the above code will need to sit.

Bear in mind that this is a very simple implementation but it should be good enough to get you started and optimize later if needed.

Prior to this addition in 1.2, the only way to do something similar required subclassing and other gymnastics and still yielded partial results. Now you just need to implement a method and hand it over to ELMAH and which simply responds to queries from ELMAH for objects based on their service type.

假面具 2024-11-14 01:01:41

作为 Atif Aziz 帖子的补充,这里是 VB.NET 版本(InitializeElmah_VB9 用于 .NET 2.0 版本 [不带 lamdda],InitializeElmah 用于 VB.NET for .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

As an addition to the post of Atif Aziz, here the VB.NET version (InitializeElmah_VB9 for the .NET 2.0 version [without lamdda], InitializeElmah for VB.NET for .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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文