I am trying to intercept the errors occurred in all the sites on a web server in order to do other useful stuff, such as logging.
As a starting point, I have
- Created http module which attaches an event handler to sites' error event.
- Created test MVC project with set of action methods throwing exceptions.
- Added the created http module class dll to GAC.
Http module class library is implemented as:
I have created a class library project with only one class extending IHttpModule. Please note that this is just a simplified version.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Web;
namespace MyWebModules
{
[ComVisibleAttribute(true)]
public class MyErrorModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.Error += new EventHandler(OnError);
}
private void OnError(object sender, EventArgs e)
{
throw new Exception("Custom exception from MyErrorModule");
//TODO removed the above exception and add real life code here.
}
}
}
I added dll to GAC, by following the process below:
- Using csc.exe, I get ".netmodule" file from ".cs" code file
- ".snk" file is from the project.
- al.exe needs ".netmodule" and ".snk" to give strong name to the .dll
- With strong name, .dll is qualified to be added to GAC.
Testing using MVC test project.
Test site web.config has the following section.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="MyCustomWebModules" />
<add name="MyCustomWebModules" type="MyWebModules.MyErrorModule,MyWebModules" />
</modules>
</system.webServer>
When I access a test MVC site, I expect to see an error which says: "Custom exception from MyErrorModule"
Instead I get this error:
"Could not load file or assembly 'MisWebModules' or one of its dependencies. The system cannot find the file specified."
