<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Development Stuffs</title>
	<atom:link href="http://devstuffs.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://devstuffs.wordpress.com</link>
	<description>gathering useful information</description>
	<lastBuildDate>Thu, 12 Jan 2012 20:49:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='devstuffs.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Development Stuffs</title>
		<link>http://devstuffs.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://devstuffs.wordpress.com/osd.xml" title="Development Stuffs" />
	<atom:link rel='hub' href='http://devstuffs.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Creating your own Pattern Layout Converter for Log4net</title>
		<link>http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter-for-log4net/</link>
		<comments>http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter-for-log4net/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 20:48:06 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=239</guid>
		<description><![CDATA[Pattern Layout Converter is the way you tell log4net how to log something that it doesn&#8217;t know yet. You first create your class that will get the information, for instance, this class will is to log the machine name: Then, you edit your config file adding the converter you just created: In the code above, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=239&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Pattern Layout Converter is the way you tell log4net how to log something that it doesn&#8217;t know yet.</p>
<p>You first create your class that will get the information, for instance, this class will is to log the machine name:</p>
<p><pre class="brush: csharp;">
using System;
using System.IO;
using log4net.Layout.Pattern;

namespace MyApplication.Logging
{
    public class MachinePatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            writer.Write(Environment.MachineName);            
        }
    }
}
</pre></p>
<p>Then, you edit your config file adding the converter you just created:</p>
<p><pre class="brush: xml; highlight: [7,8,9,10,11];">
  &lt;log4net&gt;
    &lt;appender name=&quot;Application&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
      &lt;file value=&quot;log\application.log&quot; /&gt;
      &lt;appendToFile value=&quot;true&quot; /&gt;
      &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;        
        &lt;converter&gt;
          &lt;name value=&quot;machine&quot; /&gt;
          &lt;type value=&quot;MyApplication.Logging..MachinePatternConverter&quot; /&gt;
        &lt;/converter&gt;        
        &lt;conversionPattern value=&quot;%date [%thread] %level %logger %machine - %message e:%exception%newline %newline&quot; /&gt;
      &lt;/layout&gt;
    &lt;/appender&gt;
&lt;!-- lines removed for brevity --&gt;
</pre></p>
<p>In the code above, we added our new converter and named it <strong>&#8220;machine&#8221;</strong>. Then, In the <strong>conversionPattern</strong> line, we can use it wherever we want by adding the <strong><code>%machine</code></strong> variable.</p>
<p>Here are others converters you may want to use:</p>
<p><pre class="brush: csharp;">
    public class IPPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)
            {
                writer.Write(HttpContext.Current.Request.UserHostAddress);
            }
        }
    }
</pre></p>
<p><pre class="brush: csharp;">
    public class UrlPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)            
                writer.Write(HttpContext.Current.Request.Url.AbsoluteUri);
        }
    }
</pre></p>
<p><pre class="brush: csharp;">
    public class UserAgentPatternConverter : PatternLayoutConverter
    {
        protected override void Convert(TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (HttpContext.Current != null)
            {
                writer.Write(HttpContext.Current.Request.UserAgent);
            }
        }
    }
</pre></p>
<p>Now, applying all converter in our config file:</p>
<p><pre class="brush: xml;">
  &lt;log4net&gt;
    &lt;appender name=&quot;Application&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
      &lt;file value=&quot;log\application.log&quot; /&gt;
      &lt;appendToFile value=&quot;true&quot; /&gt;
      &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;converter&gt;
          &lt;name value=&quot;url&quot; /&gt;
          &lt;type value=&quot;MyApplication.Logging.UrlPatternConverter&quot; /&gt;
        &lt;/converter&gt;
        &lt;converter&gt;
          &lt;name value=&quot;ip&quot; /&gt;
          &lt;type value=&quot;MyApplication.Logging.IPPatternConverter&quot; /&gt;
        &lt;/converter&gt;
        &lt;converter&gt;
          &lt;name value=&quot;machine&quot; /&gt;
          &lt;type value=&quot;MyApplication.Logging.MachinePatternConverter&quot; /&gt;
        &lt;/converter&gt;
        &lt;converter&gt;
          &lt;name value=&quot;userAgent&quot; /&gt;
          &lt;type value=&quot;MyApplication.Logging.UserAgentPatternConverter&quot; /&gt;
        &lt;/converter&gt;
        &lt;conversionPattern value=&quot;%date [%thread] %level %logger %url %ip %machine %userAgent - %message e:%exception%newline %newline&quot; /&gt;
      &lt;/layout&gt;
    &lt;/appender&gt;
