Posted by kipusoep on jan 6, 2012 in
Zonder categorie
Remember my previous blogpost about Umbraco, ELMAH (with SQL CE 4.0) and authentication?
After some nice chats with the creator of ELMAH, Atif Aziz, I tried to implement my wishes without modifying the ELMAH core, so it’s easy to upgrade ELMAH with, for example, NuGet.
I also tried not having to create a sub-application to use ELMAH in conjunction with Basic Authentication, by using MADAM, but I just couldn’t get that to work. One of my must-haves was it should use Windows Authentication, but that’s only possible by using the default IIS implementation as far as I know.
Anyway, my journey to add my wishes to ELMAH without modifying the core went well!
After a nice chat with Atif on Twitter, I’ve created a wrapper for the ErrorLogModule and managed to remove the AUTH_PASSWORD server var and add a custom HTTP_RAW_URL server var. It’s kind of a hack and only work with SQL CE, but hey, it works fine for me!
Also I’m now using the errorLog’s applicationName attribute to set a static application name.
Here’s the code:
public class CustomErrorLogModule : ErrorLogModule
{
protected override void OnInit(HttpApplication application)
{
this.Logged += new ErrorLoggedEventHandler(CustomErrorLogModule_Logged);
base.OnInit(application);
}
protected override void LogException(Exception e, HttpContext context)
{
context.Items.Add("RAW_URL", context.Request.RawUrl);
base.LogException(e, context);
}
void CustomErrorLogModule_Logged(object sender, ErrorLoggedEventArgs args)
{
SqlServerCompactErrorLog sqlServerCompactErrorLog = args.Entry.Log as SqlServerCompactErrorLog;
if (sqlServerCompactErrorLog != null)
{
Error error = args.Entry.Error;
error.ServerVariables.Remove("AUTH_PASSWORD");
error.ServerVariables.Add("HTTP_RAW_URL", HttpContext.Current.Items["RAW_URL"].ToString());
HttpContext.Current.Items.Remove("RAW_URL");
string errorXml = ErrorXml.EncodeString(error);
const string query = @"
UPDATE ELMAH_Error SET [AllXml] = @AllXml
WHERE [ErrorId] = @ErrorId;";
using (SqlCeConnection connection = new SqlCeConnection(sqlServerCompactErrorLog.ConnectionString))
{
using (SqlCeCommand command = new SqlCeCommand(query, connection))
{
SqlCeParameterCollection parameters = command.Parameters;
parameters.Add("@AllXml", SqlDbType.NText).Value = errorXml;
parameters.Add("@ErrorId", SqlDbType.UniqueIdentifier).Value = args.Entry.Id;
command.Connection = connection;
connection.Open();
command.ExecuteNonQuery();
}
}
}
}
}
Posted by kipusoep on jan 3, 2012 in
Asp.NET,
C#.NET,
IIS,
MS SQL,
Umbraco
So this is the second blogpost I’m writing about Umbraco, ELMAH and authentication.
My previous blogpost wasn’t really good, because with forms-authentication authenticated members could also access elmah, which is not the way to go. So I’ve dropped MADAM.
This time I’ve managed to get both forms-authentication for umbraco and basic/windows-authentication for ELMAH. The trick is to create a subfolder called ‘elmah’, which we’ll convert to an application and enable basic/windows-authenticatioin for this app.
This is kind of tricky, because all web.config configurations will be inherited by the web.config for any sub-application. Also ELMAH filters the errors in the database based on the current Application string property, for example “/LM/W3SVC/24/ROOT”, but if you configure the elmah.axd handler in a sub-application, the application for the handler will be “/LM/W3SVC/24/ROOT/elmah” and you won’t see any logged errors of the main application.
To overcome this I had to make changes to ELMAH’s source code. I’ve cloned the Mercurial repo and changed some files. This way it’ll be easy to keep it up-to-date without losing my changes. There’s one extra thing I’ve changed; I’ve disabled logging the current user’s password, because of privacy reasons.
A step-by-step guide to get ELMAH and umbraco to play nicely side-by-side:
- Download this zipfile (which contains 3 other zip files)
- Unzip the file ‘1. elmah.zip‘ in the root of your project and include the whole folder in Visual Studio (so it’ll get deployed with WebDeploy)
- Unzip the file ‘2. elmah assemblies.zip‘ somewhere in your project, where all third party assemblies reside and optionally add a reference in Visual Studio (if you’d like to use Error Signalling and if you’re using something like WebDeploy)
- Unzip the file ‘3. sqlce assemblies.zip‘ in the root of your project and include both folders in Visual Studio. Inside VS select all files underneath the AMD64 and X86 folders and go to their properties. Set ‘Build action’ to ‘None’ and ‘Copy to Output Directory’ to ‘Copy if newer’ (screenshot below)

- Add the following to your web.config:
<system.data>
<DbProviderFactories>
<remove invariant=”System.Data.SqlServerCe.4.0″ />
<add name=”Microsoft SQL Server Compact Data Provider 4.0″ invariant=”System.Data.SqlServerCe.4.0″ description=”.NET Framework Data Provider for Microsoft SQL Server Compact” type=”System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91″ />
</DbProviderFactories>
</system.data>
- Add the following to your web.config (inside the runtime\assemblyBinding element):
<dependentAssembly>
<assemblyIdentity name=”System.Data.SqlServerCe” publicKeyToken=”89845dcd8080cc91″ culture=”neutral” />
<bindingRedirect oldVersion=”0.0.0.0-4.0.0.0″ newVersion=”4.0.0.0″ />
</dependentAssembly>
- If you were using ELMAH already in your project, remove any elmah.axd handlers in your web.config
- If you’ve registered none-umbraco httpModules, please add them to the removal list in ~\elmah\web.config so they won’t be loaded for the ELMAH sub-application
- Open IIS Manager and navigate to your website underneath ‘Sites’. Expand you site so you’ll see the directories underneath it and right-click the ‘elmah’ folders and select ‘Convert to Application’ and select the AppPool your site is running in
- Next make sure this newly created sub-application is selected in the tree and open up ‘Authentication’. Enable ‘Basic Authentication’ and you’re ready to start logging and reading errors!
The ELMAH page can be accessed by navigating to ‘/elmah/’ or just ‘/elmah/elmah.axd’.
The SQL CE error log database will be stored in ‘~/App_Data/’. You could monitor the size of this file as it will grow to max 1024MB.