Thursday, March 22, 2012

Using ELMAH with Custom Errors turned on

This article specifically addresses handling uncaught exceptions in your web application. ELMAH can be plugged in pretty quickly with almost all of your changes happening in your web.config and global.asax.cs files.

Here is a slimmed down web.config file. When a 404 error occurs, it will not be logged to Elmah and the user will be redirected to a custom "Not Found" page. All other exceptions will be logged by Elmah and the user is redirected to a custom error page. To view the list of logged exceptions, the url is http://(yoursite)/elmah.axd (note that elmah.axd does not physically exist anywhere). Also, IIS will deny users that are not part of the "Developers" group to see the log page.
<?xml version="1.0"?>
<configuration>
  
  <configSections>
    <sectionGroup name="elmah">
      <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
      <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
      <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
    </sectionGroup>
  </configSections>

  <elmah>
    <security allowRemoteAccess="yes" />
    <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="Elmah" />
    <errorFilter>
      <test>
        <or>
          <equal binding="HttpStatusCode" value="404" type="Int32" />
          <regex binding="BaseException.Message" pattern="The file '/[^']+' does not exist" />
        </or>
      </test>
    </errorFilter>
  </elmah>
  
  <system.web>
    <customErrors mode="On" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="/Home/NotFound" />
    </customErrors>
    <httpModules>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
      <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
    </httpModules>
      <httpHandlers>
        <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
      </httpHandlers>
  </system.web>

  <location path="elmah.axd">
    <system.web>
      <authorization>
        <allow roles="Developers"/>
        <deny users="*" />
      </authorization>
    </system.web>
  </location>

</configuration>


In global.asax.cs I commented out the filter for the HandleErrorAttribute so that Elmah would handle the exceptions.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    // Commented out so that web.config can have customErrors="true" and still let Elmah handle exceptions
    //filters.Add(new HandleErrorAttribute());
}


So now when I bring up my elmah.axd page I don't see 404 errors when the browser is looking for the favicon.ico file which I was seeing a lot of previously.

Friday, March 16, 2012

Setting up a Color Picker for Sublime Text 2 (on Windows)

Sublime Text 2. You already know so I don't need to say it. Anywho, in Visual Studio one of my favorite extensions is devColor for picking hex colors in my stylesheets. In Sublime Text 2 you need to install one, it's not there by default sadly.

Here are instructions on how to do it step by step.
  1. Fire up Sublime Text 2
  2. Press CTRL+` (the key above the TAB key)
  3. You'll see a console window take up the bottom portion of the window. Paste the following block of code there
  4. import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
    (This will install the Sublime Text 2 Package Control system which is similar to Gem, NuGet, NPM, etc.)
  5. Close Sublime Text and reopen it
  6. Press CTRL+SHIFT+P to open the Command Palette
  7. Start typing in "Package" and select the Package Control: Install Package option and hit Enter
  8. The dialog changes to a list of available packages. Type in "Color" and select ColorPicker
    (Credit goes out to Weslley Honorato for creating the package)
  9. No restart of Sublime required this time around. It's ready to use
  10. Open a stylesheet or an area where you want to insert a hex color
  11. Press CTRL+SHIFT+C to bring up the color selection dialog
  12. That's it!