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>
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
ServiceProviderQueryHandlerdelegate. Here’s the definition: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. SeeSystem.ComponentModel.Design.ServiceContainer. The service provider’sGetServicemust respond to requests for theErrorLogtype and you can then, for example, return aSqlErrorLogobject that has been initialized with a connection string manipulated at runtime. Here’s a possible implementation: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.Currentsomewhere 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.