Monday, February 7, 2011

Elmah - basic setup IIS 7 with MVC.Net

How to configure ELMAH.

  1. Copy elmah files to the bin folder
  2. Under section "configuration -> configSections" on your web.config, add these lines
    <sectionGroup name="elmah">
          <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
          <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
          <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
          <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
  3. Under section "configuration" on your web.config, add these lines
        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
        <security allowRemoteAccess="1" />
  4. Under section "configuration -> system.web" on your web.config, add these lines

          <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
  5. If you are running IIS 7, under section "configuration -> system.webServer" on your web.config, add these lines

    <modules runAllManagedModulesForAllRequests="true">
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
          <add name="elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
  6. If you are running MVC.Net, add this line of code to your register routes function on global.asax

    public static void RegisterRoutes(RouteCollection routes)
  7. You are now ready to view the logs. Go to

C# - Truncate String with word boundary

public String Truncate(String content, int length)
    if(!String.IsNullOrEmpty(content) && content.Length > length)
      return content.Substring(0, (content.Substring(0, length)).LastIndexOf(" "));
    return content;

Thursday, February 3, 2011

IIS 7 - Add bindings to existing site programatically

To add bindings to an existing site such as add new subdomain, all you have to do is use the AppCmd tool. You may create a bat file and have this command:

%windir%\system32\inetsrv\AppCmd set site / (yoursitename)/+[bindings.protocol='(protocoltouse)',bindingInformation='(ipaddress):(port):(hostheader)']

For example your IIS 7 is configured for with name as "markg" on site listing, and you wanted to add the subdomain "" at any IP to port 80, then you must have this command:

%windir%\system32\inetsrv\AppCmd set site / markg /+bindings.[protocol='http',bindingInformation='*']