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" />
     </sectionGroup>
  3. Under section "configuration" on your web.config, add these lines
    <elmah>
        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
        <security allowRemoteAccess="1" />
    </elmah>
    
  4. Under section "configuration -> system.web" on your web.config, add these lines

    <httpHandlers>
          <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <httpModules>
          <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
    </httpModules>
  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"/>
    </modules>
    <handlers>
          <add name="elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>
    
  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)
    {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }
    
  7. You are now ready to view the logs. Go to http://yoursitedomain.com/elmah.axd

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 /site.name: (yoursitename)/+[bindings.protocol='(protocoltouse)',bindingInformation='(ipaddress):(port):(hostheader)']

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

%windir%\system32\inetsrv\AppCmd set site /site.name: markg /+bindings.[protocol='http',bindingInformation='*:80:me.markglibres.com']