</pre></p>
<p>Do you have any interest Pattern Layout Converter you use in your application?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/239/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=239&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2012/01/12/creating-your-own-pattern-layout-converter-for-log4net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>Logging to multiple files &#8211; Log4Net</title>
		<link>http://devstuffs.wordpress.com/2011/12/08/logging-to-multiple-files-log4net/</link>
		<comments>http://devstuffs.wordpress.com/2011/12/08/logging-to-multiple-files-log4net/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 15:57:27 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=220</guid>
		<description><![CDATA[I am going to explain how to log using Log4Net and how to separate the different types of logs from your application into different output files. The main focus of this post is not explain how log4net works in detail, however if your are interesting about learning more about it I recommend this article http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=220&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am going to explain how to log using Log4Net and how to separate the different types of logs from your application into different output files. The main focus of this post is not explain how log4net works in detail, however if your are interesting about learning more about it I recommend this article <a href="http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx" target="_blank">http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx</a>.</p>
<p>The purpose here is to separate the log by source, for instance, caching log in one file, queries in another file, application log in another one, and also a single file for logging errors from all sources, this way we can find errors easier. The errors will still be logged in the library&#8217;s log though, because we may want to focus on that library specifically.</p>
<p>Take a look at this configuration file and will explain where the separation comes up later:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;log4net&gt;
  &lt;appender name=&quot;Application&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
    &lt;file value=&quot;log\application.log&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level %logger [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;appender name=&quot;ErrorLog&quot; type=&quot;log4net.Appender.RollingFileAppender&quot;&gt;
    &lt;file value=&quot;log\error.log&quot; /&gt;
    &lt;rollingStyle value=&quot;Date&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;datePattern value=&quot;ddMMyyyy&quot; /&gt;
    &lt;maxSizeRollBackups value=&quot;10&quot; /&gt;
    &lt;filter type=&quot;log4net.Filter.LevelRangeFilter&quot;&gt;
      &lt;levelMin value=&quot;ERROR&quot;/&gt;
    &lt;/filter&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level %logger [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;

  &lt;/appender&gt;

  &lt;appender name=&quot;AdoNetAppender&quot; type=&quot;log4net.Appender.AdoNetAppender&quot;&gt;
    &lt;bufferSize value=&quot;1&quot; /&gt;
    &lt;connectionType value=&quot;System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&quot; /&gt;
    &lt;connectionString value=&quot;data source=127.0.0.1;initial catalog=Application_Log_Database;integrated security=false;persist security info=True;User ID=abc123;Password=abc123&quot; /&gt;
    &lt;!-- AppId parameter comes from the table Applications --&gt;
    &lt;commandText value=&quot;INSERT INTO application_log ([date],[thread],[level],[logger], [message], [exception]) VALUES (@log_date,@thread,@log_level,@logger, @message,@exception)&quot; /&gt;

    &lt;filter type=&quot;log4net.Filter.LevelRangeFilter&quot;&gt;
      &lt;levelMin value=&quot;ERROR&quot;/&gt;
      &lt;levelMax value=&quot;FATAL&quot;/&gt;
    &lt;/filter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@log_date&quot; /&gt;
      &lt;dbType value=&quot;DateTime&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.RawTimeStampLayout&quot; /&gt;
    &lt;/parameter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@thread&quot; /&gt;
      &lt;dbType value=&quot;String&quot; /&gt;
      &lt;size value=&quot;255&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%thread&quot; /&gt;
      &lt;/layout&gt;
    &lt;/parameter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@log_level&quot; /&gt;
      &lt;dbType value=&quot;String&quot; /&gt;
      &lt;size value=&quot;50&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%level&quot; /&gt;
      &lt;/layout&gt;
    &lt;/parameter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@logger&quot; /&gt;
      &lt;dbType value=&quot;String&quot; /&gt;
      &lt;size value=&quot;255&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%logger&quot; /&gt;
      &lt;/layout&gt;
    &lt;/parameter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@message&quot; /&gt;
      &lt;dbType value=&quot;String&quot; /&gt;
      &lt;size value=&quot;4000&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%message&quot; /&gt;
      &lt;/layout&gt;
    &lt;/parameter&gt;

    &lt;parameter&gt;
      &lt;parameterName value=&quot;@exception&quot; /&gt;
      &lt;dbType value=&quot;String&quot; /&gt;
      &lt;size value=&quot;4000&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.ExceptionLayout&quot; /&gt;
    &lt;/parameter&gt;    

  &lt;/appender&gt;

  &lt;appender name=&quot;OpenRasta&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
    &lt;file value=&quot;log\openrasta.log&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level %logger [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;appender name=&quot;Messaging&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
    &lt;file value=&quot;log\messaging.log&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level %logger [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;appender name=&quot;Queries&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
    &lt;file value=&quot;log\queries.log&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;appender name=&quot;Memcached&quot; type=&quot;log4net.Appender.FileAppender&quot;&gt;
    &lt;file value=&quot;log\memcached.log&quot; /&gt;
    &lt;appendToFile value=&quot;true&quot; /&gt;
    &lt;maximumFileSize value=&quot;1024KB&quot; /&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%date [%thread] %-5level [%property{NDC}] - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;root&gt;
    &lt;level value=&quot;All&quot; /&gt;
    &lt;appender-ref ref=&quot;ErrorLog&quot; /&gt;
    &lt;appender-ref ref=&quot;AdoNetAppender&quot; /&gt;
  &lt;/root&gt;

  &lt;logger name=&quot;Log4NetExample&quot;&gt;
    &lt;level value=&quot;INFO&quot; /&gt;
    &lt;appender-ref ref=&quot;Application&quot; /&gt;
  &lt;/logger&gt;

  &lt;logger name=&quot;OpenRasta&quot;&gt;
    &lt;level value=&quot;INFO&quot; /&gt;
    &lt;appender-ref ref=&quot;OpenRasta&quot; /&gt;
  &lt;/logger&gt;

  &lt;logger name=&quot;Spring&quot;&gt;
    &lt;level value=&quot;WARN&quot; /&gt;
    &lt;appender-ref ref=&quot;Messaging&quot; /&gt;
  &lt;/logger&gt;
  &lt;logger name=&quot;NServiceBus&quot;&gt;
    &lt;level value=&quot;WARN&quot; /&gt;
    &lt;appender-ref ref=&quot;Messaging&quot; /&gt;
  &lt;/logger&gt;

  &lt;logger name=&quot;System.Data.Linq&quot;&gt;
    &lt;level value=&quot;DEBUG&quot; /&gt;
    &lt;appender-ref ref=&quot;Queries&quot; /&gt;
  &lt;/logger&gt;

  &lt;logger name=&quot;Enyim.Caching.Memcached&quot;&gt;
    &lt;level value=&quot;WARN&quot; /&gt;
    &lt;appender-ref ref=&quot;Memcached&quot; /&gt;
  &lt;/logger&gt;

&lt;/log4net&gt;
</pre></p>
<p>Don&#8217;t be scared! It may look big, but that is because I&#8217;m showing a configuration with a lot of libraries and the AdoNetAppender is a little big too. What this configuration is doing is creating the <strong>Appenders</strong>, setting the <strong>root</strong> section, and then setting the level things will be logged with the <strong>loggers</strong> section.</p>
<p>The Appenders are most of them logging into text files, and one is logging into the database. In order to have the errors from all sources, notice we are adding the <strong>ErrorLog</strong> appender and the <strong>AdoNetAppender</strong> in the <em>root </em> section. This is because all the loggers must inherit those two. All the loggers will log their errors in a text file and in the database:</p>
<p><pre class="brush: xml;">
&lt;root&gt;
	&lt;level value=&quot;All&quot; /&gt;
	&lt;appender-ref ref=&quot;ErrorLog&quot; /&gt;
	&lt;appender-ref ref=&quot;AdoNetAppender&quot; /&gt;
&lt;/root&gt;
</pre></p>
<p>The <em>root&#8217;s</em> log level is set to &#8220;<strong>All</strong>&#8220;, so if you don&#8217;t specify any level to a <em>logger</em>, it will inherit from the <em>root</em> and its value will be All as well.</p>
<p>If you look at those two appenders we are filtering their minimum level types as <strong>ERROR</strong> in the appender itself.</p>
<p><pre class="brush: xml;">
&lt;appender name=&quot;ErrorLog&quot; type=&quot;log4net.Appender.RollingFileAppender&quot;&gt;    
	&lt;filter type=&quot;log4net.Filter.LevelRangeFilter&quot;&gt;
	  &lt;levelMin value=&quot;ERROR&quot;/&gt;
	&lt;/filter&gt;
...
</pre></p>
<p>This is how we carry the error loggers along with the other loggers but logging only the errors. The libraries are filtered by their namespace, for instance Enyim.Caching.Memcached, and each library has its own logger set with a specific level of logging.</p>
<p>Log4net is a well known library and most of other libraries support it by default. For those libraries that doesn&#8217;t, for example, LinqToSql, a quick search on Google can give you the code to make it supported.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/220/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/220/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/220/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=220&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2011/12/08/logging-to-multiple-files-log4net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>How to clear the DataContext cache on LinqToSql</title>
		<link>http://devstuffs.wordpress.com/2011/11/23/how-to-clear-the-datacontext-cache-on-linqtosql/</link>
		<comments>http://devstuffs.wordpress.com/2011/11/23/how-to-clear-the-datacontext-cache-on-linqtosql/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 17:53:35 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[linqtosql]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=215</guid>
		<description><![CDATA[When you get data using LinqToSql and change something in the object, you expect that calling get object again will invalidate the cache and return the new objects, but it doesn&#8217;t! LinqToSql will return a reference of the object, what means, it will return the same object you had before. The code below force LinqToSql [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=215&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When you get data using LinqToSql and change something in the object, you expect that calling get object again will invalidate the cache and return the new objects, but it doesn&#8217;t! LinqToSql will return a reference of the object, what means, it will return the same object you had before.</p>
<p>The code below force LinqToSql to get new object(s), not the one(s) it has in memory:</p>
<p><pre class="brush: csharp;">
public Order Get(int id)
{
	var order = Context.Orders.SingleOrDefault(o =&gt; o.orderId == id);
	
	if (order != null)
		// invalidate cache
		Context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, order);

	return order;
}
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/215/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=215&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2011/11/23/how-to-clear-the-datacontext-cache-on-linqtosql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>Working with Configuration Files &#8211; List Items</title>
		<link>http://devstuffs.wordpress.com/2011/05/29/working-with-configuration-files-list-items/</link>
		<comments>http://devstuffs.wordpress.com/2011/05/29/working-with-configuration-files-list-items/#comments</comments>
		<pubDate>Sun, 29 May 2011 12:00:06 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[deserialization]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=211</guid>
		<description><![CDATA[Continuing my posts about configuration files, now I&#8217;m going to show you how to work with a list of items. Here is the xml: Is basically the same xml file I was using in the previous post but now hosts is a list and not only a single entry. So, let&#8217;s see how we handle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=211&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Continuing my posts about configuration files, now I&#8217;m going to show you how to work with a list of items.</p>
<p>Here is the xml:<br />
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;main&gt;
  &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;name&gt;Example text name&lt;/name&gt;
  &lt;hosts&gt;
    &lt;host ip=&quot;127.0.0.1&quot; port=&quot;80&quot; username=&quot;test&quot; password=&quot;pass&quot; /&gt;
    &lt;host ip=&quot;172.168.0.1&quot; port=&quot;80&quot; username=&quot;test2&quot; password=&quot;pass2&quot; /&gt;
  &lt;/hosts&gt;
&lt;/main&gt;
</pre></p>
<p>Is basically the same xml file I was using in the previous post but now hosts is a list and not only a single entry.</p>
<p>So, let&#8217;s see how we handle it in our code:<br />
<pre class="brush: csharp;">
[XmlRoot(&quot;main&quot;)]
public class ModelExampleConfiguration
{
    public ModelExampleConfiguration()
    {
        this.Hosts = new List&lt;HostConfiguration&gt;();
    }
        
    [XmlElement(&quot;enabled&quot;)]
    public bool Enabled { get; set; }

    [XmlElement(&quot;name&quot;)]
    public string Name { get; set; }

    [XmlArray(&quot;hosts&quot;)]
    [XmlArrayItem(&quot;host&quot;)]
    public List&lt;HostConfiguration&gt; Hosts { get; set; }
}
</pre></p>
<p>This is important! Note that the list had to be instantiated in the constructor so that the .NET can add the items into the list. Also, it was used to new attributes: XmlArray and XmlArrayItem. The first one defines the name of the list, and the second the name of the items of the list.</p>
<p>You might also need a list where you don&#8217;t have a parent item. Let&#8217;s see an example:<br />
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;main&gt;
  &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;name&gt;Example text name&lt;/name&gt;
  &lt;hosts&gt;
    &lt;host ip=&quot;127.0.0.1&quot; port=&quot;80&quot; username=&quot;test&quot; password=&quot;pass&quot; /&gt;
    &lt;host ip=&quot;172.168.0.1&quot; port=&quot;80&quot; username=&quot;test2&quot; password=&quot;pass2&quot; /&gt;
  &lt;/hosts&gt;
  &lt;users groupName=&quot;admin&quot;&gt;
    &lt;user name=&quot;user1&quot; /&gt;
    &lt;user name=&quot;user2&quot; /&gt;
  &lt;/users&gt;
  &lt;users groupName=&quot;guests&quot;&gt;
    &lt;user name=&quot;user3&quot; /&gt;
    &lt;user name=&quot;user4&quot; /&gt;
  &lt;/users&gt;
&lt;/main&gt;
</pre></p>
<p>I add those two groups of users and each group of user has a list of users. Our full code then will look like:<br />
<pre class="brush: csharp;">
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot(&quot;main&quot;)]
    public class ModelExampleConfiguration
    {
        public ModelExampleConfiguration()
        {
            this.Hosts = new List&lt;HostConfiguration&gt;();
        }
        
        [XmlElement(&quot;enabled&quot;)]
        public bool Enabled { get; set; }

        [XmlElement(&quot;name&quot;)]
        public string Name { get; set; }

        [XmlArray(&quot;hosts&quot;)]
        [XmlArrayItem(&quot;host&quot;)]
        public List&lt;HostConfiguration&gt; Hosts { get; set; }

        [XmlElement(&quot;users&quot;)]
        public UserGroupConfiguration[] UserGroup { get; set; }
    }

    public class HostConfiguration
    {
        [XmlAttribute(&quot;ip&quot;)]
        public string IP { get; set; }

        [XmlAttribute(&quot;port&quot;)]
        public int Port { get; set; }

        [XmlAttribute(&quot;username&quot;)]
        public string Username { get; set; }

        [XmlAttribute(&quot;password&quot;)]
        public string Password { get; set; }
    }

    public class UserGroupConfiguration
    {
        [XmlAttribute(&quot;groupName&quot;)]
        public string GroupName { get; set; }

        [XmlElement(&quot;user&quot;)]
        public UserConfiguration[] Users { get; set; }
    }

    public class UserConfiguration
    {
        [XmlAttribute(&quot;name&quot;)]
        public string Name { get; set; }
    }
}
</pre></p>
<p>See that now I used an array instead of a generic list. I could have used the list as well, but I think that way is simpler.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=211&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2011/05/29/working-with-configuration-files-list-items/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>Working with Configuration Files &#8211; Quick Start</title>
		<link>http://devstuffs.wordpress.com/2011/05/26/working-with-configuration-files-quick-start/</link>
		<comments>http://devstuffs.wordpress.com/2011/05/26/working-with-configuration-files-quick-start/#comments</comments>
		<pubDate>Thu, 26 May 2011 19:55:02 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[deserialization]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=205</guid>
		<description><![CDATA[I&#8217;m currently working in a project where one need is to create easy customizable configuration files. The interest part comes when we need to read these configuration files and parsing them to .NET classes. As I myself had to look up how some attributes work, I decided to share my experience creating those files, so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=205&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working in a project where one need is to create easy customizable configuration files. The interest part comes when we need to read these configuration files and parsing them to .NET classes.</p>
<p>As I myself had to look up how some attributes work, I decided to share my experience creating those files, so this might help someone else going through the same problems.</p>
<p>To start off, let&#8217;s have a simple xml:</p>
<p><pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;main&gt;
  &lt;enabled&gt;true&lt;/enabled&gt;
  &lt;name&gt;Example text name&lt;/name&gt;
  &lt;host ip=&quot;127.0.01&quot; port=&quot;80&quot; username=&quot;test&quot; password=&quot;pass&quot; /&gt;
&lt;/main&gt;
</pre></p>
<p>Now the our model class:<br />
<pre class="brush: csharp;">
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot(&quot;main&quot;)]
    public class ModelExampleConfiguration
    {
        [XmlElement(&quot;enabled&quot;)]
        public bool Enabled { get; set; }

        [XmlElement(&quot;name&quot;)]
        public string Name { get; set; }

        [XmlElement(&quot;host&quot;)]
        public HostConfiguration Host { get; set; }
    }

    public class HostConfiguration
    {
        [XmlAttribute(&quot;ip&quot;)]
        public string IP { get; set; }

        [XmlAttribute(&quot;port&quot;)]
        public int Port { get; set; }

        [XmlAttribute(&quot;username&quot;)]
        public string Username { get; set; }

        [XmlAttribute(&quot;password&quot;)]
        public string Password { get; set; }
    }
}
</pre></p>
<p>Note: If you have your class and property names exactly like your config file then you don&#8217;t need to specify the attributes, but I just prefer to always use them, this way I can name my classes however I want.</p>
<p>Notice I&#8217;m handling both elements and attributes, using XmlElement and XmlAttributes.</p>
<p>Now, the code to parse the xml into a model class:<br />
<pre class="brush: csharp;">
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = XmlReader.Create(&quot;Example.xml&quot;);

            var factory = new XmlSerializerFactory();
            var serializer = factory.CreateSerializer(typeof(ModelExampleConfiguration));
            var config = serializer.Deserialize(reader) as ModelExampleConfiguration;
        }
    }
}

// config.Enabled will output true
// config.Host.Username will output test
</pre></p>
<p>In the next post I&#8217;ll show how to deserialize a list / array of items.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/205/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/205/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/205/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=205&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2011/05/26/working-with-configuration-files-quick-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>21 Things I learned from the jQuery Source</title>
		<link>http://devstuffs.wordpress.com/2011/03/11/21-things-i-learned-from-the-jquery-source/</link>
		<comments>http://devstuffs.wordpress.com/2011/03/11/21-things-i-learned-from-the-jquery-source/#comments</comments>
		<pubDate>Fri, 11 Mar 2011 15:28:58 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[learn]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=195</guid>
		<description><![CDATA[I know I&#8217;ve been posting only videos lately but this one deserves to be here too and I really don&#8217;t think videos are bad. Instead of reading and just listen to your mind you can watch the person talking, acting and see their personality. In this two videos, the astonish Paul Irish tells us 10 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=195&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I know I&#8217;ve been posting only videos lately but this one deserves to be here too and I really don&#8217;t think videos are bad. Instead of reading and just listen to your mind you can watch the person talking, acting and see their personality.</p>
<p>In this two videos, the astonish <a title="http://paulirish.com" href="http://paulirish.com" target="_blank">Paul Irish</a> tells us 10 things he learned from the jQuery Source and then 11 more things he learned from it. At first, when we look at the jQuery Source it looks scary and huge, but Paul Irish diggs in it and shows us that we can learn many things straight from the source, instead of looking for docs on the internet.</p>
<p>I personally already did this long time ago and I recommend to do it. It&#8217;s really like a wiki of javascript codes.</p>
<div class='embed-vimeo' style='text-align:center;'><iframe src='http://player.vimeo.com/video/12529436' width='600' height='340' frameborder='0'></iframe></div>
<p>and here is the other</p>
<div class='embed-vimeo' style='text-align:center;'><iframe src='http://player.vimeo.com/video/18901514' width='600' height='340' frameborder='0'></iframe></div>
<p><a title="11 more things i learned from the jQuery souce" href="http://paulirish.com/2011/11-more-things-i-learned-from-the-jquery-source/" target="_blank">Click here to see the post in his blog</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=195&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2011/03/11/21-things-i-learned-from-the-jquery-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>jQuery Templates, Data Link, and Globalization Accepted as Official jQuery Plugins</title>
		<link>http://devstuffs.wordpress.com/2010/12/15/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins/</link>
		<comments>http://devstuffs.wordpress.com/2010/12/15/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins/#comments</comments>
		<pubDate>Wed, 15 Dec 2010 20:17:00 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[data linking]]></category>
		<category><![CDATA[globalization]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">https://devstuffs.wordpress.com/2010/12/15/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins/</guid>
		<description><![CDATA[Stephen Walther from the ASP.NET Team joins James Senior (@jsenior) on Web Camps TV to discuss an exciting announcement: Microsoft is making their first contributions to the jQuery open source project! In the past 6 months, they&#8217;ve been busy working on three features: Templating Data-Linking Globalization In this video, they show demos of the contributions [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=181&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Stephen Walther from the ASP.NET Team joins James Senior (<a href="http://www.twitter.com/jsenior">@jsenior</a>) on Web Camps TV to discuss an exciting announcement: Microsoft is making their first contributions to the jQuery open source project!<br />
In the past 6 months, they&#8217;ve been busy working on three features:</p>
<ol>
<li>Templating</li>
<li>Data-Linking</li>
<li>Globalization</li>
</ol>
<p>In this video, they show demos of the contributions in action, take a look:</p>
<p><a href="http://channel9.msdn.com/Shows/Web+Camps+TV/Web-Camps-TV-5-Microsoft-Commits-Code-to-jQuery" target="_blank"><img class="alignnone size-full wp-image-190" title="video_thumb_jquery_plugins" src="http://devstuffs.files.wordpress.com/2010/12/video_thumb_jquery_plugins.jpg?w=640" alt="Web Camps TV #6 - Microsoft Commits Code to jQuery!"   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/181/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=181&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2010/12/15/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>

		<media:content url="http://devstuffs.files.wordpress.com/2010/12/video_thumb_jquery_plugins.jpg" medium="image">
			<media:title type="html">video_thumb_jquery_plugins</media:title>
		</media:content>
	</item>
		<item>
		<title>How to be a better developer through social media</title>
		<link>http://devstuffs.wordpress.com/2010/12/14/how-to-be-a-better-developer-through-social-media/</link>
		<comments>http://devstuffs.wordpress.com/2010/12/14/how-to-be-a-better-developer-through-social-media/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 20:58:00 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[Videos]]></category>
		<category><![CDATA[blogs]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[social network]]></category>

		<guid isPermaLink="false">https://devstuffs.wordpress.com/2010/12/13/how-to-be-a-better-developer-through-social-media/</guid>
		<description><![CDATA[Great speech from Scott Hanselman that shows us how we can be better developers using social networking and he proposes that every developer needs a blog. &#160; Scott gave an advice I really liked it. He says &#34;Remember, we are all immature!&#34;. If you think like that you will feel more confident to write something [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=178&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Great speech from <a href="http://www.hanselman.com/blog/" target="_blank">Scott Hanselman</a> that shows us how we can be better developers using social networking and he proposes that every developer needs a blog.</p>
<p>&#160;<a href="http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-Social-Networking-for-Developers-Part-1-Every-Developer-Needs-a-Blog/" target="_blank"><img title="How to be a better developer through social media" alt="How to be a better developer through social media" src="http://www.hanselman.com/blog/content/binary/blogsuck1.JPG" /></a> </p>
<p>Scott gave an advice I really liked it. He says &quot;Remember, we are all immature!&quot;. If you think like that you will feel more confident to write something out there. You don&#8217;t need to feel afraid of not writing the best code. Just go and say: &quot;Here it is. Here&#8217;s my code, I&#8217;m learning&quot;. Let it roll.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=178&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2010/12/14/how-to-be-a-better-developer-through-social-media/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>

		<media:content url="http://www.hanselman.com/blog/content/binary/blogsuck1.JPG" medium="image">
			<media:title type="html">How to be a better developer through social media</media:title>
		</media:content>
	</item>
		<item>
		<title>What motivates developers?</title>
		<link>http://devstuffs.wordpress.com/2010/12/13/what-motivates-developers/</link>
		<comments>http://devstuffs.wordpress.com/2010/12/13/what-motivates-developers/#comments</comments>
		<pubDate>Mon, 13 Dec 2010 17:27:59 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[Videos]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[motivation]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">https://devstuffs.wordpress.com/2010/12/13/what-motivates-developers/</guid>
		<description><![CDATA[This is an animation like video that talks about how people feel rewarded and what motivates them to do a better job at work. It also shows how it relates to open source.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=177&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is an animation like video that talks about how people feel rewarded and what motivates them to do a better job at work. It also shows how it relates to open source.</p>
<span style="text-align:center; display: block;"><a href="http://devstuffs.wordpress.com/2010/12/13/what-motivates-developers/"><img src="http://img.youtube.com/vi/u6XAPnuFjJc/2.jpg" alt="" /></a></span>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/177/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/177/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/177/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=177&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2010/12/13/what-motivates-developers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>
	</item>
		<item>
		<title>How to use CustomErrors in ASP.NET MVC 2</title>
		<link>http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/</link>
		<comments>http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/#comments</comments>
		<pubDate>Sun, 12 Dec 2010 18:42:48 +0000</pubDate>
		<dc:creator>devstuffs</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[403]]></category>
		<category><![CDATA[404]]></category>
		<category><![CDATA[access denied]]></category>
		<category><![CDATA[action filters]]></category>
		<category><![CDATA[customErrors]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[HandleError]]></category>
		<category><![CDATA[handling errors]]></category>
		<category><![CDATA[not found]]></category>

		<guid isPermaLink="false">http://devstuffs.wordpress.com/?p=157</guid>
		<description><![CDATA[First, let&#8217;s see how our Web.config file will look like and after that I will show you how to create the pages. I’m going to show the steps to make it work for those three types of errors and also explain you about the HandleError attribute and how it handles the information specified in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=157&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>First, let&#8217;s see how our Web.config file will look like and after that I will show you how to create the pages.</p>
<p><pre class="brush: xml;">
&lt;customErrors mode=&quot;On&quot; defaultRedirect=&quot;~/Error&quot;&gt;
  &lt;error statusCode=&quot;404&quot; redirect=&quot;~/Error/NotFound&quot;/&gt;
  &lt;error statusCode=&quot;403&quot; redirect=&quot;~/Error/AccessDenied&quot;/&gt;
&lt;/customErrors&gt;
</pre></p>
<p>I’m going to show the steps to make it work for those three types of errors and also explain you about the HandleError attribute and how it handles the information specified in the customErrors. I’m also putting here the code I wrote in order to handle the AccessDenied error.</p>
<h3>Error Page</h3>
<p>When we are talking about the Error Page, one thing that you should understand is that without the HandleError Attribute, if an error occurs the .NET will just <strong>redirect</strong> the user to the page you specified in your Web.Config. So, if you have the &#8220;defaultRedirect&#8221; property pointing to something like &#8220;~/GenericError.aspx&#8221;, the GenericError.aspx page under the root folder of your project will be shown without any information about the Error. It could be a static HTML page that only tells the user &#8220;Hey, you&#8217;ve got an Error&#8221;.</p>
<p>Now, if you what you want is to show the user some information about the error, like the description or even the stackTrace but with the page looking like your default theme, then you can use the <strong>HandleError</strong> Attribute. By applying it to a Controller or a method the behavior of the customErrors changes a little bit. Instead of just pointing to some page, the exception handling will <strong>render</strong> a View, more specifically, a view called <strong>Error</strong> (if you don&#8217;t specify any other name to the Attribute) it doesn&#8217;t matter what name you&#8217;ve told the &#8220;defaultRedirect&#8221; to take you to. Along with that, it will send a HandleErrorInfo model containing the Controller name where the exception occurred, the Action name and the exception itself, so you can get the stackTrace and the description of the error.</p>
<p>Although it might seem obvious for some what the HandleError attribute does I&#8217;ve seen it&#8217;s a common issue people setting their customErrors to show something like GenericError.aspx and using the HandleError attribute. Or even, setting the defaultRedirect property to &#8220;Error&#8221; and expecting the Index method within the ErrorController to receive a HandleErrorInfo parameter. Remember, when using the HandleError attribute, the “Error“ View will be rendered internally, without even getting into the ErrorController.</p>
<h4>Common mistake</h4>
<p>Web.Config<br />
<pre class="brush: xml;">
&lt;customErrors mode=&quot;On&quot; defaultRedirect=&quot;Error/Ops&quot; /&gt;
</pre></p>
<p>HomeController.cs<br />
<pre class="brush: csharp;">
[HandleError] // using HandleError attribute
public class HomeController : Controller
{
    public ActionResult Index()
    {
		return View();
    }
}
</pre></p>
<p>ErrorController.cs<br />
<pre class="brush: csharp;">
public class ErrorController : Controller
{
    public ActionResult Ops()
    {
        //
		// Wrong! It won't get here because
		// the HandleError will handle the error.
		return View();
    }
}
</pre></p>
<h4>The right way</h4>
<p>I&#8217;m going to demonstrate now how you can make it work with a page that shows some information about the error. With this example, even though someone forget using the HandleError attribute, a friendly page still shows up but without the error info (it&#8217;s better than not showing anything). It could be improved, off course, but that it&#8217;s another point.</p>
<p>Web.config<br />
<pre class="brush: xml;">
&lt;customErrors mode=&quot;On&quot; defaultRedirect=&quot;Error&quot; /&gt;
</pre></p>
<p>Error.aspx<br />
<pre class="brush: csharp; html-script: true;">
&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; Inherits=&quot;System.Web.Mvc.ViewPage&lt;System.Web.Mvc.HandleErrorInfo&gt;&quot; %&gt;

&lt;asp:Content ID=&quot;errorTitle&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;
    Error
&lt;/asp:Content&gt;

&lt;asp:Content ID=&quot;errorContent&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;       

    &lt;h2&gt;
        Sorry, an error occurred while processing your request.
    &lt;/h2&gt;

	&lt;% if (Model.Exception != null ) { %&gt;
		&lt;p&gt;
		  Controller: &lt;%= Model.ControllerName %&gt;
		&lt;/p&gt;
		&lt;p&gt;
		  Action: &lt;%= Model.ActionName %&gt;
		&lt;/p&gt;
		&lt;p&gt;
		  Message: &lt;%= Model.Exception.Message%&gt;
		&lt;/p&gt;
		&lt;p&gt;
		  Stack Trace: &lt;%= Model.Exception.StackTrace%&gt;
		&lt;/p&gt;
	&lt;% } %&gt;
&lt;/asp:Content&gt;
</pre></p>
<p>Place that page inside your <strong>Views</strong> folder. It can be either your <strong>Shared</strong> or <strong>Error</strong> folder. The <strong>name</strong> must be Error.aspx.</p>
<p>That’s it! Only the aspx file and the setting inside the Web.config. </p>
<p>Only if you want that whether the Controller have or not the HandleError attribute, your custom error page will still be shown, then you can add an action method into your ErrorController:</p>
<p>ErrorController.cs<br />
<pre class="brush: csharp;">
public class ErrorController : Controller
{
    public ActionResult Index()
    {
		return View(&quot;Error&quot;);
    }
}
</pre></p>
<p>Now whether the page is being redirected to the defaultRedirect or rendered by the HandleError, the page will always be called.</p>
<p>To force an error, let&#8217;s create a code that throws an exception inside the Index of the HomeController:</p>
<p><pre class="brush: csharp;">
int a = 0;
int b = 0;
int result = a / b;
</pre></p>
<p>That will throw the <strong>DivideByZeroException</strong>, see the friendly page in action:</p>
<p><a href="http://devstuffs.files.wordpress.com/2010/12/error_page.jpg"><img title="error_page" src="http://devstuffs.files.wordpress.com/2010/12/error_page.jpg?w=640&#038;h=487" alt="CustomErrros Error Handling" width="640" height="487" /></a></p>
<p>&nbsp;</p>
<h3>404 NotFound Page</h3>
<p>For the Not Found page we don&#8217;t have much to tell the user, so there is not much to do. It&#8217;s only the redirect to a page and, the information we can show is the wrong path. That information already comes as a query string.</p>
<p>Just to centralize the error handling and make it more organized, let&#8217;s put our NotFound within our ErrorController. So, add your the following method to the ErrorController:</p>
<p><pre class="brush: csharp;">
public ActionResult NotFound(string aspxerrorpath)
{
	ViewData[&quot;error_path&quot;] = aspxerrorpath;

	return View();
}
</pre></p>
<p>and the aspx page code:</p>
<p>NotFound.aspx<br />
<pre class="brush: csharp; html-script: true;">
&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; Inherits=&quot;System.Web.Mvc.ViewPage&quot; %&gt;

&lt;asp:Content ID=&quot;errorTitle&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;
    Not Found
&lt;/asp:Content&gt;

&lt;asp:Content ID=&quot;errorContent&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;
    &lt;h2&gt;
        Sorry, the page &lt;%: ViewData[&quot;error_path&quot;] %&gt; does not exist.
    &lt;/h2&gt;    
&lt;/asp:Content&gt;
</pre></p>
<p>and add the following line for the Not Found error into your customErrors section:</p>
<p><pre class="brush: xml;">
&lt;customErrors mode=&quot;On&quot; defaultRedirect=&quot;Error&quot;&gt;
  &lt;error statusCode=&quot;404&quot; redirect=&quot;Error/NotFound&quot;/&gt;
&lt;/customErrors&gt;
</pre></p>
<p>See the result:<a href="http://devstuffs.files.wordpress.com/2010/12/page_not_found.jpg"><img title="page_not_found" src="http://devstuffs.files.wordpress.com/2010/12/page_not_found.jpg?w=640&#038;h=259" alt="CustomError Page Not Found" width="640" height="259" /></a></p>
<p>&nbsp;</p>
<h3>403 AccessDenied</h3>
<p>Most of the times when someone doesn’t have access to a page it’s only a login issue. But there are also times that a user has logged in but they are allowed to see something or perform some task, maybe because the lack of sufficient privileges. I’m going to show here how to handle those type of errors and the way I do that is by creating an ActionFilter attribute that you can put on a class or a method.</p>
<p>I’m going to hide some code for clarity:</p>
<p><pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Web.Configuration;

namespace ErrorHandling.Controllers
{
    public class AuthorizationAttribute : ActionFilterAttribute, IExceptionFilter
    {
        public string Action { get; set; }
        
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //
            // check permissions

            if (accessDenied)
            {
                throw new System.Web.HttpException((int)System.Net.HttpStatusCode.Forbidden,
                        &quot;You are now allowed to see this page.&quot;);
            }

            base.OnActionExecuting(filterContext);
        }

        #region IExceptionFilter Members

        // this method is almost a clone of HandleErrorAttribute from MVC
        // it's just changed so that we take the error message to the page set in the &lt;customErrors&gt;
        public virtual void OnException(ExceptionContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException(&quot;filterContext&quot;);
            }
            if (filterContext.IsChildAction)
            {
                return;
            }

            // If custom errors are disabled, we need to let the normal ASP.NET exception handler
            // execute so that the user can see useful debugging information.
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
                return;

            Exception exception = filterContext.Exception;

            // If this is not an HTTP 403 (for example, if somebody throws an HTTP 500 from an action method),
            // ignore it.
            if (new HttpException(null, exception).GetHttpCode() != 403)
            {
                return;
            }

            // try to get the customError page for the 403 code
            string customErrorPage = GetCustomError(&quot;403&quot;);

            // if there isn't a redirect to a 403 error page then get out
            if (customErrorPage == null)
                return;

            filterContext.Result = new RedirectResult(String.Concat(customErrorPage, &quot;?action=&quot;, this.Action));
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;            
        }

        #endregion

        public string GetCustomError(string statusCode)
        {
            CustomErrorsSection customErrorsSection = ConfigurationManager.GetSection(&quot;system.web/customErrors&quot;) as CustomErrorsSection;

            if (customErrorsSection != null)
            {
                CustomError customErrorPage = customErrorsSection.Errors[statusCode];

                if (customErrorPage != null)
                    return customErrorPage.Redirect;
            }
            return null;
        }        
    }
}
</pre></p>
<p>What that code does is throw an <strong>403 Exception</strong> if the user rights are not enough, then we implement the <strong>IExceptionFilter</strong>, so we can handle the error on our own way with the <strong>OnException</strong> method. Notice we are reading the customErrors section looking for the page that is set for the 403 code and <strong>redirecting</strong> the user to that page.</p>
<p>With that <strong>AuthorizationAttribute</strong> you can decorate any class or method that you want to be checked when a user access it, like so:</p>
<p><pre class="brush: csharp;">
[AuthorizationAttribute]
[HandleError]
public class HomeController : Controller
</pre></p>
<p>Let’s create our page inside the Views folder (again, it can be the Shared or the Errors folder) to tell the user about the error:</p>
<p>AccessDenied.aspx<br />
<pre class="brush: csharp; html-script: true;">
&lt;%@ Page Language=&quot;C#&quot; MasterPageFile=&quot;~/Views/Shared/Site.Master&quot; Inherits=&quot;System.Web.Mvc.ViewPage&quot; %&gt;

&lt;asp:Content ID=&quot;errorTitle&quot; ContentPlaceHolderID=&quot;TitleContent&quot; runat=&quot;server&quot;&gt;
    Access Denied
&lt;/asp:Content&gt;

&lt;asp:Content ID=&quot;errorContent&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;
    &lt;h2&gt;
        Sorry, you do not have access to the action &lt;%: ViewData[&quot;action&quot;] %&gt;.
    &lt;/h2&gt;    
&lt;/asp:Content&gt;
</pre></p>
<p>Add one more action method to our ErrorController:</p>
<p><pre class="brush: csharp;">
public ActionResult AccessDenied(string action)
{
	ViewData[&quot;action&quot;] = action;

	return View();
}
</pre></p>
<p>In this example, I’m passing in a parameter called “action” to the AccessDenied method, but it could be anything you want and can contain any information you want.</p>
<p>And finally, completing our Web.Config, we now add the treatment for the 403 error code:</p>
<p><pre class="brush: xml;">
&lt;customErrors mode=&quot;On&quot; defaultRedirect=&quot;Error&quot;&gt;
  &lt;error statusCode=&quot;404&quot; redirect=&quot;Error/NotFound&quot;/&gt;
  &lt;error statusCode=&quot;403&quot; redirect=&quot;Error/AccessDenied&quot;/&gt;
&lt;/customErrors&gt;
</pre></p>
<p>Here is how our ErrorController end up like:</p>
<p><pre class="brush: csharp;">
namespace ErrorHandling.Controllers
{
    public class ErrorController : Controller
    {
        public ActionResult Index()
        {
            return View(&quot;Error&quot;);
        }

        public ActionResult NotFound(string aspxerrorpath)
        {
            ViewData[&quot;error_path&quot;] = aspxerrorpath;

            return View();
        }

        public ActionResult AccessDenied(string action)
        {
            ViewData[&quot;action&quot;] = action;

            return View();
        }
    }
}
</pre></p>
<p>With this structure the errors are very organized and all the things related to it are together.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/devstuffs.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/devstuffs.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/devstuffs.wordpress.com/157/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devstuffs.wordpress.com&amp;blog=9150119&amp;post=157&amp;subd=devstuffs&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devstuffs.wordpress.com/2010/12/12/how-to-use-customerrors-in-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/714896732f249e6887c761a90b0986ad?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">devstuffs</media:title>
		</media:content>

		<media:content url="http://devstuffs.files.wordpress.com/2010/12/error_page.jpg" medium="image">
			<media:title type="html">error_page</media:title>
		</media:content>

		<media:content url="http://devstuffs.files.wordpress.com/2010/12/page_not_found.jpg" medium="image">
			<media:title type="html">page_not_found</media:title>
		</media:content>
	</item>
	</channel>
</rss>
