You can implement a custom HttpModule in a separate class library or web app project so it compiles into your custom dll. I would use a post build event to copy your dll up to the Web/bin folder.
You can plug it in by adding it in the Web.config <system.web><httpModules> section for IIS 6 and <system.webServer><modules> section in IIS 7 Integrated mode.
Inside the module you can hook into the application events. Note that the order in which the configured modules are listed in Web.config is the order they will execute, this can be important depending on what you are doing.
There are examples of custom HttpModules in mojoPortal source code, like Web/Components/BannedIPBlockingHttpModule.cs, you can see how it is plugged in by configuration in Web.config
Hope it helps,
Joe