The scenario of usage is, we wanted to log all unhandled exception thrown by our WCF service layer in a database which in our case was SQL Azure. Once done we could easily navigate to the the elmah default url to get a nice report on what errors were being generated by the application. Had it been a typical ASP.Net application, integrating ELMAH is a piece of cake. But since we were working on a Azure web deployment with SQL Azure as the backend some tweaks were necessary. Here are the things to do to get ELMAH fully functional in an Azure deployment
- Download Binaries : Start with downloading the binaries from the google source code repository here and add it to your solution project.
- Install ELMAH SQL Script: The default SQL script that is part of the download does not work directly with SQL Azure DB. I have updated the script and am adding it to the end of post.
- Implement IErrorHandler: Since WCF itself does not have a common\single point failure catching mechanism like ASP.Net (global.asax ApplicationError event) we need to implement one. The IErrorHandler approach is a perfect fit for this situation. Check this post for better understanding the approach. It involved
- Writing an implementation for IErrorHandler and IServiceBehaviour.
- Implementing the method ProvideFault which would log the error using ELMAH. This method is called everytime an error occurs.
public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
if (error != null && HttpContext.Current != null) // Notify ELMAH of the exception.
{
try
{
Elmah.ErrorSignal.FromCurrentContext().Raise(error);
}
catch (Exception loggerException)
{
//If failed to log with ELMAH trace it.
System.Diagnostics.Trace.TraceError(error.GetDetails());
System.Diagnostics.Trace.TraceError(loggerException.GetDetails());
}
}
return;
}
- Add other custom code to implement the IServiceBehaviour.
- Since we wanted config file support we also implemented a custom BehaviorExtensionElement.
- The complete implementation is available as downlad at the end of the blog post.
- Cofigure ELMAH : ELMAH by default is configure to store errors in memory which i don't think is terribly useful. To configure SQL Server integration we need to
- Add configuration section declaration for ElMAH in web.config
<configSections>
<sectionGroup name="elmah">
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
</sectionGroup>
</configSections>
- Add configuration section for ELMAH. Remember to provide a valid connection string in the settings. This connectionstring value will not be user which i will detail later.
<elmah>
<errorLog type="ELMAHIntegration.ELMAHSQLErrorLog,ELMAHIntegration" connectionString="[Dummy Connection String]"/>
</elmah>
- Note the type used in this configuration (ELMAHSQLErrorLog) is not the default type that comes with the library. I have attached code for this class too.
- Integrate ELMAH settings with Azure configuration : The problem with Connection string defined in web.config for an Azure deployment is that it is not consistent with Azure's own configuration . Azure uses a cscfg file to store application level settings whereas ELMAH is configured to pick connectionstring from web.config, What we want is to pick connectionstring defined in Azure cscfg file. This is where we use the magic of inheritance to change ELMAH behaviour in terms of selecting connection string.
- Create a class ELMAHSQLErrorLog which derives from Elmah.SQLErrorLog class and overrides its ConnectionString property.
public override string ConnectionString
{
get
{
if (RoleEnvironment.IsAvailable)
{
return RoleEnvironment.GetConfigurationSettingValue(ErrorLoggerConnectionStringConfigKey);
}
else
{
return ConfigurationManager.ConnectionStrings[ErrorLoggerConnectionStringConfigKey].ConnectionString;
}
}
}
- Reference this class in relevant section in the web.config file (See step 3)
- Voila !!! now ELMAH should start logging data in SQL Azure database.
Hope this post helps everyone integrate ELMAH in their WCF/Azure project. The code files mentioned in this walk through can be downloaded from this location.
Happy Coding,
Chandermani