<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-59384554657271185</atom:id><lastBuildDate>Fri, 02 Dec 2011 18:52:31 +0000</lastBuildDate><category>NUnit</category><category>Software Engineering Education</category><category>Python</category><category>Unit testing</category><category>SQL</category><category>Peer-to-Peer</category><category>Requirements Engineering</category><category>MSMQ</category><category>Programming Thoughts</category><category>Interview Questions</category><category>JSON</category><category>SWEBOK</category><category>CSDP</category><title>Midnight Researcher Notes</title><description /><link>http://ebeid-soliman.blogspot.com/</link><managingEditor>noreply@blogger.com (Ebeid Soliman)</managingEditor><generator>Blogger</generator><openSearch:totalResults>45</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ebeid-soliman" /><feedburner:info uri="ebeid-soliman" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-251974912800519055</guid><pubDate>Thu, 01 Dec 2011 23:43:00 +0000</pubDate><atom:updated>2011-12-02T01:49:31.559+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">JSON</category><title>JSON–yet another tutorial :)</title><description>&lt;h4&gt;What is JSON?&lt;/h4&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://json.org/" target="_blank"&gt;JSON&lt;/a&gt;&lt;/strong&gt; (or &lt;strong&gt;J&lt;/strong&gt;ava&lt;strong&gt;S&lt;/strong&gt;cript &lt;strong&gt;O&lt;/strong&gt;bject &lt;strong&gt;N&lt;/strong&gt;otation) is a highly portable data interchange format. While its structure is recognized natively by Javascript (as it &lt;em&gt;is&lt;/em&gt; Javascript), its formatting conventions are easily recognized by other C-like languages. &lt;/p&gt;  &lt;p&gt;JSON is built on two structures:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array. Example: &lt;/li&gt; &lt;/ul&gt;  &lt;pre class="brush:javascript"&gt;var jason = {&lt;br /&gt; "age" : "28",&lt;br /&gt; "hometown" : "Cairo, Egypt",&lt;br /&gt; "gender" : "male"&lt;br /&gt; };&lt;/pre&gt;&lt;blockquote&gt;&lt;p&gt;The object created in the example could be accessed like any other object.&lt;/p&gt;&lt;/blockquote&gt;&lt;ul&gt;  &lt;li&gt;An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. Example,&lt;/li&gt;&lt;/ul&gt;&lt;pre class="brush:javascript"&gt;var family = [{&lt;br /&gt; "name" : "Jason",&lt;br /&gt; "age" : "24",&lt;br /&gt; "gender" : "male"&lt;br /&gt; },&lt;br /&gt; {&lt;br /&gt; "name" : "Kyle",&lt;br /&gt; "age" : "21",&lt;br /&gt; "gender" : "male"&lt;br /&gt; }];&lt;/pre&gt;&lt;blockquote&gt;&lt;p&gt;This array could be accessed like any array of objects.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Since JSON and XML are both used for data exchange, you may find yourself in the middle of deciding which format to use.&lt;/p&gt;&lt;h5&gt;Reasons to choose JSON over XML&lt;/h5&gt;&lt;ol&gt;  &lt;li&gt;JSON is easier to read than XML (especially of large amounts of data)&lt;/li&gt;  &lt;li&gt;JSON requires less tags than XML – XML items must be wrapped in open and close tags whereas JSON you just name the tag once&lt;/li&gt;  &lt;li&gt;Because JSON is transportation-independent, you can just bypass the XMLHttpRequest object for getting your data.&lt;/li&gt;  &lt;li&gt;JavaScript is not just data – you can also put methods and all sorts of goodies in JSON format.&lt;/li&gt;  &lt;li&gt;JSON is better at helping procedural decisions in your JavaScript based on objects and their values (or methods).&lt;/li&gt;  &lt;li&gt;You can get JSON data from anywhere, not just your own domain.&lt;/li&gt;&lt;/ol&gt;&lt;h5&gt;Reasons to choose XML over JSON&lt;/h5&gt;&lt;ol&gt;  &lt;li&gt;Easy to take XML and apply XSLT to make XHTML.&lt;/li&gt;  &lt;li&gt;XML is supported by many more desktop applications than JSON.&lt;/li&gt;  &lt;li&gt;XML can put JSON in it on the way back to the client.&lt;/li&gt;  &lt;li&gt;AJAX includes XML in it and not JSON.&lt;/li&gt;&lt;/ol&gt;&lt;h4&gt;.Net Support for JSON&lt;/h4&gt;&lt;h4&gt;&lt;span class="Apple-style-span" style="font-weight: normal; "&gt;.Net framework provides System.Web.Script.Serialization.JavaScriptSerializer to serialize and deserialize objects. Here is a simple example:&lt;/span&gt;&lt;/h4&gt;&lt;pre class="brush:csharp"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;namespace JSON_Demo1&lt;br /&gt;{&lt;br /&gt;  public partial class Page1 : System.Web.UI.Page&lt;br /&gt;  {&lt;br /&gt;      protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;      {&lt;br /&gt;          Employee oEmployee1 = new Employee { Name = "Emp1", ID = "11", Age = "30" };&lt;br /&gt;          Employee oEmployee2 = new Employee { Name = "Emp2", ID = "22", Age = "31" };&lt;br /&gt;          Employee oEmployee3 = new Employee { Name = "Emp3", ID = "33", Age = "20" };&lt;br /&gt;          List&lt;employee&gt; oList = new List&lt;employee&gt;() { oEmployee1, oEmployee2, oEmployee3 };&lt;br /&gt;          System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();&lt;br /&gt;          string sJSON = oSerializer.Serialize(oList);&lt;br /&gt;          Response.Write(sJSON);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  public class Employee&lt;br /&gt;  {&lt;br /&gt;      public string Name { get; set; }&lt;br /&gt;      public string Age { get; set; }&lt;br /&gt;      public string ID { get; set; }&lt;br /&gt;  }&lt;br /&gt;}&lt;/employee&gt;&lt;/employee&gt;&lt;/pre&gt;&lt;p&gt;When you run the above application, you will see the JSON array on the response like so:&lt;/p&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/-YyboQaEuXN0/TtgREFLqnwI/AAAAAAAAALA/UZBtgfZSlP0/s1600-h/JSON_Demo11%25255B3%25255D.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="JSON_Demo11" border="0" alt="JSON_Demo11" src="http://lh4.ggpht.com/-3m9QJruQuvA/TtgREUxCsDI/AAAAAAAAALI/5WZl5t_qBmA/JSON_Demo11_thumb%25255B1%25255D.jpg?imgmax=800" width="496" height="371" /&gt;&lt;/a&gt; &lt;/p&gt;&lt;p&gt;Now let’s change this example a little bit so we can make Ajax call to get the JSON data and parse it using javaScript. Change the code-behind to look like:&lt;/p&gt;&lt;pre class="brush:csharp"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Web;&lt;br /&gt;using System.Web.UI;&lt;br /&gt;using System.Web.UI.WebControls;&lt;br /&gt;using System.Web.Services;&lt;br /&gt;&lt;br /&gt;namespace JSON_Demo1&lt;br /&gt;{&lt;br /&gt;  public partial class Page1 : System.Web.UI.Page&lt;br /&gt;  {&lt;br /&gt;      protected void Page_Load(object sender, EventArgs e)&lt;br /&gt;      {&lt;br /&gt;          Response.Write(GetJSONData());&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      [WebMethod()]&lt;br /&gt;      public static string GetJSONData()&lt;br /&gt;      {&lt;br /&gt;          Employee oEmployee1 = new Employee { Name = "Emp1", ID = "11", Age = "30" };&lt;br /&gt;          Employee oEmployee2 = new Employee { Name = "Emp2", ID = "22", Age = "31" };&lt;br /&gt;          Employee oEmployee3 = new Employee { Name = "Emp3", ID = "33", Age = "20" };&lt;br /&gt;          List&lt;employee&gt; oList = new List&lt;employee&gt;() { oEmployee1, oEmployee2, oEmployee3 };&lt;br /&gt;          System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();&lt;br /&gt;          return oSerializer.Serialize(oList);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  public class Employee&lt;br /&gt;  {&lt;br /&gt;      public string Name { get; set; }&lt;br /&gt;      public string Age { get; set; }&lt;br /&gt;      public string ID { get; set; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;/employee&gt;&lt;/employee&gt;&lt;/pre&gt;&lt;p&gt;now change the .aspx page to look like:&lt;/p&gt;&lt;pre style="background: #ffffff; color: #000000"&gt;&amp;lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1&lt;span style="color: #008c00"&gt;.&lt;/span&gt;aspx&lt;span style="color: #008c00"&gt;.&lt;/span&gt;cs" Inherits="JSON_Demo1&lt;span style="color: #008c00"&gt;.&lt;/span&gt;Page1" %&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #004a43"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "&lt;/span&gt;&lt;span style="color: #5555dd"&gt;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&lt;/span&gt;&lt;span style="color: #004a43"&gt;"&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;html&lt;/span&gt;&lt;span style="color: #274796"&gt; xmlns&lt;/span&gt;&lt;span style="color: #808030"&gt;=&lt;/span&gt;&lt;span style="color: #0000e6"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;head&lt;/span&gt;&lt;span style="color: #274796"&gt; runat&lt;/span&gt;&lt;span style="color: #808030"&gt;=&lt;/span&gt;&lt;span style="color: #0000e6"&gt;"server"&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;title&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;title&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;script&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;script type="text/javascript"&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      $&lt;span style="color: #808030"&gt;(&lt;/span&gt;document&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #808030"&gt;.&lt;/span&gt;ready&lt;span style="color: #808030"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;function&lt;/span&gt; &lt;span style="color: #808030"&gt;(&lt;/span&gt;&lt;span style="color: #808030"&gt;)&lt;/span&gt; &lt;span style="color: #800080"&gt;{&lt;/span&gt;&lt;br /&gt;          $&lt;span style="color: #808030"&gt;.&lt;/span&gt;ajax&lt;span style="color: #808030"&gt;(&lt;/span&gt;&lt;span style="color: #800080"&gt;{&lt;/span&gt;&lt;br /&gt;              type&lt;span style="color: #800080"&gt;:&lt;/span&gt; &lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #0000e6"&gt;POST&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #808030"&gt;,&lt;/span&gt;&lt;br /&gt;              url&lt;span style="color: #800080"&gt;:&lt;/span&gt; &lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #0000e6"&gt;Page1.aspx/GetJSONData&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #808030"&gt;,&lt;/span&gt;&lt;br /&gt;              contentType&lt;span style="color: #800080"&gt;:&lt;/span&gt; &lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #0000e6"&gt;application/json; charset=utf-8&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #808030"&gt;,&lt;/span&gt;&lt;br /&gt;              dataType&lt;span style="color: #800080"&gt;:&lt;/span&gt; &lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #0000e6"&gt;json&lt;/span&gt;&lt;span style="color: #800000"&gt;"&lt;/span&gt;&lt;span style="color: #808030"&gt;,&lt;/span&gt;&lt;br /&gt;              success&lt;span style="color: #800080"&gt;:&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold"&gt;function&lt;/span&gt; &lt;span style="color: #808030"&gt;(&lt;/span&gt;msg&lt;span style="color: #808030"&gt;)&lt;/span&gt; &lt;span style="color: #800080"&gt;{&lt;/span&gt;&lt;br /&gt;                  BuildTable&lt;span style="color: #808030"&gt;(&lt;/span&gt;msg&lt;span style="color: #808030"&gt;.&lt;/span&gt;d&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;                  &lt;span style="color: #800080"&gt;}&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #800080"&gt;}&lt;/span&gt;&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #800080"&gt;}&lt;/span&gt;&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;      &lt;span style="color: #800000; font-weight: bold"&gt;function&lt;/span&gt; BuildTable&lt;span style="color: #808030"&gt;(&lt;/span&gt;msg&lt;span style="color: #808030"&gt;)&lt;/span&gt; &lt;span style="color: #800080"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #800000; font-weight: bold"&gt;var&lt;/span&gt; table &lt;span style="color: #808030"&gt;=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;table&amp;gt; &amp;lt;thead&amp;gt; &amp;lt;tr&amp;gt; &amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;ID&amp;lt;/th&amp;gt; &amp;lt;th&amp;gt;Age&amp;lt;/th&amp;gt; &amp;lt;/thead&amp;gt; &amp;lt;tbody&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #800000; font-weight: bold"&gt;var&lt;/span&gt; myJSONObject &lt;span style="color: #808030"&gt;=&lt;/span&gt; &lt;span style="color: #800000; font-weight: bold"&gt;eval&lt;/span&gt;&lt;span style="color: #808030"&gt;(&lt;/span&gt; msg &lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #800000; font-weight: bold"&gt;for&lt;/span&gt; &lt;span style="color: #808030"&gt;(&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;var&lt;/span&gt; i &lt;span style="color: #808030"&gt;=&lt;/span&gt; &lt;span style="color: #008c00"&gt;0&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt; i &lt;span style="color: #808030"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #008c00"&gt;3&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt; i&lt;span style="color: #808030"&gt;++&lt;/span&gt;&lt;span style="color: #808030"&gt;)&lt;/span&gt; &lt;span style="color: #800080"&gt;{&lt;/span&gt;              &lt;br /&gt;              &lt;span style="color: #800000; font-weight: bold"&gt;var&lt;/span&gt; row &lt;span style="color: #808030"&gt;=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;tr&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;              row &lt;span style="color: #808030"&gt;+=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;td&amp;gt;'&lt;/span&gt; &lt;span style="color: #808030"&gt;+&lt;/span&gt; myJSONObject&lt;span style="color: #808030"&gt;[&lt;/span&gt;i&lt;span style="color: #808030"&gt;]&lt;/span&gt;&lt;span style="color: #808030"&gt;.&lt;/span&gt;Name &lt;span style="color: #808030"&gt;+&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;/td&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;              row &lt;span style="color: #808030"&gt;+=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;td&amp;gt;'&lt;/span&gt; &lt;span style="color: #808030"&gt;+&lt;/span&gt; myJSONObject&lt;span style="color: #808030"&gt;[&lt;/span&gt;i&lt;span style="color: #808030"&gt;]&lt;/span&gt;&lt;span style="color: #808030"&gt;.&lt;/span&gt;ID &lt;span style="color: #808030"&gt;+&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;/td&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;              row &lt;span style="color: #808030"&gt;+=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;td&amp;gt;'&lt;/span&gt; &lt;span style="color: #808030"&gt;+&lt;/span&gt; myJSONObject&lt;span style="color: #808030"&gt;[&lt;/span&gt;i&lt;span style="color: #808030"&gt;]&lt;/span&gt;&lt;span style="color: #808030"&gt;.&lt;/span&gt;Age &lt;span style="color: #808030"&gt;+&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;/td&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;              row &lt;span style="color: #808030"&gt;+=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;/tr&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;              table &lt;span style="color: #808030"&gt;+=&lt;/span&gt; row&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: #800080"&gt;}&lt;/span&gt;&lt;br /&gt;          table &lt;span style="color: #808030"&gt;+=&lt;/span&gt; &lt;span style="color: #0000e6"&gt;'&amp;lt;/tbody&amp;gt;&amp;lt;/table&amp;gt;'&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;          $&lt;span style="color: #808030"&gt;(&lt;/span&gt;&lt;span style="color: #0000e6"&gt;'#Container'&lt;/span&gt;&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #808030"&gt;.&lt;/span&gt;html&lt;span style="color: #808030"&gt;(&lt;/span&gt;table&lt;span style="color: #808030"&gt;)&lt;/span&gt;&lt;span style="color: #800080"&gt;;&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #800080"&gt;}&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;script&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;head&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;body&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #a65700"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;div&lt;/span&gt;&lt;span style="color: #274796"&gt; &lt;/span&gt;&lt;span style="color: #074726"&gt;id&lt;/span&gt;&lt;span style="color: #808030"&gt;=&lt;/span&gt;&lt;span style="color: #0000e6"&gt;"Container"&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;div&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;body&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #a65700"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000; font-weight: bold"&gt;html&lt;/span&gt;&lt;span style="color: #a65700"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now let’s run the application and see the output:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/-BV1Tl-JzV38/TtgRE5nPvJI/AAAAAAAAALQ/U494e_qrJeU/s1600-h/JSON_Demo12%25255B4%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="JSON_Demo12" border="0" alt="JSON_Demo12" src="http://lh6.ggpht.com/-OeGpH-Lhj9Y/TtgRFAHduiI/AAAAAAAAALY/r8BlbXR5Atw/JSON_Demo12_thumb%25255B2%25255D.jpg?imgmax=800" width="410" height="427" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;In the previous example, we created a WebMethod, then call it from jQuery and pass the output of it to JavaScript method &lt;strong&gt;BuildTable(msg)&lt;/strong&gt;. This method convert the string msg into a JSON array of objects and then loop on that array to build the shown simple data table. We also rendered the JSON data to the browser directly from server-side code, just to show the original data and how it transformed using JSON.&lt;/p&gt;&lt;p&gt;In this post we gave a brief background about JSON, when use it and when use XML to send data, how .Net provides native serializer for JSON, and how to deserialize it using JavaScript. We may look into more advanced topics regarding JSON in future posts.&lt;/p&gt;&lt;p&gt;Good Luck&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-251974912800519055?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/lU62eQDj3VI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/lU62eQDj3VI/jsonyet-another-tutorial.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-3m9QJruQuvA/TtgREUxCsDI/AAAAAAAAALI/5WZl5t_qBmA/s72-c/JSON_Demo11_thumb%25255B1%25255D.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/12/jsonyet-another-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-5646394697131614938</guid><pubDate>Tue, 22 Nov 2011 17:52:00 +0000</pubDate><atom:updated>2011-12-01T20:36:07.637+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">MSMQ</category><title>An introduction to Microsoft Message Queuing–Part 2</title><description>&lt;p&gt;In the &lt;a title="An introduction to Microsoft Message Queuing" href="http://ebeid-soliman.blogspot.com/2011/11/introduction-to-microsoft-message.html" target="_blank"&gt;previous post&lt;/a&gt;, we explored very basic concepts of MSMQ. We used non-transactional queue to send and receive a message. In this post we will try to dive more into Message Queuing Queues.&lt;/p&gt;  &lt;p&gt;Simply, queues are logical containers that Message Queuing uses to store and later forward messages, providing the bases for the loosely coupled aspects of Message Queuing. Queues can be created by applications and by Message Queuing. Queues created by applications or by users in an MMC snap-in are referred to as &lt;em&gt;&lt;strong&gt;application queues&lt;/strong&gt;&lt;/em&gt;. Queues created by Message Queuing are referred to as &lt;strong&gt;&lt;em&gt;system-generated queues &lt;/em&gt;&lt;/strong&gt;(we will talk about system- generated queues in another post).&lt;/p&gt;  &lt;p&gt;Depending on the service provided by the queue, &lt;strong&gt;application queues&lt;/strong&gt; can be &lt;u&gt;public&lt;/u&gt; or &lt;u&gt;private&lt;/u&gt;, and they can be &lt;u&gt;transactional&lt;/u&gt; or &lt;u&gt;nontransactional&lt;/u&gt;. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Application queues&lt;/strong&gt; can play any of the following roles:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Destination queues : any queue used to send/receive messages between applications. &lt;/li&gt;    &lt;li&gt;Administration queues : used for &lt;em&gt;acknowledgment messages&lt;/em&gt; returned by Message Queuing or &lt;em&gt;connector applications&lt;/em&gt;. &lt;/li&gt;    &lt;li&gt;Response queues : used by receiving applications to return &lt;em&gt;response messages&lt;/em&gt; to the sending application. &lt;/li&gt;    &lt;li&gt;Report queues : used to store &lt;em&gt;report messages&lt;/em&gt; returned by Message Queuing. &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;Destination Queues&lt;/h5&gt;  &lt;p&gt;Destination queues are any queue used to send/receive messages between applications. The sending application on computer 1 sends the messages to the queue and the receiving application on computer 2 reads the messages from the queue. Typically, destination queues are located on the same computer as the receiving application in order to minimize network traffic.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Public Versus Private Queues&lt;/u&gt;&lt;/strong&gt; The decision to use public or private destination queues depends primarily on whether you want other applications to be able to locate the queues or not. Message Queuing registers private queues locally by storing a description of each queue in a separate file in the local queue storage (LQS) folder on the local computer (the default Lqs folder is %windir%\System32\MSMQ\Storage\Lqs). Also a description of each public queue created on the local computer is also stored locally in a separate file in the Lqs folder.&lt;/p&gt;  &lt;p&gt;Adv. of Public Queues:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Registered in the directory service, so it can be located by other Message Queuing applications. &lt;/li&gt;    &lt;li&gt;Persistent and its registration information can be backed up. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Adv. of Private Queues:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Requires minimal directory service overhead(faster to create, no latency, and no replication) &lt;/li&gt;    &lt;li&gt;Can be created and deleted when the &lt;em&gt;directory service&lt;/em&gt; is not working &lt;/li&gt;    &lt;li&gt;Can be accessed directly by name without query the directory service. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Div. of Private Queues:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;It is registered on local computer, so it is properties cannot be obtained by Message Queuing applications running on remote computers. Private queues can be exposed to other applications by making the location of the private queue known to the applications. To distribute the location of a private queue, an application can send the format name of the private queue as the response queue property of a message. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Transactional Versus Nontransactional Queues&lt;/u&gt;&lt;/strong&gt; The decision to use transactional or nontransactional queues is based on whether the applications accessing the queue will be sending and receiving messages within the context of a transaction. So, what is a transaction ? a transaction is to execute a multiple-steps process such that either all or none of the steps will complete. In reality, transactions are handled by rolling back any steps that have already occurred if the entire transaction is not completed successfully. &lt;/p&gt;  &lt;p&gt;When sending messages, only transactional queues can accept messages sent in the context of a transaction. These messages are also referred to as transactional messages. In a similar way, nontransactional queues can only accept messages sent outside the context of a transaction. Note that only transactional messages are delivered with exactly-once-delivery (EOD) guarantees.&lt;/p&gt;  &lt;p&gt;When receiving messages, only local transactional queues can be accessed within the context of a transaction. The transactional queue must be local to the receiving application. On the other hand, nontransactional queues can be accessed within or outside of a transaction. Also, transactional queues, local or remote, can be accessed outside of a transaction (because we ask it to do less than what it can do).&lt;/p&gt;  &lt;p&gt;If you just want to be able to recover lost messages, don’t use transactional queues. You can set the &lt;em&gt;Recoverable&lt;/em&gt; property of a every message you sent. Or you can sent the queue property &lt;span&gt;&lt;span&gt;DefaultPropertiesToSend.Recoverable&lt;/span&gt; &lt;span&gt;to true.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;h5&gt;Creating a Transactional Queue&lt;/h5&gt;  &lt;ul&gt;   &lt;li&gt;Through the snap in : just check the check-box Transactional below the queue name textbox. &lt;/li&gt;    &lt;li&gt;Programmatically : just like &lt;span&gt;privateQueue = MessageQueue.Create(".\\Private$\\privateQueue", true);&lt;/span&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;span&gt;MessageQueue &lt;span&gt;have a property called&lt;/span&gt; Transactional &lt;span&gt;that you can check to ensure that the queue is transactional&lt;/span&gt; &lt;/span&gt;&lt;/p&gt;  &lt;p&gt;MSMQ supports two types of transactions: Internal and External.&lt;/p&gt;  &lt;h5&gt;Internal Transactions&lt;/h5&gt;  &lt;p&gt;Class &lt;span&gt;MessageQueueTransaction &lt;span&gt;can be used to&lt;/span&gt; Begin(), Commit(), Abort() &lt;span&gt;the transaction. It also can be passed through&lt;/span&gt; Send() &lt;span&gt;and&lt;/span&gt; Receive() &lt;/span&gt;&lt;span&gt;methods to that operation falls under a transaction. The class also exposes a Status property to give the transaction status. Transaction status can be one of:&lt;/span&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Initialized : The transaction has been initialized but not yet started. &lt;/li&gt;    &lt;li&gt;Pending : The transaction has been began but not committed or aborted yet. &lt;/li&gt;    &lt;li&gt;Committed : The transaction has been committed. &lt;/li&gt;    &lt;li&gt;Aborted : The transaction has been aborted. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Transaction Types&lt;/strong&gt; when sending or receiving using MessageQueue class through transactional queues, you could pass one of the values in MessageQueueTransactionType enumeration. This specifies how you would like to interact with the queue. These values are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Single : each queue operation will be doine in a separate internal transaction. &lt;/li&gt;    &lt;li&gt;None : enable you to receive a message from a transactional queue, but outside a transaction. It also enables us to send a transactional message to a non-transactional queue. &lt;/li&gt;    &lt;li&gt;Automatic : used with external transactions to direct the send or receive operations to use an already existing transaction context. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now lets compile these pieces into one example to get sense of what internal transactional queues means.&lt;/p&gt;&lt;pre class="brush:csharp"&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Messaging;&lt;br /&gt;&lt;br /&gt;namespace MSMQ_Demo3&lt;br /&gt;{&lt;br /&gt; class Program&lt;br /&gt; {&lt;br /&gt;     static void Main(string[] args)&lt;br /&gt;     {&lt;br /&gt;         MessageQueueTransaction mqTran = new MessageQueueTransaction();&lt;br /&gt;         MessageQueue queueA = MessageQueue.Create(".\\Private$\\TranQueueA", true);&lt;br /&gt;         MessageQueue queueB = MessageQueue.Create(".\\Private$\\TranQueueB", true);&lt;br /&gt;         mqTran.Begin();&lt;br /&gt;         try&lt;br /&gt;         {&lt;br /&gt;             queueA.Send("Message A", "Label A", mqTran );&lt;br /&gt;             queueB.Send("Message B", "Label B", mqTran );&lt;br /&gt;             mqTran.Commit();&lt;br /&gt;         }&lt;br /&gt;         catch(Exception ex)&lt;br /&gt;         {&lt;br /&gt;             mqTran.Abort();&lt;br /&gt;             Console.WriteLine(ex.Message);&lt;br /&gt;         }             &lt;br /&gt;&lt;br /&gt;         MessageQueue queueC = new MessageQueue(".\\Private$\\TranQueueA");&lt;br /&gt;         MessageQueue queueD = new MessageQueue(".\\Private$\\TranQueueB");&lt;br /&gt;         string strMsg = "";&lt;br /&gt;         mqTran.Begin();&lt;br /&gt;         try&lt;br /&gt;         {&lt;br /&gt;             Message msg = queueC.Receive(mqTran);&lt;br /&gt;             msg.Formatter = new XmlMessageFormatter(new Type[1] { typeof(string) });&lt;br /&gt;             strMsg = msg.Body.ToString();&lt;br /&gt;             msg = queueD.Receive(mqTran);&lt;br /&gt;             msg.Formatter = new XmlMessageFormatter(new Type[1] { typeof(string) });&lt;br /&gt;             strMsg += " \n ";&lt;br /&gt;             strMsg += msg.Body.ToString();&lt;br /&gt;             Console.WriteLine(strMsg);&lt;br /&gt;             mqTran.Commit();&lt;br /&gt;         }&lt;br /&gt;         catch (Exception ex)&lt;br /&gt;         {&lt;br /&gt;             mqTran.Abort();&lt;br /&gt;             Console.WriteLine(ex.Message);&lt;br /&gt;         }&lt;br /&gt;         Console.WriteLine();&lt;br /&gt;     }&lt;br /&gt;  &lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px; white-space: normal; "&gt;In this example we created two transactional queues, send two messages to them. Then we received these messages. Both the sending and receiving done in a transaction that commits only after the success of all operations. If any errors happened, we abort the whole transaction and rollback all operations done in it.&lt;/span&gt;&lt;p&gt;In future posts we will talk more about MSQM details.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-5646394697131614938?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/6ZMij6MDGck" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/6ZMij6MDGck/in-previous-post-we-explored-very-basic.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/11/in-previous-post-we-explored-very-basic.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-3381795044346782183</guid><pubDate>Thu, 17 Nov 2011 12:49:00 +0000</pubDate><atom:updated>2011-11-28T01:37:29.398+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">MSMQ</category><title>An introduction to Microsoft Message Queuing</title><description>&lt;p&gt;&lt;strong&gt;What is MSMQ ?&lt;/strong&gt; Message Queuing (also known as MSMQ) is a messaging infrastructure and a development tool for creating distributed messaging applications for Microsoft Windows operating systems. Applications developed for Message Queuing send messages to queues, which are temporary storage locations, from which messages can proceed to their final destination as conditions permit.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Installing MSMQ &lt;/strong&gt;The procedures to install MSMQ on Windows Server 2008, Winows Server 2008 R2, Windows 7, Windows Vista, Windows XP, and Windows Server 2003 are available &lt;a title="Installing Message Queuing (MSMQ)" href="http://msdn.microsoft.com/en-us/library/aa967729.aspx" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Basic Messaging  &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-BIn-NR6GGNQ/TsUC7bfFDKI/AAAAAAAAAKo/nn1sOeBP1ow/s1600-h/MSMQ1%25255B3%25255D.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MSMQ1" border="0" alt="MSMQ1" src="http://lh3.ggpht.com/-iRV60VxSKZo/TsUC7ooDFjI/AAAAAAAAAKs/2gB3XueW_ZU/MSMQ1_thumb%25255B1%25255D.jpg?imgmax=800" width="555" height="337" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The image shows the basic messaging scenario. You have two applications: a sending application and a receiving application. A sending application prepares a message and puts it into a queue. A message can be any piece of information that the receiving application understands. The receiving application receives the message from the queue and processes it. One thing to note here is that the sender and the receiver are not tightly coupled and they can work without the other application being online.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Messaging in .Net &lt;/strong&gt;To build a messaging application we need to create at least three components: queue, application to send messages, and another application to receive these messages.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Types of queues&lt;/strong&gt; The basic types of MSMQ queues are: private and public. Public queues are those that are published in the active directory. So, applications running on different servers throughout the network can find and use public queues through the active directory. Private queues on the other hand are available locally on a particular machine and are not published in the active directory.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Creating a Queue&lt;/strong&gt; Queues can be created either programmatically or through the computer management snap-in (if you installed MSMQ on your machine). &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Programmatically&lt;/strong&gt; The System.Messaging.M&lt;code&gt;&lt;span&gt;essageQueue&lt;/span&gt; &lt;/code&gt;class provides all the necessary functionality to work with and manipulate MSMQ queues. It is like a wrapper around message queuing. MessageQueue.Create(path) creates a non-transactional message queue at the specified path (we will talk about transactional and non-transactional queues later). For public queues, path is MachineName\QueueName. For private queues, MachineName\Private$\QueueName. You can use "." for the local computer. Code should look like: &lt;/li&gt; &lt;/ul&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;MessageQueue privateQueue = MessageQueue.Create(&lt;span class="str"&gt;".\\Private$\\privateQueue"&lt;/span&gt;); &lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt;&lt;strong&gt;Through the computer management snap-in &lt;/strong&gt;Open the computer management snap-in (right click computer, then manage). Navigate to Services and Applications, then Message Queuing. Right click on Private Queues –&amp;gt; New –&amp;gt; Private Queue. Or right click on public Queues –&amp;gt; New –&amp;gt; Public Queue. &lt;/li&gt;
&lt;br /&gt;&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/--VAHZ6yAB10/TsVgDjIqZ8I/AAAAAAAAAKw/GdRORuiOjfI/s1600-h/MMC%25255B5%25255D.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="MMC" border="0" alt="MMC" src="http://lh6.ggpht.com/-za1LFgBEpts/TsVgEIgQ0vI/AAAAAAAAAK4/6x4JoPYdqzY/MMC_thumb%25255B3%25255D.jpg?imgmax=800" width="743" height="559" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Sending a message&lt;/strong&gt; Use Send() method of your previously created MessageQueue object like so&lt;/p&gt;
&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="csharpcode"&gt;
&lt;br /&gt;&lt;pre class="alt"&gt;privateQueue.Send(&lt;span class="str"&gt;"Message Body (could be any managed object)"&lt;/span&gt;, &lt;span class="str"&gt;"Message Label"&lt;/span&gt;);&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Receiving a message&lt;/strong&gt; There are two types of operations with respect to reading a message fom the Queue: Peeking and Receiving. When we receive a message from the queue, the message is removed from the queue. Peeking is similar to Receiving but here the message is not removed from the queue. Code could look like&lt;/p&gt;
&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;div class="csharpcode"&gt;
&lt;br /&gt;&lt;pre class="alt"&gt;MessageQueue anotherPrivateQueue = &lt;span class="kwrd"&gt;new&lt;/span&gt; MessageQueue(&lt;span class="str"&gt;".\\Private$\\privateQueue"&lt;/span&gt;);&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;pre&gt;System.Messaging.Message msg = anotherPrivateQueue.Receive();&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Additional Considerations&lt;/strong&gt; before writing the first complete messaging application&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;ul&gt;
&lt;br /&gt;&lt;li&gt;You have to check if there is already a Message Queuing queue with the same name before creating your new queue. Use MessageQueue.Exists(path) to check for queue existence. &lt;/li&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;li&gt;When receiving a message and before accessing its body, you have to set the Formatter property of the Message object. There is a lot of built-in formatters. For now, we can use XMLFormatter. &lt;/li&gt;
&lt;br /&gt;&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;Now, let’s get things together into a “Hello, World” example. Open Visual Studio, New project, Windows forms. All reference to System.Messaging namespace. Then edit your code to look like so:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;pre class="brush:csharp"&gt;
&lt;br /&gt;
&lt;br /&gt;using System;
&lt;br /&gt;using System.Collections.Generic;
&lt;br /&gt;using System.Linq;
&lt;br /&gt;using System.Text;
&lt;br /&gt;using System.Messaging;
&lt;br /&gt;
&lt;br /&gt;namespace MSMQ_Demo
&lt;br /&gt;{
&lt;br /&gt;    class Program
&lt;br /&gt;    {
&lt;br /&gt;        static void Main(string[] args)
&lt;br /&gt;        {
&lt;br /&gt;            try
&lt;br /&gt;            {
&lt;br /&gt;                MessageQueue privateQueue;
&lt;br /&gt;                if (!MessageQueue.Exists(".\\Private$\\privateQueue"))
&lt;br /&gt;                    privateQueue = MessageQueue.Create(".\\Private$\\privateQueue");
&lt;br /&gt;                else
&lt;br /&gt;                    privateQueue = new MessageQueue(".\\Private$\\privateQueue");
&lt;br /&gt;
&lt;br /&gt;                privateQueue.Send("Hello, World !", "Message Label");
&lt;br /&gt;                MessageQueue anotherPrivateQueue = new MessageQueue(".\\Private$\\privateQueue");
&lt;br /&gt;                System.Messaging.Message msg = anotherPrivateQueue.Receive();
&lt;br /&gt;                msg.Formatter = new System.Messaging.XmlMessageFormatter(new Type[1] { typeof(string) });
&lt;br /&gt;                Console.WriteLine(msg.Body.ToString());
&lt;br /&gt;            }
&lt;br /&gt;            catch (Exception ex)
&lt;br /&gt;            {
&lt;br /&gt;                Console.WriteLine(ex.Message);
&lt;br /&gt;            }
&lt;br /&gt;        }
&lt;br /&gt;    }
&lt;br /&gt;}
&lt;br /&gt;
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;p&gt;If you run the project, you will see a message-box displaying “Hello, World !”. This message content created by had been sent from MSMQ queue, received by another MSMQ, and processed. In our example both the sending and the receiving queues exist in the same application for the sake of simplicity. In real world applications they reside in very different areas. &lt;/p&gt;&lt;p&gt;This is a very basic introduction to MSMQ and non-transactional queues. In future posts, we will explore more advanced topics in MSMQ.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-3381795044346782183?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/gYgsrQaiqPY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/gYgsrQaiqPY/introduction-to-microsoft-message.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-iRV60VxSKZo/TsUC7ooDFjI/AAAAAAAAAKs/2gB3XueW_ZU/s72-c/MSMQ1_thumb%25255B1%25255D.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/11/introduction-to-microsoft-message.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-438810458685811864</guid><pubDate>Fri, 21 Oct 2011 17:19:00 +0000</pubDate><atom:updated>2011-11-04T21:53:39.699+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unit testing</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><title>Asserts in NUnit</title><description>&lt;p&gt;Assertions (or asserts for short) are helper methods that can assist us in determining whether a method under test is performing correctly or not. They let us assert conditions, data, and so on. All assertion methods will report failures (that’s when the assertion is false) or errors (that’s when we get an unexpected exception) and report these through the NUnit test runner. when a failure or error occurs, execution of the current test method is aborted. Other Test within the same test fixture will still be run.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Classic Asserts&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Assert.AreEqual (&lt;em&gt;expected&lt;/em&gt;, &lt;em&gt;actual&lt;/em&gt;, string &lt;em&gt;message&lt;/em&gt;)       &lt;ul&gt;       &lt;li&gt;For floating-point numbers we need to specify the tolerance of the equality. &lt;/li&gt;        &lt;li&gt;Assert.AreEqual(3.33, 10.0/3.0, 0.01)&amp;#160; // Checks the result but looks only for the first two decimal places. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Assert.Less(x, y)&amp;#160;&amp;#160;&amp;#160;&amp;#160; // asserts that x &amp;lt; y &lt;/li&gt;    &lt;li&gt;Assert.Greater(x, y)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // asserts that x &amp;gt; y &lt;/li&gt;    &lt;li&gt;Assert.LessOrEqual(x, y)&amp;#160;&amp;#160;&amp;#160; // asserts that x &amp;lt;= y &lt;/li&gt;    &lt;li&gt;Assert.GreaterOrEqual(x, y)&amp;#160;&amp;#160;&amp;#160; // asserts that x &amp;gt;= y &lt;/li&gt;    &lt;li&gt;Assert.IsNull(object, string &lt;em&gt;message&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;Assert.IsNotNull(object, string &lt;em&gt;message&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;Assert.AreSame(expected, actual, string &lt;em&gt;message&lt;/em&gt;) &lt;/li&gt;    &lt;li&gt;Assert.IsTrue(bool condition, string &lt;em&gt;message)&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;Assert.IsFalse(bool condition, string &lt;em&gt;message)&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;Assert.Fail(string &lt;em&gt;message) &lt;/em&gt;      &lt;ul&gt;       &lt;li&gt;Fails the test immediately, can be used to mark sections of code that should not be reached. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;*&lt;em&gt;message&lt;/em&gt; in all previous statements is optional.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Constrain-Based Asserts&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;These are new assertion style introduced in NUnit 2.4. That new style enable many expectations to be evaluated together in a single assertion:&lt;/p&gt;  &lt;p&gt;Assert.That(4, Is.LessThan(5) &amp;amp; Is.GreatThan(0));&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Assert.That(actual, &lt;strong&gt;Is.EqualTo&lt;/strong&gt;(expected)) &lt;/li&gt;    &lt;li&gt;Assert.That(actual, &lt;strong&gt;Is.Not.EqualTo&lt;/strong&gt;(expected)); &lt;/li&gt;    &lt;li&gt;Assert.That(actual, &lt;strong&gt;Is.AtMost&lt;/strong&gt;(expected));&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //equivalent to Assert.LessOrEqual() &lt;/li&gt;    &lt;li&gt;Assert.That(actual, &lt;strong&gt;Is.Atleast&lt;/strong&gt;(expected));&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //equivalent to Assert.GreaterOrEqual() &lt;/li&gt;    &lt;li&gt;Assert.That(expected, &lt;strong&gt;Is.Null&lt;/strong&gt;); &lt;/li&gt;    &lt;li&gt;Assert.That(expected, &lt;strong&gt;Is.Not.Null&lt;/strong&gt;);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //equivalent to Assert.That(expected, !Is.Null); &lt;/li&gt;    &lt;li&gt;Assert.That(expected, &lt;strong&gt;Is.Empty&lt;/strong&gt;);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; //for collections and strings &lt;/li&gt;    &lt;li&gt;Assert.That(actual, &lt;strong&gt;Is.InstanceOfType&lt;/strong&gt;(expected)); &lt;/li&gt;    &lt;li&gt;Assert.That(actual, &lt;strong&gt;Has.Length&lt;/strong&gt;(expected)); &lt;/li&gt;    &lt;li&gt;Assert.That(actualCollection, &lt;strong&gt;List.Contains&lt;/strong&gt;(expectedValue));       &lt;ul&gt;       &lt;li&gt;Assert.That({5, 3, 2}, List.contains(2))&amp;#160;&amp;#160; //will pass &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Assert.That(actualCollection, &lt;strong&gt;Is.SubsetOf&lt;/strong&gt;(expectedCollection));       &lt;ul&gt;       &lt;li&gt;Assert.That({5, 3, 2}, Is.SubsetOf({5, 4, 3, 2, 1}) &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Assert.That(&lt;em&gt;actual&lt;/em&gt;, &lt;strong&gt;Text.StartsWith&lt;/strong&gt;(&lt;em&gt;expected&lt;/em&gt;)); //asserts that the &lt;em&gt;expected&lt;/em&gt; string is at the beginning of &lt;em&gt;actual&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;Assert.That(&lt;em&gt;actual&lt;/em&gt;, &lt;strong&gt;Text.Matches&lt;/strong&gt;(&lt;em&gt;expected&lt;/em&gt;));&amp;#160;&amp;#160;&amp;#160; //asserts that the &lt;em&gt;expected&lt;/em&gt; regular expression string matches &lt;em&gt;actual&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;FileAssert.AreEqual(FileInfo expected, FileInfo actual);      &lt;ul&gt;       &lt;li&gt;FileAssert.AreEqual(String pathToExpected, String PathToActual); &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;NUnit and Exceptions&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Part of your code behavior may be throwing a specific exception in certain cases. If you want to assert that your code throws the exceptions that it designed to throw, you need to tag your test method with attribute: &lt;strong&gt;[ExpectedException(typeof(&lt;em&gt;ExpectedException&lt;/em&gt;))]&lt;/strong&gt;. &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ImportListTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    [ExpectedException(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ArgumentNullException))]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; NullList()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        Class1.ImportList(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This test method in now expected to throw an exception [from the call to ImportList(null)]. If it doesn’t, the test will fail. If a different exception is thrown (even a superclass of the one specified), the test fails. The test passes only if the exact exception specified is thrown.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Temporarily ignoring tests&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If you wrote the test code first and will start writing code incrementally (something like TDD), you may temporarily ignore tests that you still implementing the code required to pass them. You could just tag these tests with attribute &lt;strong&gt;[Ignore(“Optional Message”)] .&lt;/strong&gt; NUnit will report that these tests were skipped and show a yellow bar in the GUI, so you won’t forget about it later.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-438810458685811864?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/BBpu8V8IqJg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/BBpu8V8IqJg/asserts-in-nunit.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/10/asserts-in-nunit.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-9169556324814322059</guid><pubDate>Fri, 21 Oct 2011 17:19:00 +0000</pubDate><atom:updated>2011-11-04T21:54:02.356+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unit testing</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><title>Structuring unit tests in NUnit</title><description>&lt;p&gt;Our goal in the previous post Introduction to NUnit, was just to introduce the unit testing using NUnit as simple as possible. Now it is the time to elaborate more on NUnit framework, structuring and organizing your test cases. If you examined the test code again:&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ClassLibrary1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    [TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CmpTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LargestOf3()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;[] { 8, 9, 7 };&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            Assert.That(Cmp.Largest(numbers), Is.EqualTo(9));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The code is pretty straightforward. The &lt;font color="#0000ff"&gt;using &lt;/font&gt;statement on line 2 brings in the necessary NUnit classes. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Next, we have the class definition on line 6: each class that contains tests must be annotated with a [TestFixture] attribute. The class have to be public, so the test runners can run it. The class must have a public, no-parameter constructor.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Finally, the test class contains individual methods annotated with [Test] attributes. Any public, parameterless method specified with a [Test] attribute will be run automatically by NUnit.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Structuring Unit Tests&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As a good object-oriented design concept, a class should be focused on one responsibility and only one. This applies to test fixtures as well. Test fixtures should be focused on verifying behavior in a specific scenario. Its name should reflect this scenario. Tests inside that fixture should test different behaviors within this scenario. Tests should be organized around &lt;em&gt;behaviors&lt;/em&gt;, not necessarily individual methods. To keep things readable and clear in the test runner output, favor putting fixture classes under a namespace that includes the name of the class that the fixture are testing. &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; solutionName.Test.ShoppingCartTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    [TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; EmptyCartFixture&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OverallRateIsZero() { }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Categories&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;NUnit provides an easy way to mark and run individual test and fixtures by using categories. A category is just a name that we define. We can associate different test methods with one or more categories, that enable us to select which categories we want to exclude (or include) when running the tests. A category is specified as an attribute. &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ClassLibrary1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    [TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CmpTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        [Category(&lt;span class="str"&gt;&amp;quot;Short&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LargestOf3()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;[] { 8, 9, 7 };&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            Assert.That(Cmp.Largest(numbers), Is.EqualTo(9));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        [Category(&lt;span class="str"&gt;&amp;quot;Long&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        [Category(&lt;span class="str"&gt;&amp;quot;Fred&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LargestOf3Alt()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;[3];&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;            numbers[0] = 1;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            numbers[1] = 1;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;            numbers[2] = 1;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;            Assert.That(Cmp.Largest(numbers), Is.EqualTo(1));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        [SetUp]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LargestOf3Alt2()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;[1];&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;            numbers[0] = 0;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;            Assert.That(Cmp.Largest(numbers), Is.EqualTo(0));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the GUI, if you excluded “Short” methods from run, only LargestOf3() will be selected and executed. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/-nXyJ5v9aObs/TqGpgt33HLI/AAAAAAAAAJo/zjY9Qasyy7A/s1600-h/NUnit35.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="NUnit3" border="0" alt="NUnit3" src="http://lh3.ggpht.com/-AWxRoAtM0NQ/TqGpg8H6mJI/AAAAAAAAAJw/ImDIFUJg3sI/NUnit3_thumb3.jpg?imgmax=800" width="801" height="492" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When categories are used, only the tests in the selected categories will be run. Those tests in categories that are not selected are not reported at all. If no category selected in the GUI, then all methods will be executed unless it is tagged with Explicit attribute. The Explicit attribute causes a test or test fixture to be ignored unless it is explicitly selected for running.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Per-Method Setup and Teardown&lt;/u&gt;&amp;#160; &lt;/strong&gt;We should write our test in order that each test can run independently of other tests; this allows us to run any test at any time in any order. In order to accomplish that we may need to reset things between tests or clean up after a test has run. [Setup] and [TearDown] attributes are used for these tasks.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Per-Fixture Setup and Teardown&lt;/u&gt;&amp;#160; &lt;/strong&gt;If we need to set something up or clean up after the entire test class has run, use [TestFixtureSetup] and [TestFixtureTearDown] for that.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Although setup and teardown methods generally come in pair, they don’t have to do so.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Suppose we need some sort of database connection for each test. Rather than duplicating code in each test method that connects to and disconnects from the database, we could use SetUp and TearDown methods. Since creating the initial connection to the database can be slow, we may want to do that only once before all the tests run by using TestFixtureSetUp.&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt; &lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; DBTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;private&lt;/span&gt; SqlConnection dbConn;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;            &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    [TestFixtureSetUp]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerFixtureSetup()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        dbConn = &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlConnection(&lt;span class="str"&gt;&amp;quot;ConnectionString&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        dbConn.Open();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    [SetUp]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerTestSetup()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        &lt;span class="rem"&gt;//populate database with test data&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    [TearDown]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerTestTearDown()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        &lt;span class="rem"&gt;// clean up database&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    [TestFixtureTearDown]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PerFixtureTearDown()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;        dbConn.Close();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;        dbConn.Dispose();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;    [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AccountAccess()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;        &lt;span class="rem"&gt;// Uses dbConn&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;    [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; EmployeeAccess()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;        &lt;span class="rem"&gt;// Uses dbConn&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In this example, here is the methods run sequence: PerFixtureSetup &amp;gt;&amp;gt; PerTestSetup &amp;gt;&amp;gt; AccountAccess &amp;gt;&amp;gt; PerTestTearDown &amp;gt;&amp;gt; PerFixtureTearDown &amp;gt;&amp;gt; PerFixtureSetup &amp;gt;&amp;gt; PerTestSetup &amp;gt;&amp;gt; EmployeeAccess &amp;gt;&amp;gt; PertestTearDown &amp;gt;&amp;gt; PerFixtureTearDown.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In this post we tried to elaborate more on the NUnit framework and how we structure and organize test cases in NUnit.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-9169556324814322059?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/rKz8Aaw_UfU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/rKz8Aaw_UfU/structuring-unit-tests-in-nunit.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-AWxRoAtM0NQ/TqGpg8H6mJI/AAAAAAAAAJw/ImDIFUJg3sI/s72-c/NUnit3_thumb3.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/10/structuring-unit-tests-in-nunit.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-8620442067718593403</guid><pubDate>Fri, 21 Oct 2011 17:17:00 +0000</pubDate><atom:updated>2011-11-04T21:51:57.532+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unit testing</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><title>Introduction to NUnit</title><description>&lt;p&gt;&lt;u&gt;&lt;strong&gt;What is unit testing?&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;According to &lt;a href="http://www-128.ibm.com/developerworks/library/j-test.html"&gt;Jeff Canna&lt;/a&gt;, unit testing ensures that a particular method of a class successfully performs a set of specific tasks. Each test confirms that a method produces the expected output when given a known input.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;What is NUnit?&lt;/u&gt; &lt;/strong&gt;NUnit is an open source framework that facilitates unit testing for all .NET languages. It allows you to build your test cases directly into the code of the project.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;How do I get NUnit?&lt;/u&gt; &lt;/strong&gt;Download the appropriate file from &lt;a href="http://www.nunit.org/index.php?p=download"&gt;here&lt;/a&gt; and install it. That’s it !!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Write your code&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;We will write our code now and let it drive our testing cases and exploration of NUnit. Make a new project, class library that similar to the code below. Below an implementation of a “largest” method that returns the largest number in a list. &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ClassLibrary1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Cmp&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Largest(&lt;span class="kwrd"&gt;int&lt;/span&gt;[] list)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        {            &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt; max = Int32.MaxValue;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; index = 0; index &amp;lt; list.Length - 1; index++)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (list[index] &amp;gt; max)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                    max = list[index];&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; max;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Test your code&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now it comes to the testing step. Writing test cases is a big topic to be discussed here. We will try to make our first test as simple as possible because there is much to be tested the first time besides the code itself. First, add reference to NUnit and then write our test cases.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;Right click on the references in “Solution Explorer” and click add a reference. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Click on tab “Browse” &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Navigate to your NUnit directory and go into the bin subdirectory. For me this directory is in “C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework” &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Select &lt;u&gt;nunit.framework.dll&lt;/u&gt; and click OK. &lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Then add a new class library to the project and add the code below to it. This code uses the NUnit namespace, attributes, Assert, and Is. We will explain all of these in detail in further posts. For now, just build the project.&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ClassLibrary1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    [TestFixture]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CmpTest&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        [Test]&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LargestOf3()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            &lt;span class="kwrd"&gt;int&lt;/span&gt;[] numbers = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt;[] { 8, 9, 7 };&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            Assert.That(Cmp.Largest(numbers), Is.EqualTo(9));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Run your tests&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now we have got our code, and our test cases. The next step is to run that test cases to test our code and that is the role of &lt;em&gt;test runners. &lt;/em&gt;A test runner knows to look for the [TestFixture] attribute of a class and for the [Test] methods within it. The runner will run the tests, accumulate some statistics on which tests passed and failed, and report the results to you. There are many test runners, you can use the NUnit GUI which is installed by default with NUnit (will be located in the programs menu of the start menu).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;&lt;u&gt;Using NUnit GUI&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the NUnit GUI, select File &amp;gt; Open or press Ctrl + O. This would show the Open Project dialog. Browse to the folder which contains the assembly containing your tests and open it. &lt;br /&gt;  &lt;br /&gt;&lt;a href="http://lh5.ggpht.com/-ZzfW7Wivl68/TqGpFxFkxxI/AAAAAAAAAJA/MxxwI9pvD7Y/s1600-h/NUnit14.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="NUnit1" border="0" alt="NUnit1" src="http://lh5.ggpht.com/-ZpqTaDf8XsM/TqGpGCkzcaI/AAAAAAAAAJI/K7cQKbo6JwI/NUnit1_thumb2.jpg?imgmax=800" width="559" height="340" /&gt;&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;Now you can test your code by clicking the Run button. &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;a href="http://lh4.ggpht.com/-pyKIWMEDcu4/TqGpGQw5GhI/AAAAAAAAAJQ/5aPwJG1wfmw/s1600-h/NUnit24.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="NUnit2" border="0" alt="NUnit2" src="http://lh4.ggpht.com/-T9Nd1jHgELU/TqGpGhO4YFI/AAAAAAAAAJY/ddP8YPT94tc/NUnit2_thumb2.jpg?imgmax=800" width="521" height="317" /&gt;&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;When we run a selected test, the GUI will display a large, colored, status bar. If all the tests pass, the bar is bright green. If any test fails, the bar is red. If any test was skipped, the bar is yellow.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It is obvious that our test case here have been failed. Try to fix the code and run the test again until it succeed &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="http://lh4.ggpht.com/-tPXK8-ulKw8/TqGpGyLTHlI/AAAAAAAAAJg/fFvD3ixzxB8/wlEmoticon-smile2.png?imgmax=800" /&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-8620442067718593403?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/V_tNejnR9B8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/V_tNejnR9B8/introduction-to-nunit.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-ZpqTaDf8XsM/TqGpGCkzcaI/AAAAAAAAAJI/K7cQKbo6JwI/s72-c/NUnit1_thumb2.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/10/introduction-to-nunit.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-8591507174850617619</guid><pubDate>Mon, 17 Oct 2011 02:48:00 +0000</pubDate><atom:updated>2011-10-17T04:48:18.643+02:00</atom:updated><title>Log4net Extended Tutorial</title><description>&lt;p&gt;Logging is one of the most effective debugging and troubleshooting techniques although some engineers claim that it is not that helpful or it will slow the application. My experience with it is always positive. &lt;/p&gt;  &lt;p&gt;I use &lt;a href="http://logging.apache.org/log4net/index.html" target="_blank"&gt;log4net&lt;/a&gt; from the &lt;a href="http://www.apache.org/" target="_blank"&gt;Apache Software Foundation&lt;/a&gt; as my logging layer for .Net applications. log4net is available &lt;a href="http://logging.apache.org/log4net/download.html"&gt;here&lt;/a&gt; as a zip archive.&lt;/p&gt;  &lt;h5&gt;Hello World Example &lt;font style="font-weight: normal"&gt;[uses &lt;em&gt;BasicConfigurator&lt;/em&gt;]&lt;/font&gt;&lt;/h5&gt;  &lt;ol&gt;   &lt;li&gt;Download log4net from &lt;a href="http://logging.apache.org/log4net/download.html"&gt;here&lt;/a&gt;. &lt;/li&gt;    &lt;li&gt;Open Visual Studio and create a new Console application project. &lt;/li&gt;    &lt;li&gt;Add to the project a reference to the \bin\net\2.0\release\log4net.dll assembly in the log4net distribution. &lt;/li&gt;    &lt;li&gt;Modify your Main() method like so: &lt;/li&gt; &lt;/ol&gt; &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            log4net.Config.BasicConfigurator.Configure();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            log4net.ILog log = log4net.LogManager.GetLogger(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Program));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            log.Debug(&lt;span class="str"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            log.Info(&lt;span class="str"&gt;&amp;quot;Just info.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            log.Warn(&lt;span class="str"&gt;&amp;quot;careful this is a warning.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            log.Error(&lt;span class="str"&gt;&amp;quot;Bad news: Error occurred.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            log.Fatal(&lt;span class="str"&gt;&amp;quot;Here is the end.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Compile and run the application, and you'll see output to the console like so:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh5.ggpht.com/-1HbirpfdUDI/TpuXZavA1pI/AAAAAAAAAIA/c8JwR4YM8p8/s1600-h/log4net14.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="log4net1" border="0" alt="log4net1" src="http://lh3.ggpht.com/-kPnzbo6YZtM/TpuXZ6KYXvI/AAAAAAAAAII/e0racliecz8/log4net1_thumb2.jpg?imgmax=800" width="732" height="388" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the Hello World example we configured log4net in the most basic way possible just to get started. Using the BasicConfigurator (line 8) will cause log4net to output log entries to the console using the default layout. Line 9 requests a &lt;em&gt;logger &lt;/em&gt;from the LogManager object.&amp;#160; Logger objects implement the ILog interface, which is what your application will use to instrument itself and percolate log entries.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Lines 11-15 use the logger to log a few statements with various &lt;em&gt;severity levels&lt;/em&gt;.&amp;#160; log4net defines 5 such levels:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;Debug&lt;/strong&gt;: fine-grained statements concerning program state, typically used for debugging; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;Info&lt;/strong&gt;: informational statements concerning program state, representing program events or behavior tracking; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;Warn&lt;/strong&gt;: statements that describe potentially harmful events or states in the program; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;Error&lt;/strong&gt;: statements that describe non-fatal errors in the application; this level is used quite often for logging handled exceptions; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;Fatal&lt;/strong&gt;: statements representing the most severe of error conditions, assumedly resulting in program termination. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;XML Configurator&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the previous example we used the BasicConfigurator. If we just changed the configurator to XmlConfigurator and run the application you will get the following output error:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;font color="#0000ff"&gt;log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in &lt;br /&gt;    &lt;br /&gt;the application's .config file. Check your .config file for the &amp;lt;log4net&amp;gt; and &amp;lt; &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;configSections&amp;gt; elements. The configuration section should look like: &amp;lt;section n &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;ame=&amp;quot;log4net&amp;quot; type=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler,log4net&amp;quot; / &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;gt;&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This error resulted because XMLConfigurator relies on an XML document to supply configuration for log4net. The static Configure method have 10 overloads to accept the XML configuration from a file, stream, URI, or an XmlElement object. The parameterless overload of the method we used instructs log4net to load the XML from the application’s configuration file. Since our application doesn’t contain any configuration files (till now), the Configure method returned the above error.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To overcome this error, add Application Configuration File to the project and add the following XML configuration:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.ConsoleAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ALL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Compile and run, you will see output changed to be like so:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh6.ggpht.com/-841B0VRGiM0/TpuXaEEWiPI/AAAAAAAAAIQ/EN4zl87Vh7U/s1600-h/log4net26.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="log4net2" border="0" alt="log4net2" src="http://lh5.ggpht.com/-kSibZT6Yjkc/TpuXahgfVYI/AAAAAAAAAIY/b2wqUiGyDeI/log4net2_thumb4.jpg?imgmax=800" width="744" height="403" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In addition to the Configure method, XmlConfigurator offers a static ConfigureAndWatch() method that accepts a FileInfo reference. ConfigureAndWatch() will monitor the XML configuration file and reconfigure log4net when a change is detected in this file.&amp;#160; This allows you to alter logging behavior while the application is running to fit your current troubleshooting requirements.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If we look at the application configurations file, the declaration of the log4net configuration section on line 3 is mandatory. The log4net configurations section [lines 6-15] consists of two sub-sections: &lt;em&gt;appender&lt;/em&gt; and &lt;em&gt;root.&lt;/em&gt; log4net appender is the place where your application log entries end up (Console in our example). We will talk about appenders later. The root logger controls the general behavior of log4net.&amp;#160; In this example, the root logger is told to send everything to the console appender.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Taming log4net Output&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the previous examples we were logging all types of severity levels. If you want to filter the logging output to fit your application’s life current phase (development, production) or troubleshooting needs, you can just change the value of &amp;lt;level /&amp;gt; XML element in your app.config.&amp;#160; The level value instructs the logger of the minimum level at which log entries should be processed. It can be: ALL (everything is enabled), DEBUG (identical to ALL), INFO (everything except DEBUG), WARN (warn, Error, and Fatal), ERROR (Error and Fatal), FATAL (only Fatal), OFF (logging is disabled).&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;font style="font-weight: normal"&gt;The Structure of log4net&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Before going further with log4net, let’s talk about its structure. log4net built using the layered approach, with four main components inside of the framework. These are Logger, Repository, Appender, and Layout. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The &lt;i&gt;Logger&lt;/i&gt; is the main component with which your application interacts. It is also the component that generates the log messages. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The &lt;em&gt;Repository&lt;/em&gt; is responsible for maintaining the logical organization of loggers inside the framework. Currently log4net only supported the hierarchical organization through &lt;code&gt;log4net.Repository.Hierarchy&lt;/code&gt; namespace. If you want to extend log4net to implement new organization, you have to implement the &lt;code&gt;log4net.Repository.ILoggerRepository&lt;/code&gt; interface or inherit class &lt;code&gt;log4net.Repository.LoggerRepositorySkeleton&lt;/code&gt;. Almost all developers do not use any of these &lt;code&gt;Repository&lt;/code&gt; classes; they only use the &lt;code&gt;LogManager&lt;/code&gt; class to automatically manage the repositories and the loggers.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The &lt;em&gt;Appender&lt;/em&gt; is used to define the output medium that log statements will go for it at the end of the day. As the name suggests, these components append themselves to the Logger component and relay the output to an output stream. You can append multiple appenders to a single logger. There are several appenders provided by the &lt;code&gt;log4net&lt;/code&gt; framework. If you want to write your own appender, you can start by inheriting the&lt;code&gt;log4net.Appender.AppenderSkeleton&lt;/code&gt; class, which works as an adapter between your class and the &lt;code&gt;IAppender&lt;/code&gt; interface. You can use &lt;i&gt;Appender Filters&lt;/i&gt; to filter logging events sent from appenders before processing it.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The &lt;i&gt;Layout&lt;/i&gt; component is used to display the final formatted output to the user. The output can be shown in multiple formats, depending upon the layout we are using. It can be linear or an XML file. The layout component works with an appender. There is a list of different layouts in the API documentation. You cannot use multiple layouts with an appender. To create your own layout, you need to inherit the&lt;code&gt;log4net.Layout.LayoutSkeleton&lt;/code&gt; class, which implements the &lt;code&gt;ILayout&lt;/code&gt; interface.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;font style="font-weight: normal"&gt;Appenders&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;An appender is an object that persists your log messages someplace. In the table below a list of the appenders provided by log4net. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;Appender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Description &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;AdoNetAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appender that logs to a database. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;AnsiColorTerminalAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends logging events to the terminal using ANSI color escape sequences. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;AspNetTraceAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends log events to the ASP.NET TraceContext system. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;BufferingForwardingAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Buffers events and then forwards them to attached appenders. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;ColoredConsoleAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends logging events to the console colored. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;ConsoleAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends logging events to the console. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;DebugAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends log events to the Debug system. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;EventLogAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Writes events to the system event log. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;ForwardingAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;This appender forwards logging events to attached appenders. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;FileAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends logging events to a file. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;LocalSyslogAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Logs events to a local syslog service. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;MemoryAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Stores logging events in an array. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;NetSendAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Logs entries by sending network messages using the NetMessageBufferSend native function. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;OutputDebugStringAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends log events to the OutputDebugString system. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;RemoteSyslogAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Logs events to a remote syslog daemon. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;RemotingAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Delivers logging events to a remote logging sink. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;RollingFileAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appender that rolls log files based on size or date or both. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;SmtpAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Send an e-mail when a specific logging event occurs, typically on errors or fatal errors. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;SmtpPickupDirAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Send an email when a specific logging event occurs, typically on errors or fatal errors. Rather than sending via smtp it writes a file that another service, such as the IIS SMTP agent, can use to manage sending the messages. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;TelnetAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appender that allows clients to connect via Telnet to receive log messages. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;TraceAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Appends log events to the Trace system. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;UdpAppender &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Sends logging events as connectionless UDP datagrams to a remote host or multicast using the UdpClient class. &lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;FileAppender and RollingFileAppender &lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;These appenders write log messages to files. A typical configuration for the FileAppender might look like this: &lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;FileAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.FileAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;file&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appendToFile&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;encoding&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Configuration attributes and its description are:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;file&lt;/strong&gt;: the full or relative path to the log file; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;appendToFile&lt;/strong&gt;: boolean indicating whether the log file should be appended (true) or overwritten (false).&amp;#160; If false, the file overwrite occurs during log4net initialization.&amp;#160; If unspecified, the log file is appended; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;immediateFlush&lt;/strong&gt;: boolean indicating whether to flush the log file TextWriter after each log message is written.&amp;#160; The default is true (flush each message after its written); &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;lockingModel&lt;/strong&gt;: allows control over the log file locking strategy.&amp;#160; This can be either &amp;quot;log4net.Appender.FileAppender+MinimalLock&amp;quot; to allow for loose file locking or &amp;quot;log4net.Appender.FileAppender+ExclusiveLock&amp;quot; to lock the file during program execution.&amp;#160; &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Keep in mind that the log file managed by FileAppender will be allowed to grow without bounds. The RollingFileAppender provides basic log file management, configurable size- or date-boxing of the log file, and limited rolling backups of the log file.&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;RollingFileAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.RollingFileAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;file&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log-file.txt&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appendToFile&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;rollingStyle&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Size&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;maxSizeRollBackups&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;maximumFileSize&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1MB&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;staticLogFileName&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Configuration attributes and its description are:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;rollingStyle&lt;/strong&gt;: this controls how log files are &amp;quot;rolled,&amp;quot; and can be one of the following values: &lt;br /&gt;&lt;br /&gt;    &lt;ul&gt;&lt;br /&gt;      &lt;li&gt;&lt;strong&gt;Once&lt;/strong&gt;: the log file is rolled every time log4net is initialized (typically at application startup); &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;      &lt;li&gt;&lt;strong&gt;Size&lt;/strong&gt;: the log file is rolled once it breaches a certain size; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;      &lt;li&gt;&lt;strong&gt;Date&lt;/strong&gt;: the log file is rolled based on the current date; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;      &lt;li&gt;&lt;strong&gt;Composite&lt;/strong&gt;: the log file is rolled based on size constraints and the current date; &lt;/li&gt;&lt;br /&gt;    &lt;/ul&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;maximumFileSize&lt;/strong&gt;: the size cap on the log file.&amp;#160; This is an expression of size in the form of &amp;quot;#(KB|MB|GB)&amp;quot;.&amp;#160; For instance, &amp;quot;100KB&amp;quot; or &amp;quot;10MB&amp;quot;; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;maxSizeRollBackups&lt;/strong&gt;: the maximum number of rolled log file backups to maintain when rollingStyle is SIZE; when rollingStyle is COMPOSITE, this indicates the maximum number of roll-offs maintained &lt;em&gt;per day&lt;/em&gt;; this property has no effect when rollingStyle is ONCE or DATE; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;datePattern&lt;/strong&gt;: the date pattern used to roll files based on date.&amp;#160; The value of this parameter needs to adhere to the format used by the SimpleDateFormatter class; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;staticLogFileName&lt;/strong&gt;: a bit of a misnomer - when true this setting indicates whether log4net should actively write logs to the configured file (log-file.txt in our example configuration) and maintain rolling backups by copy.&amp;#160; When false, this setting indicates that log4net will actively log to the latest roll-off file (e.g., log-file1.txt, log-file2.txt, log-file3.txt, etc); &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;strong&gt;countDirection&lt;/strong&gt;: indicates how roll-off file numbering is managed.&amp;#160; When this parameter is &amp;gt;= 0, the newest log file will have the largest number; e.g., log-file.txt.5 will be newer than log-file.txt.4.&amp;#160; When countDirection &amp;lt; 0, the newest log file will have the lowest number; e.g., log-file.txt.1 will be newer than log-file.txt.2.&amp;#160; If unspecified, countDirection defaults to (-1); &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Keep in mind that when using a file appender, the user running the logging process must have rights to create and/or modify the log file in order for log messages to be written properly.&amp;#160; In addition, log4net will create the log file if it does not exist, but it will not create directories in the log file path that do not already exist.&amp;#160; If log4net encounters a problem initializing the file appender (e.g., it cannot create the log file for security reasons), the log file will not be written but your application will continue to execute normally.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Using multiple appenders&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can use multiple appenders by specifying each appender you need under the root logger.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;font style="font-weight: normal"&gt;Layouts and patterns&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A layout is just a template for your log messages.&amp;#160; Layouts are specified per-appender, and you can specify only one layout for an appender:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ColoredConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.ColoredConsoleAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The PatternLayout allows you to specify a printf-style template for your log entries using a &amp;quot;conversion pattern,&amp;quot; and gives you the opportunity to decorate each entry with some valuable instance data. For instance, this configuration: &lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ColoredConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.ColoredConsoleAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.PatternLayout&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;conversionPattern&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;%date [%thread] %-5level %logger - %message%newline&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;produces a log that appears like so: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh3.ggpht.com/-veh4_pqN5wM/TpuXa2f4jdI/AAAAAAAAAIg/in7y2Exwnw4/s1600-h/log4net45.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="log4net4" border="0" alt="log4net4" src="http://lh6.ggpht.com/-pHSaj8CXcUw/TpuXbbj5epI/AAAAAAAAAIo/W9zuW5Y7E-c/log4net4_thumb3.jpg?imgmax=800" width="761" height="380" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The conversion pattern string can include literal text and the following format expressions: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;&lt;b&gt;expression &lt;/b&gt;&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;&lt;b&gt;value&lt;/b&gt;&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%appdomain &lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the friendly name of the appdomain from which the log entry was made&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%date&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the local datetime when the log entry was made&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%exception&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;a formatted form of the exception object in the log entry, if the entry contains an exception; otherwise, this format expression adds nothing to the log entry&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%file&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the file name from which the log entry was made; note that using %file has a significant performance impact and I don't recommend using it&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%identity&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the user name of the active user logging the entry; this one is less reliable than %username; note that using %identity has a significant performance impact and I don't recommend using it&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%level&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the severity level of the log entry (DEBUG,INFO, etc)&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%line&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the source code line number from which the log entry was made; slow&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%location&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;some rudimentary call stack information, including file name and line number at which the log entry was made; using&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%logger&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the name of the logger making the entry; more on this in a bit&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%method&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the name of the method in which the log entry was made; also slow&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%message&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the log message itself (don't forget this part!)&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%newline&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the value of Environment.NewLine&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%timestamp&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the milliseconds between the start of the application and the time the log entry was made&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%type&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the full typename of the object from which the log entry was made&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%username&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the Windows identity of user making the log entry; slow&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%utcdate&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;the UTC datetime when the log entry was made&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;%%&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;a percent sign (%)&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;A common usage of the %logger expression is to identify the source class in the log entries. Give the class name to the logger like:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;private static log4net.ILog Log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );&amp;#160; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;and the output will be something like:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;ConsoleApplication1.Program [INFO]- this is an info message &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Using this technique can help you control logging concerns of your objects. For example, add a new class “MyClass” to your ConsoleApplication and use the above approach in declaring your loggers. Add the following after the &amp;lt;root&amp;gt; section in your app.config :&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleApplication1&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ALL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;FileAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleApplication1.Program&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ALL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleApplication1.MyClass&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ALL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;logger&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This way, the console output will display only the log statements from the Program class, the log file will contain the log statements from both Program class and MyClass class. Although MyClass doesn’t have an appender associated with it, the container namespace does and that appender who processes MyClass log statements. If you changed the level value to “OFF” for ConsoleApplication1.MyClass, the log file will not contain log statements from MyClass. When applying the previous technique keep into consideration the following:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Appenders accumulate through the hierarchy: if both the Program class and ConsoleApplication1 namespace are each configured to append to the console, any log from the Program class will show up twice in the console (once for the class logger and once for the namespace logger). &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Specific logger levels deeper in the hierarchy overcome the general levels : if the Program class logging level is set to ALL and the ConsoleApplication1 namespace logging level is set to OFF, logs from the program class are still written to the appenders configured for ConsoleApplication1. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Log event context&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Beside the expressions mentioned above, sometimes you need to log context information. Modify your console application to look so:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            log4net.Config.XmlConfigurator.Configure();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            log4net.ThreadContext.Properties[&lt;span class="str"&gt;&amp;quot;myContext&amp;quot;&lt;/span&gt;] = &lt;span class="str"&gt;&amp;quot;Logging from Main&amp;quot;&lt;/span&gt;;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            log.Info(&lt;span class="str"&gt;&amp;quot;Just info.&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            Console.ReadLine();  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now update the app.config to look like: &lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;      &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.ConsoleAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.PatternLayout&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;conversionPattern&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;%logger (%property{myContext}) [%level]- %message%newline&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ALL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Compile and run, you will got a log statement similar to:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;ConsoleApplication1.Program (Logging from Main) [INFO]- Just info.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;What we did here is that we added a property “myContext” to the ThreadContext static class and assigned a simple string value. This property processed by the appender through the &lt;font color="#0000ff"&gt;%property{myContext} &lt;font color="#000000"&gt;expression in the layout conversion pattern.&lt;/font&gt; &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There are three hierarchy logging contexts available in log4net, where properties in the more granular contexts override property values in less granular contexts.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;table border="1"&gt;&lt;tbody&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;Context&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Description&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;&lt;strong&gt;log4net.GlobalContext&lt;/strong&gt;&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;A global context shared across all application threads and app domains. If two threads set the same property on the GlobalContext, one value will overwrite the other.&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;&lt;strong&gt;log4net.ThreadContext&lt;/strong&gt;&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;Any properties set in this context are scoped to the calling thread. In other words, in this context two threads can set the same property to different values without stomping on each other.&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;&lt;br /&gt;    &lt;tr&gt;&lt;br /&gt;      &lt;td&gt;&lt;strong&gt;log4net.ThreadLogicalContext&lt;/strong&gt;&lt;/td&gt;&lt;br /&gt;&lt;br /&gt;      &lt;td&gt;This context behaves similarly to the ThreadContext, except that the scope is defined by logical thread boundaries. I'll be honest and say that I've never used the ThreadLogicalContext in my career, but if you're working with a custom thread pool algorithm or hosting the CLR, you may find some use for this one.&lt;/td&gt;&lt;br /&gt;    &lt;/tr&gt;&lt;br /&gt;  &lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Calculated context values&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Context property values don't have to be strings. You can set the value of a context property to any object reference; then the object's ToString can be used to obtain the needed context property when a logging event occurs. This technique could be used to log whatever state you want at the time of each logging event. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;ThreadContext Stacks&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;ThreadContext and ThreadLogicalContext can store property values in stacks available via the Stacks static property of each class. Pushing a property value onto a stack returns an IDisposable that, when disposed, pops the property value off of the stack.&amp;#160; The logging output reflects the state of the context stack at each logging event; the stack is represented as a space-delimited list, with newer items appearing later in the list. Its like printing the current call stack in every log statement. check this example:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main(&lt;span class="kwrd"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            log4net.Config.XmlConfigurator.Configure();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (log4net.ThreadContext.Stacks[&lt;span class="str"&gt;&amp;quot;myContext&amp;quot;&lt;/span&gt;].Push(&lt;span class="str"&gt;&amp;quot;Main&amp;quot;&lt;/span&gt;))&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                FirstAction();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;                SecondAction();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            Console.ReadLine();  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FirstAction()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (log4net.ThreadContext.Stacks[&lt;span class="str"&gt;&amp;quot;myContext&amp;quot;&lt;/span&gt;].Push(&lt;span class="str"&gt;&amp;quot;FirstAction&amp;quot;&lt;/span&gt;))&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                UtilityRoutine();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            }            &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SecondAction()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (log4net.ThreadContext.Stacks[&lt;span class="str"&gt;&amp;quot;myContext&amp;quot;&lt;/span&gt;].Push(&lt;span class="str"&gt;&amp;quot;SecondAction&amp;quot;&lt;/span&gt;))&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;                UtilityRoutine();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;                FirstAction();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;            }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UtilityRoutine()&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;            &lt;span class="kwrd"&gt;using&lt;/span&gt; (log4net.ThreadContext.Stacks[&lt;span class="str"&gt;&amp;quot;myContext&amp;quot;&lt;/span&gt;].Push(&lt;span class="str"&gt;&amp;quot;UtilityRoutine&amp;quot;&lt;/span&gt;))&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;                log.Info(&lt;span class="str"&gt;&amp;quot;this is an info message&amp;quot;&lt;/span&gt;);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;            }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;        }  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Compile and run, you will get these log messages: &lt;br /&gt;  &lt;br /&gt;ConsoleApplication1.Program (Main FirstAction UtilityRoutine) [INFO]- this is an info message &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;ConsoleApplication1.Program (Main SecondAction UtilityRoutine) [INFO]- this is an info message &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;ConsoleApplication1.Program (Main SecondAction FirstAction UtilityRoutine) [INFO]- this is an info message &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&amp;#160;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;font style="font-weight: normal"&gt;Filters&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Filters are applied to individual appenders via the log4net configuration, and they help the appender determine whether a log event should be processed by the appender. The following configuration defines two file appenders, each with a unique filter applied:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LogFileAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.FileAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;file&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log.txt&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;filter&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Filter.LevelRangeFilter&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;levelMin&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;levelMax&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;WARN&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;filter&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ErrorFileAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.FileAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;file&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;errors.txt&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;filter&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Filter.LevelRangeFilter&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;levelMin&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ERROR&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;levelMax&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;FATAL&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;filter&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LogFileAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ErrorFileAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The filter on the first appender will log statements with levels DEBUG, INFO, and WARN; the filter on the second appender will log statements with levels ERROR and FATAL. There are many filters available in log4net, we will review them quickly.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.LoggerMatchFilter&lt;/strong&gt; Filters log events based on the name of the logger object from which they are emitted. This filter can be configured using the following properties:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;loggerToMatch&lt;/u&gt;: a string value to match against the message's logger name.&amp;#160; The match is made using the String.StartsWith method; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;acceptOnMatch&lt;/u&gt;: a boolean value indicating whether a matching logger name results in accepting the message (true) or rejecting it (false).&amp;#160; This defaults to true, meaning that only matching logger names will be allowed into the appender. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.LevelMatchFilter&lt;/strong&gt; Filters log statements that match (or don’t match) a specific logging level. This filter can be configured using the following properties:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;levelToMatch&lt;/u&gt;: the log level to match - either DEBUG, INFO, WARN, ERROR, or FATAL; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;acceptOnMatch&lt;/u&gt;: a boolean value indicating whether to accept log levels matching the levelToMatch property (true), or reject log levels matching the levelToMatch property (false).&amp;#160; Defaults to true. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.LevelRangeFilter&lt;/strong&gt; Similar to the LevelMatchFilter, except that instead of filtering a single log level, this filters on an inclusive range of contiguous levels. This filter can be configured using the following properties:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;levelMin&lt;/u&gt;: the minimum log level to match - either DEBUG, INFO, WARN, ERROR, or FATAL; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;levelMax&lt;/u&gt;: the minimum log level to match - either DEBUG, INFO, WARN, ERROR, or FATAL; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;acceptOnMatch&lt;/u&gt;: a boolean value indicating whether to accept log levels matching the levelToMatch property (true), or to punt filtering to the next filter in the chain (false).&amp;#160; Defaults to true.&amp;#160; Note that any log level outside of the [levelMin, levelMax] range is denied by this filter. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.StringMatchFilter&lt;/strong&gt; Filters log events based on a string or regular expression match against the log message. This filter can be configured using the following properties:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;regexToMatch&lt;/u&gt;: a regular expression to match against the log message.&amp;#160; Note this regex is created with the Compiled option enabled for performance; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;stringToMatch&lt;/u&gt;: a static string to match against the log message.&amp;#160; The match is made using the String.IndexOf method to see if the static string exists in the log message, which is a case-sensitive search. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;acceptOnMatch&lt;/u&gt;: a boolean value indicating whether to accept log messages matching the string or regex (true), or to deny log messages matching the string or regex (false).&amp;#160; Defaults to true.&amp;#160; &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.PropertyFilter &lt;/strong&gt;Filters log events based on a value or regular expression match against a specific context property.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;key&lt;/u&gt;: the name of the property value to match; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;regexToMatch&lt;/u&gt;: a regular expression to match against the specified property value.&amp;#160; Note this regex is created with the Compiled option enabled for performance; &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;stringToMatch&lt;/u&gt;: a static string to match against the specified property value.&amp;#160; The match is made using the String.IndexOf method to see if the static string exists in the property value, which is a case-sensitive search. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;u&gt;acceptOnMatch&lt;/u&gt;: a boolean value indicating whether to accept messages with a property value matching the string or regex (true), or to deny messages with a property value matching the string or regex (false).&amp;#160; Defaults to true. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;log4net.Filter.DenyAllFilter&lt;/strong&gt; This filter simply denies all filtering.&amp;#160; When this is used, it's always at the end of a filter chain to block unwanted log messages from the appender.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;&lt;font style="font-weight: normal"&gt;Lossy logging&lt;/font&gt;&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If you want comprehensive log to be there in case of errors but you worry about the resources consumed by logging, you might use lossy logging.&amp;#160; Lossy logging gives the best of both worlds, under normal operation your application will not log any messages; however, if your application logs an error, a small batch of messages leading up to the error is placed into the log, giving you the error and a snapshot of system activity just before it happened.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Lets modify our code to be like:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; ConsoleApplication1&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;class&lt;/span&gt; Program&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main( &lt;span class="kwrd"&gt;string&lt;/span&gt;[] args )&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            log4net.Config.XmlConfigurator.Configure();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            &lt;span class="kwrd"&gt;for&lt;/span&gt;( &lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 100; ++i )&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            {&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                Log.DebugFormat( &lt;span class="str"&gt;&amp;quot;this is debug msg #{0}&amp;quot;&lt;/span&gt;, i );&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            }                &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            Log.Error( &lt;span class="str"&gt;&amp;quot;error: an error occurred!&amp;quot;&lt;/span&gt; );           &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            Log.Warn( &lt;span class="str"&gt;&amp;quot;warning: you've been warned&amp;quot;&lt;/span&gt; );&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            Console.ReadLine();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's modify our app.config to look like:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="html"&gt;xml&lt;/span&gt; &lt;span class="attr"&gt;version&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;encoding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;section&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configSections&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.ConsoleAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;layout&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Layout.SimpleLayout&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LossyConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Appender.BufferingForwardingAppender&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;bufferSize&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;lossy&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;evaluator&lt;/span&gt; &lt;span class="attr"&gt;type&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;log4net.Core.LevelEvaluator&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;threshold&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ERROR&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;evaluator&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;appender&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;level&lt;/span&gt; &lt;span class="attr"&gt;value&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;DEBUG&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;appender-ref&lt;/span&gt; &lt;span class="attr"&gt;ref&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LossyConsoleAppender&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;root&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;log4net&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Compile and run, you will see output like: &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a href="http://lh4.ggpht.com/-N8_s5KkwyD8/TpuXbr5iCLI/AAAAAAAAAIw/XQ4STy5OuAM/s1600-h/log4net5%25255B1%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="log4net5" border="0" alt="log4net5" src="http://lh5.ggpht.com/-u44gUB440QA/TpuXcBXBUPI/AAAAAAAAAI4/2QVnZyi5U5w/log4net5_thumb.jpg?imgmax=800" width="744" height="406" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The logging configuration defines two appenders, one very generic Console appender and a BufferingForwardingAppender.&amp;#160; As the name implies, the latter appender buffers log messages and forwards them in batches to one or more other appenders.&amp;#160; You can probably tell from the configuration XML that I've set this appender up with a 20-message buffer.&amp;#160; The lossy and evaluator parameters work together to define when the log message buffer is forwarded to the &amp;quot;base&amp;quot; appender.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The program issues a total of 102 log messages (100 DEBUG, one ERROR, and one WARN), but the console only contains 20 messages (19 DEBUG and 1 ERROR).&amp;#160; So what happened to the other 82 DEBUG and WARN messages?&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When the BufferingForwardingAppender's lossy property is enabled, the appender will buffer log messages without forwarding them.&amp;#160; If the buffer fills up, the oldest messages are dropped from the buffer to make room for the new messages.&amp;#160; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The evaluator property determines when the appender forwards the messages from the buffer to its base appenders.&amp;#160; There is only one evaluator defined in log4net - the LevelEvaluator.&amp;#160; The LevelEvaluator triggers the forward when a log message is received that meets or exceeds the configured threshold.&amp;#160; The example above is configured so that an ERROR message triggers the appender to forward its buffer to the ConsoleAppender.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Lossy Appender Types log4net provides the following appenders that can operate “lossy” :&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;log4net.Appender.AdoNetAppender &lt;br /&gt;  &lt;br /&gt;log4net.Appender.RemotingAppender &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;log4net.Appender.SmtpAppender &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;log4net.Appender.SmtpPickupDirAppender &lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;log4net.Appender.BufferingForwardingAppender&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Configuring log4net Programmatically&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Sometimes we are in the mood to code as quickly as possible without getting into configuration files. Normally, that happens when we are trying to test something. In that case, you have another way to do the configuration. All of the long configuration files that we saw in the previous section can be defined programmatically using a few lines of code. See the following code:&lt;/p&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;style type="text/css"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;.csharpcode, .csharpcode pre&lt;br /&gt;{&lt;br /&gt;	font-size: small;&lt;br /&gt;	color: black;&lt;br /&gt;	font-family: consolas, "Courier New", courier, monospace;&lt;br /&gt;	background-color: #ffffff;&lt;br /&gt;	/*white-space: pre;*/&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode pre { margin: 0em; }&lt;br /&gt;&lt;br /&gt;.csharpcode .rem { color: #008000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .kwrd { color: #0000ff; }&lt;br /&gt;&lt;br /&gt;.csharpcode .str { color: #006080; }&lt;br /&gt;&lt;br /&gt;.csharpcode .op { color: #0000c0; }&lt;br /&gt;&lt;br /&gt;.csharpcode .preproc { color: #cc6633; }&lt;br /&gt;&lt;br /&gt;.csharpcode .asp { background-color: #ffff00; }&lt;br /&gt;&lt;br /&gt;.csharpcode .html { color: #800000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .attr { color: #ff0000; }&lt;br /&gt;&lt;br /&gt;.csharpcode .alt &lt;br /&gt;{&lt;br /&gt;	background-color: #f4f4f4;&lt;br /&gt;	width: 100%;&lt;br /&gt;	margin: 0em;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="csharpcode"&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="rem"&gt;// using a FileAppender with a PatternLayout&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;log4net.Config.BasicConfigurator.Configure( &lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Appender.FileAppender(&lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Layout.PatternLayout(&lt;span class="str"&gt;&amp;quot;%d [%t]%-5p %c [%x] &amp;amp;lt;%X{auth}&amp;amp;gt; - %m%n&amp;quot;&lt;/span&gt;),&lt;span class="str"&gt;&amp;quot;testfile.log&amp;quot;&lt;/span&gt;));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="rem"&gt;// using a FileAppender with an XMLLayout&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;log4net.Config.BasicConfigurator.Configure( &lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Appender.FileAppender(&lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Layout.XMLLayout(),&lt;span class="str"&gt;&amp;quot;testfile.xml&amp;quot;&lt;/span&gt;));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="rem"&gt;// using a ConsoleAppender with a PatternLayout&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;log4net.Config.BasicConfigurator.Configure( &lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Appender.ConsoleAppender(&lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Layout.PatternLayout(&lt;span class="str"&gt;&amp;quot;%d [%t] %-5p %c [%x] &amp;amp;lt;%X{abc}&amp;amp;gt; - %m%n&amp;quot;&lt;/span&gt;)));&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="rem"&gt;// using a ConsoleAppender with a SimpleLayout&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;log4net.Config.BasicConfigurator.Configure( &lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Appender.ConsoleAppender(&lt;span class="kwrd"&gt;new&lt;/span&gt; log4net.Layout.SimpleLayout()));&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;You can see that while it is easy to code here, you can't configure settings for individual loggers. All of the settings that are defined here are applied to the root logger. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Log4net Best Practices&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1- Use a unique logger object for each type in your application and give it the class name. Like so:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;code&gt;static&lt;/code&gt; &lt;code&gt;log4net.ILog Log = log4net.LogManager.GetLogger(&lt;/code&gt;&lt;code&gt;System.Reflection.MethodBase.GetCurrentMethod().DeclaringType&lt;/code&gt;&lt;code&gt;);&lt;/code&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;2- Whenever you catch an exception, log it.&amp;#160; Even if you just plan to throw it again, log it.&amp;#160; In addition, log4net knows how to format Exception objects, so don't try and build your own string from the exception data, just pass the exception to the logging statement. Like So:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;code&gt;Log.Error(&lt;/code&gt;&lt;code&gt;&amp;quot;An exception raised due to XYZ&amp;quot;&lt;/code&gt;&lt;code&gt;, &lt;/code&gt;&lt;code&gt;e&lt;/code&gt;&lt;code&gt;);&lt;/code&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;3- Log to mark the activity and steps of your code.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;4- Check for null references before logging any property. Logging code is code, and it should be treated the same way.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;5- If an argument to the log method is expensive to obtain, be sure to guard your log statement with a check of the appropriate Is*Enabled property &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;code&gt;if&lt;/code&gt;&lt;code&gt;( Log.IsDebugEnabled )&lt;/code&gt;&lt;code&gt;{ /* log statement */ }&lt;/code&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;6- Before an object calls on a shared dependency, consider pushing a tag onto the log context stack.&amp;#160; This will allow you to determine the caller of the shared code that logged a particular message. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;7- Whenever you use a formatted log statement, surround the format argument placeholders (the {}'s) with brackets or parentheses. This will mark the areas in each log message. This make the log scan a bit easier and makes empty or null formatting arguments more obvious.&amp;#160; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Suggested logging levels&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;INFO Level&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;The start and end of the method &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;The start and end of any major loops &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;The start of any major case/switch statements &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;DEBUG Level&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Any parameters passed into the method &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Any row counts from result sets I retrieve &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Any datarows that may contain suspicious data when being passed down to the method &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Any &amp;quot;generated&amp;quot; file paths, connection strings, or other values that could get mungled up when being &amp;quot;pieced together&amp;quot; by the environment. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;ERROR Level&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Handled exceptions &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Invalid login attempts (if security is an issue) &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Bad data that I have intercepted forreporting &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;FATAL Level&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Unhandled exceptions. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In this post I tried to cover as much as possible about logging using log4net. Although logging is extremely useful, keep in mind that it is not free, it consumes resources and the more you log the less you can find. So be careful, and Good Luck.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-8591507174850617619?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/LPn-IGJ4F8o" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/LPn-IGJ4F8o/log4net-extended-tutorial.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-kPnzbo6YZtM/TpuXZ6KYXvI/AAAAAAAAAII/e0racliecz8/s72-c/log4net1_thumb2.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2011/10/log4net-extended-tutorial.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-6286593038208095159</guid><pubDate>Tue, 28 Dec 2010 00:54:00 +0000</pubDate><atom:updated>2010-12-28T03:01:30.336+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CSDP</category><category domain="http://www.blogger.com/atom/ns#">SWEBOK</category><category domain="http://www.blogger.com/atom/ns#">Software Engineering Education</category><title>How I prepared for CSDP ?</title><description>&lt;p&gt;After I became an &lt;strong&gt;&lt;a href="http://www.computer.org/portal/web/certification/csdp" target="_blank"&gt;IEEE Certified Software Development Professional&lt;/a&gt;&lt;/strong&gt;, many of my friends asked me about it and how to prepare for it. As a CSDP holder, let me introduce CSDP for you and how I prepared for it.&lt;/p&gt;  &lt;p&gt;The CSDP credential is intended for mid-career software development professionals that want to confirm their proficiency of standard software development practices and advance in their careers.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Who is eligible to take the CSDP?&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Licensed SW Engineers and IEEE Senior Members are already qualified for the CSDP. All others must meet the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Education: (need at least one)&lt;/b&gt;&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Bachelor's degree&lt;/li&gt;      &lt;li&gt;CSDA certificate holder&lt;/li&gt;      &lt;li&gt;Educator at the post-baccalaureate level&lt;/li&gt;      &lt;li&gt;Full member of the IEEE&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;&lt;b&gt;Experience: (need at least one)&lt;/b&gt;&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Advanced degree in software engineering and at least two years (about 3,500 hrs) of experience in software engineering/development&lt;/li&gt;      &lt;li&gt;At least four years (about 7,000 hrs) experience in software engineering/development&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;Preparing for CSDP&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;Unfortunately, IEEE Computer Society didn’t provide or recommend a specific training material for CSDP. All what they provided is:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;CSDP Exam specification: lists the knowledge area, its weight in the exam, and the competency level you should possess. &lt;/li&gt;    &lt;li&gt;Recommend list of references.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Of course preparation depends on your knowledge level and experience you have. Below the materials that I used:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Guide to the Software Engineering Body of Knowledge | CS Press&lt;/li&gt;    &lt;li&gt;Software Engineering, 8th Edition | Ian Sommerville | Addison Wesley&lt;/li&gt;    &lt;li&gt;Code Complete, 2nd Edition | Steve McConnell | Microsoft Press&lt;/li&gt;    &lt;li&gt;Software Engineering | IIT Bombay NPTEL Online Course , Prof. Rushikesh K Joshi&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;40 Lecture, almost 1 hour each&lt;/li&gt;      &lt;li&gt;Almost covers all topics in the Ian Sommerville’s book: Software Engineering&lt;/li&gt;      &lt;li&gt;&lt;a href="http://freevideolectures.com/Course/2318/Software-Engineering"&gt;http://freevideolectures.com/Course/2318/Software-Engineering&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;Software Engineering | UC Berkeley Online Course, Fall 2010 , Prof. Armando Fox&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;17 Lectures&lt;/li&gt;      &lt;li&gt;&lt;a href="http://freevideolectures.com/Course/2729/Software-Engineering"&gt;http://freevideolectures.com/Course/2729/Software-Engineering&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;Principles of Software Engineering | University of Washington Online Course, Autumn 2007 , Prof. David Notkin&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;9 Lectures&lt;/li&gt;      &lt;li&gt;&lt;a href="http://freevideolectures.com/Course/2831/CSEP-503-Principles-of-Software-Engineering"&gt;http://freevideolectures.com/Course/2831/CSEP-503-Principles-of-Software-Engineering&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;For &lt;b&gt;software process&lt;/b&gt; only, the podcast series “IEEE Talks Software Process” &lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;9 Podcasts, Avg 20 min each&lt;/li&gt;      &lt;li&gt;&lt;a href="http://www.computer.org/portal/web/ieee-talks-software-process/home"&gt;http://www.computer.org/portal/web/ieee-talks-software-process/home&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;For &lt;b&gt;software architecture&lt;/b&gt;, the podcast series “IEEE Software on architecture with Grady Booch”&lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;26 Podcasts, Avg 15 min each&lt;/li&gt;      &lt;li&gt;&lt;a href="http://www.computer.org/portal/web/computingnow/onarchitecture"&gt;http://www.computer.org/portal/web/computingnow/onarchitecture&lt;/a&gt;&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;Although these resources looks comprehensive, questions in the exam may not be completely covered by these resource. So, try to enrich your knowledge and experience in software engineering as much as you can and Good Luck.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-6286593038208095159?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/-d2bzy8FuXk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/-d2bzy8FuXk/how-i-prepared-for-csdp.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/12/how-i-prepared-for-csdp.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-2941205472967461996</guid><pubDate>Fri, 17 Dec 2010 14:02:00 +0000</pubDate><atom:updated>2010-12-17T16:02:28.917+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Software Engineering Education</category><category domain="http://www.blogger.com/atom/ns#">Requirements Engineering</category><title>Business analysis &amp; Requirements Engineering Certifications</title><description>&lt;p&gt;I was searching for certifications on requirements engineering and I found that there is a lot of mixing between business analysis and requirements engineering. This mix exists both on the daily use, professional certifications and organizations.&lt;/p&gt;  &lt;p&gt;Starting by the definitions:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Business analysis&lt;/b&gt; is the discipline of identifying business needs and determining solutions to business problems. Solutions often include a systems development component, but may also consist of process improvement, organizational change or strategic planning and policy development. (Kathleen B Hass, Richard Vander Horst, Kimi Ziemski (2008). &lt;i&gt;From Analyst to Leader: Elevating the Role of the Business Analyst&lt;/i&gt; Management Concepts, 2008. ISBN 1567262139. p94: &amp;quot;As the discipline of business analysis becomes professionalized&amp;quot;) &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Requirement engineering&lt;/strong&gt; according to Laplante is &amp;quot;a sub-discipline of systems engineering and software engineering that is concerned with determining the goals, functions, and constraints of hardware and software systems.&amp;quot;. (Phillip A. Laplante (2007) &lt;i&gt;What Every Engineer Should Know about Software Engineering&lt;/i&gt;. ISBN 0849372283 p44) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This may lead us to conclude that business analysis as a discipline has a heavy overlap with requirements engineering, but focuses on identifying the changes to an organization that are required for it to achieve strategic goals. These changes include changes to strategies, structures, policies, processes, and information systems.&lt;/p&gt;  &lt;p&gt;Regarding organizations: &lt;strong&gt;&lt;a href="http://www.theiiba.org/" target="_blank"&gt;IIBA&lt;/a&gt;&lt;/strong&gt;&lt;sup&gt;®&lt;/sup&gt; International &lt;strong&gt;Institute of Business Analysis&lt;/strong&gt; is the independent non-profit professional association serving the growing field of Business Analysis, &lt;strong&gt;&lt;a href="http://www.certified-re.de/en" target="_blank"&gt;IREB&lt;/a&gt;&lt;/strong&gt; &lt;strong&gt;International Requirements Engineering Board&lt;/strong&gt; is one serving Requirements Engineering&lt;/p&gt;  &lt;p&gt;Both IIBA and IREB offers professional certifications:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;IIBA : Certified Business Analyst Professional &lt;strong&gt;CBAP&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;IREB : Certified Professional for Requirements Engineering &lt;strong&gt;CPRE&lt;/strong&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;based on many forum posts and discussions I found that:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CBAP is targeted to highly educated BA professionals only (you need a minimum of 5 years of experience in business analysis / RE) &lt;/li&gt;    &lt;li&gt;CPRE is aimed to everyone working in the field of requirements engineering &lt;/li&gt;    &lt;li&gt;CPRE is a lot cheaper and can be achieved at an independent certification body &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;What you think ?&lt;/p&gt;  &lt;p&gt;Could you please share your opinion and ideas with me ?&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-2941205472967461996?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/jqZ00GHPGrc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/jqZ00GHPGrc/business-analysis-requirements.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/12/business-analysis-requirements.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-7656707429044972277</guid><pubDate>Wed, 01 Sep 2010 10:51:00 +0000</pubDate><atom:updated>2010-09-01T12:51:36.550+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CSDP</category><category domain="http://www.blogger.com/atom/ns#">SWEBOK</category><category domain="http://www.blogger.com/atom/ns#">Software Engineering Education</category><title>How to educate the next generation of software engineers ?</title><description>&lt;p&gt;After my completion of my master in software engineering at &lt;a href="http://www.nileu.edu.eg/" target="_blank"&gt;Nile University&lt;/a&gt; which we faced many problems at it (I hope it be better in the future), I tried to search for what globally constitutes a master of software engineering and I found the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The last effort to create a reference curriculum for graduate software engineering education was by the SEI in the early 1990s. &lt;/li&gt;    &lt;li&gt;There were, in effect, no current community-endorsed recommendations on what to teach software engineers. &lt;/li&gt;    &lt;li&gt;Response: create a project to create a new reference curriculum in software engineering (iSSec Project) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;The Integrated Software and      &lt;br /&gt;Systems Engineering Curriculum iSSec Project&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;iSSec&lt;/strong&gt; Project begun in May 2007 at Stevens Institute of Technology and sponsored by DoD Director of Systems and Software Engineering Kristen Baldwin. Three products planned for iSSec:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A modern reference curriculum for a master’s degree in software engineering that integrates an appropriate amount of systems engineering &lt;/li&gt;    &lt;li&gt;A modern reference curriculum for a master’s degree in systems engineering that integrates an appropriate amount of software engineering &lt;/li&gt;    &lt;li&gt;A truly interdisciplinary degree that is neither systems nor software engineering – it is both &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;What interests me the most is the first product, and it is the only completed product. A project of seven steps had been set to produce that reference curriculum:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Understand the current state of SwE graduate education (November 2007) &lt;/li&gt;    &lt;li&gt;Create GSwE2009 0.25 (formerly GSwERC) with a small team, suitable for limited review (February 2008) &lt;/li&gt;    &lt;li&gt;Publicize effort through conferences, papers, website, etc (continuous) &lt;/li&gt;    &lt;li&gt;Create GSwE2009 0.50 (formerly GSwERC) suitable for broad community review and early adoption (October 2008) &lt;/li&gt;    &lt;li&gt;Create GSwE2009 1.0 suitable for broad adoption (September 2009) &lt;/li&gt;    &lt;li&gt;Transition stewardship to professional societies (Late 2009-Early 2010) &lt;/li&gt;    &lt;li&gt;Foster adoption world-wide (2009 and beyond) &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Steps 6 and 7 are undertaking now.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&lt;a href="http://www.gswe2009.org/fileadmin/files/GSwE2009_Curriculum_Docs/GSwE2009_version_1.0.pdf" target="_blank"&gt;Graduate&amp;#160; Software&amp;#160; Engineering&amp;#160; 2009&amp;#160; (GSwE2009):&amp;#160; Curriculum&amp;#160; Guidelines&amp;#160; for&amp;#160; Graduate Degree Programs in Software Engineering&lt;/a&gt;&lt;/em&gt; is the first product of the iSSEc project. &lt;/p&gt;  &lt;p&gt;This document is a reference for anyone currently in a master program in software engineering or will enroll in such a program. GSwE2009 is a REFERENCE curriculum, not an absolute statement of requirements. It is a set of recommendations to universities on the best guidance that the authors could generate, incorporating extensive review comments from the broad community. The authors believe that GSwE2009 sets the “gold standard” for graduate software engineering education. Here is the summary of the most important parts of GSwE2009, outcomes and requirements.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;strong&gt;Summary of Outcomes&lt;/strong&gt;     &lt;br /&gt;Graduates of a master‘s program that satisfies GSwE2009 recommendations will: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;u&gt;CBOK&lt;/u&gt; : Master the CBOK (Core Body Of Knowledge). &lt;/li&gt;    &lt;li&gt;&lt;u&gt;DOMAIN&lt;/u&gt; : Master software engineering in at least one application domain, such as&amp;#160; finance, medical, transportation, or telecommunications, and one application type, such as real-time, embedded, safety-critical, or highly distributed systems. That mastery includes understanding how differences in domain and type manifest themselves in both the software itself and in its engineering, and includes understanding how to learn a new application domain or type. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;DEPTH&lt;/u&gt; : Master at least one KA (Knowledge Area) or sub-area from the CBOK to at least the Bloom Synthesis level. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;ETHICS&lt;/u&gt; : Be able to make ethical professional decisions and practice ethical professional behavior. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;SYSTEMS ENGINEERING&lt;/u&gt; : Understand the relationship&amp;#160; between SwE and SE and&amp;#160; be able to apply SE principles and practices in the engineering of software. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;TEAM&lt;/u&gt; : Be an effective member of a team, including teams that are international and geographically distributed, effectively communicate both orally and in writing, and lead in one area of project development, such as project management, requirements analysis, architecture, construction, or quality assurance. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;RECONCILIATION&lt;/u&gt; : Be able to reconcile conflicting project objectives, finding acceptable compromises within       &lt;br /&gt;limitations of cost, time, knowledge, existing systems, and organizations. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;PERSPECTIVE&lt;/u&gt; : Understand and appreciate feasibility analysis, negotiation, and good communications with stakeholders in a typical software development environment, and be able to perform those tasks well; have effective work habits and be a leader. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;LEARNING&lt;/u&gt; : Be able to learn new models, techniques, and technologies as they emerge, and appreciate       &lt;br /&gt;the necessity of such continuing professional development. &lt;/li&gt;    &lt;li&gt;&lt;u&gt;TECHNOLOGY&lt;/u&gt; : Be&amp;#160; able&amp;#160; to&amp;#160; analyze&amp;#160; a&amp;#160; current&amp;#160; significant&amp;#160; software&amp;#160; technology,&amp;#160; articulate&amp;#160; its&amp;#160; strengths&amp;#160; and weaknesses, compare it to alternative technologies, and specify and promote improvements or extensions to that technology.       &lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Summary of Expected Background      &lt;br /&gt;&lt;/strong&gt;GSwE2009 presumes that an entering student has:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;u&gt;DEGREE&lt;/u&gt; : The equivalent of an undergraduate degree in computing or an undergraduate degree in an engineering or scientific field and a minor in computing, &lt;/li&gt;    &lt;li&gt;&lt;u&gt;SWE COURSE&lt;/u&gt; : The equivalent of an introductory course in software engineering, and &lt;/li&gt;    &lt;li&gt;&lt;u&gt;EXPERIENCE&lt;/u&gt; :At least two years of practical experience in some aspect&amp;#160; of software engineering or software development. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;As Nile University is the only institute in Egypt that give a master degree in software engineering, I hope they follow this reference curriculum. &lt;/p&gt;  &lt;p&gt;Individuals can also use it to guide their self-study plans in software engineering.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-7656707429044972277?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/pPTxzIOKYbw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/pPTxzIOKYbw/how-to-educate-next-generation-of.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/09/how-to-educate-next-generation-of.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-4836678540686313551</guid><pubDate>Sun, 11 Apr 2010 16:44:00 +0000</pubDate><atom:updated>2010-04-11T18:44:40.437+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL</category><title>SQL Server Metadata</title><description>&lt;p&gt;Many times when you need to troubleshoot an SQL Server issue, you will need to collect metadata about the server, databases, and server resources in general. Here we will briefly review the mechanisms to collect these metadata.&lt;/p&gt;  &lt;h3&gt;System Base Tables&lt;/h3&gt;  &lt;p&gt;SQL server maintains a set of tables that store information about all the objects, data types, constraints, configuration options, and resources available to SQL Server. These tables are called the &lt;em&gt;system base tables.&lt;/em&gt; &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Some of these tables exist in &lt;em&gt;master&lt;/em&gt; database –&amp;gt; contain system-wide information. &lt;/li&gt;    &lt;li&gt;Some exist in every database –&amp;gt; contain database specific information. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;You can access these tables names only if you are logged in as a system administrator. You can access through:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Run sp_help &lt;/li&gt;    &lt;li&gt;Run      &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=use&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;use&lt;/a&gt; master; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;select&lt;/a&gt; name &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=from&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;from&lt;/a&gt; sys.objects &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=where&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;where&lt;/a&gt; type_desc = '&lt;span style="color: #8b0000"&gt;SYSTEM_TABLE&lt;/span&gt;';&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If you tried to select data from any of these system tables, you will got 208 error indicating that the object name is invalid. The only way to access these data is through &lt;a title="dedicated administrator connection (DAC)" href="http://ebeid-soliman.blogspot.com/2010/04/sql-server-dedicated-administrator.html"&gt;dedicated administrator connection (DAC)&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Keep in mind that these system base tables are used for internal purposes only within the database engine and are not intended for general use. &lt;font color="#ff0000"&gt;They are subject to change and compatibility is not guaranteed.&lt;/font&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Compatibility Views&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Although it is possible to see data in the system tables in versions of SQL Server before 2005, it wasn’t recommended. For compatibility, SQL server 2005 and 2008 provided a set of &lt;a title="Compatibility Views on MSDN" href="http://msdn.microsoft.com/en-us/library/ms187376.aspx" target="_blank"&gt;compatibility views&lt;/a&gt; that allow access to a subset of the SQL server 2000 system tables. These views should be used for backward compatibility only; going forward, you should use catalog views.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Catalog Views&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;SQL Server 2005 introduced a set of catalog views as a general interface to the persisted system metadata. All the catalog views are in the sys schema, and you must reference the schema name when access the objects like:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;select&lt;/a&gt; name &lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=from&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;from&lt;/a&gt; sys.databases;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For a complete list of catalog views categories, please consult &lt;a title="http://msdn.microsoft.com/en-us/library/ms174365.aspx" href="http://msdn.microsoft.com/en-us/library/ms174365.aspx."&gt;http://msdn.microsoft.com/en-us/library/ms174365.aspx.&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Information Schema Views&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The information schema views comply with SQL-92 standard and all of it are in a schema called INFORMATION_SCHEMA. If you need to write a portable application that access the metadata you should use these views. This compliance come with the price of limited provided information (it provide the standard defined information only). If you need metadata about non-standard features, use catalog views.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For a complete list of Information schema views and its closest map to catalog views, please consult &lt;a title="http://msdn.microsoft.com/en-us/library/ms186778.aspx" href="http://msdn.microsoft.com/en-us/library/ms186778.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms186778.aspx&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;System Functions&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Give us individual property values for many SQL Server instance, objects, databases. The values returned by system functions are scalar, so they can be used as values returned by &lt;em&gt;SELECT&lt;/em&gt; statements like:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;amp;siteid=us%2Fdev&amp;amp;p=1&amp;amp;nq=NEW&amp;amp;qu=select&amp;amp;IntlSearch=&amp;amp;boolean=PHRASE&amp;amp;ig=01&amp;amp;i=09&amp;amp;i=99"&gt;select&lt;/a&gt; DATABASEPROPERTYEX('&lt;span style="color: #8b0000"&gt;msdb&lt;/span&gt;','&lt;span style="color: #8b0000"&gt;Recovery&lt;/span&gt;');&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For a complete list of system functions and its types, review &lt;a title="http://msdn.microsoft.com/en-us/library/ms187786.aspx" href="http://msdn.microsoft.com/en-us/library/ms187786.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms187786.aspx&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;System Stored Procedures&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;System Stored Procedures are the original metadata access tool but it had a drawback, basically you have to accept the data that it returns. Some of the procedures allow parameters but they are very limited. Catalog views are more enhanced and flexible in controlling what data appears.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I hope this brief post gives you an overall image about SQL Server metadata access mechanisms and which one to use according to your situation.&amp;#160; &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-4836678540686313551?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/Ko05ydvnrA0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/Ko05ydvnrA0/sql-server-metadata.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/04/sql-server-metadata.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-7764502142869492782</guid><pubDate>Sun, 11 Apr 2010 10:08:00 +0000</pubDate><atom:updated>2010-04-11T12:08:23.729+02:00</atom:updated><title>SQL Server Dedicated Administrator Connection</title><description>&lt;p&gt;Microsoft SQL Server provides a dedicated administrator connection (DAC) which allows an administrator to access a running instance of SQL Server Database Engine to troubleshoot problems on the server—even when the server is unresponsive to other client connections. The DAC is available through the &lt;strong&gt;sqlcmd&lt;/strong&gt; utility and SQL Server Management Studio. The connection is only allowed from a client running on the server. No network connections are permitted. &lt;/p&gt;  &lt;p&gt;To use SQL Server Management Studio with the DAC, connect to an instance of the SQL Server Database Engine with Query Editor by typing &lt;strong&gt;ADMIN:&lt;/strong&gt; before the server name. Object Explorer cannot connect using the DAC.&lt;/p&gt;  &lt;p&gt;To connect to a server using the DAC&lt;/p&gt;  &lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;     &lt;p&gt;In SQL Server Management Studio, with no other DACs open, on the toolbar, click &lt;strong&gt;Database Engine Query&lt;/strong&gt;. &lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;In the &lt;strong&gt;Connect to Database Engine&lt;/strong&gt; dialog box, in the &lt;strong&gt;Server name&lt;/strong&gt; box, type &lt;strong&gt;ADMIN:&lt;/strong&gt; followed by the name of the server instance. For example, to connect to a server instance named &lt;strong&gt;ACCT\PAYABLE&lt;/strong&gt;, type &lt;strong&gt;ADMIN:&lt;/strong&gt;&lt;strong&gt;ACCT\PAYABLE&lt;/strong&gt;.&lt;/p&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;p&gt;Complete the &lt;strong&gt;Authentication&lt;/strong&gt; section, providing credentials for a member of the &lt;strong&gt;sysadmin&lt;/strong&gt; group, and then click &lt;strong&gt;Connect&lt;/strong&gt;.&lt;/p&gt;      &lt;p&gt;The connection is made.&lt;/p&gt;      &lt;p&gt;If the DAC is already in use, the connection will fail with an error indicating it cannot connect.&lt;/p&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;If you are trying to that and got the following error:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#ff0000"&gt;Dedicated administrator connections are not supported. (ObjectExplorer)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;It means that you are trying to connect Object Explorer using the DAC. Object Explorer cannot connect using the DAC; only Query Window can. That means that you cannot press the New Query button; you have to use the Database Engine Query button.&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;a href="http://photos1.blogger.com/blogger2/7616/913/1600/pic2440.2.png"&gt;&lt;img border="0" alt="" src="http://photos1.blogger.com/blogger2/7616/913/400/pic2440.2.png" width="468" height="302" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-7764502142869492782?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/8QO_qu7qvwU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/8QO_qu7qvwU/sql-server-dedicated-administrator.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/04/sql-server-dedicated-administrator.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-5312930658805659364</guid><pubDate>Sun, 11 Apr 2010 01:41:00 +0000</pubDate><atom:updated>2010-04-11T03:44:51.761+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">CSDP</category><category domain="http://www.blogger.com/atom/ns#">SWEBOK</category><title>What is the SWEBOK Guide?</title><description>&lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;a href="http://lh4.ggpht.com/_R1exOFT_lVs/S8Eo4C_qXGI/AAAAAAAAAHg/SiuF0_pOc68/s1600-h/SWEBOK%5B3%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="SWEBOK" border="0" alt="SWEBOK" align="left" src="http://lh6.ggpht.com/_R1exOFT_lVs/S8Eo5LTsBBI/AAAAAAAAAHk/mNo-1A_yNc8/SWEBOK_thumb%5B3%5D.png?imgmax=800" width="244" height="189" /&gt;&lt;/a&gt;&amp;#160; SWEBOK or &lt;strong&gt;S&lt;/strong&gt;oftware &lt;strong&gt;E&lt;/strong&gt;ngineering &lt;strong&gt;B&lt;/strong&gt;ody &lt;strong&gt;O&lt;/strong&gt;f &lt;strong&gt;K&lt;/strong&gt;nowledge is an all-inclusive term that describes the sum of knowledge within the profession of software engineering. Since it is usually not possible to put the full body of knowledge of even an emerging discipline, such as software engineering, into a single document, there is a need for a Guide to the Software Engineering Body of Knowledge. This Guide will seek to identify and describe that subset of the body of knowledge that is generally accepted.&lt;/p&gt;  &lt;p&gt;SWEBOK Guide is developed by the IEEE Computer Society with leaders from industry and academia. It is ratified by ISO-IEC 24773 standard as the BOK for software engineering certifications internationally. The CSDA and CSDP are the first 2 certifications to conform to the ISO-IEC standard.&lt;/p&gt;  &lt;p&gt;Here are the SWEBOK knowledge areas, its sub areas, and it’s references and suggested books. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; &lt;strong&gt;Business Practices and Engineering Economics (&lt;/strong&gt;3-4% questions in CSDP&lt;strong&gt;) &lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Engineering Economics &lt;/li&gt;      &lt;li&gt;Ethics &lt;/li&gt;      &lt;li&gt;Professional Practice &lt;/li&gt;      &lt;li&gt;Standards&lt;a name="II"&gt;&lt;/a&gt;&lt;i&gt;&lt;/i&gt; &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;No book-length references are recommended for this topic&lt;/u&gt;.&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;The following books are suggested as supplemental reading in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Morality and Machines: Perspectives on Computer Ethics&lt;/b&gt;, Edgar, S. L., Sudbury, Massachusetts: Jones and Bartlett, 1997. ISBN 0763717673 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Computer Ethics&lt;/b&gt;, 2d Ed., Johnson, Deborah G, Upper Saddle River, NJ: Prentice-Hall, 1994. ISBN 0130836990 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Smith and Roberson's Business Law&lt;/b&gt;, 11th ed., Mann, Richard A., &amp;amp; Barry S. Roberts, Cincinnati, OH: West Thomson Learning, 2000. ISBN 0324121849 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Engineering Economy&lt;/b&gt;, Thusen, G.J., Prentice-Hall, 2000. ISBN 013028128X &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;2.&lt;/strong&gt; &lt;strong&gt;Software Requirements (&lt;/strong&gt;13-15% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Requirements Engineering Process &lt;/li&gt;      &lt;li&gt;Requirements Elicitation &lt;/li&gt;      &lt;li&gt;Requirements Analysis &lt;/li&gt;      &lt;li&gt;Software Requirements Specification &lt;/li&gt;      &lt;li&gt;Requirements Validation &lt;/li&gt;      &lt;li&gt;Requirements Management &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="III"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;Any one of the following books is recommended in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Software Requirements: Objects, Functions, &amp;amp; States&lt;/b&gt;, Davis, Alan. Upper Saddle River, New Jersey: Prentice Hall, 1993. ISBN 013805763X &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Practical Software Requirements: A Manual of Content and Style&lt;/b&gt;, Kovitz, Benjamin L., Manning Publications Company, 1998. ISBN 1884777597 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Mastering the Requirements Process&lt;/b&gt;, Robertson, James and Suzanne Robertson, New York: Dorset House, 2000. ISBN 0201360462 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Requirements Engineering: A Good Practice Guide&lt;/b&gt;, Sommerville, Ian, New York: John Wiley &amp;amp; Sons, 1997. ISBN 0471974447 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Software Requirements&lt;/b&gt;, Wiegers, Karl. Microsoft Press, 1999. ISBN 0735606315 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;3.&lt;/strong&gt; &lt;strong&gt;Software Design (&lt;/strong&gt;22-24% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Software Design Concepts &lt;/li&gt;      &lt;li&gt;Software Architecture &lt;/li&gt;      &lt;li&gt;Software Design Quality Analysis and Evaluation &lt;/li&gt;      &lt;li&gt;Software Design Notations and Documentation &lt;/li&gt;      &lt;li&gt;Software Design Strategies and Methods &lt;/li&gt;      &lt;li&gt;Human Factors in Software Design &lt;/li&gt;      &lt;li&gt;Software and System Safety &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="IV"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;Any one of the following books is recommended in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Software Architecture in Practice&lt;/b&gt;, Bass, Len, Paul Clements and Rick Kazman. Reading, Massachusetts: Addison-Wesley, 1998. ISBN 0201199300 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Pattern Oriented Software Architecture: A System of Patterns&lt;/b&gt;, Buschmann, Frank, et al, John Wiley &amp;amp; Sons, 1996. ISBN 0471958697 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Fundamentals of Object-Oriented Design in UML&lt;/b&gt;, Page-Jones, Meilir, Addison-Wesley, 1999. ISBN 0201699946X &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;The following books are suggested as supplemental reading in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Fundamentals of Database Systems&lt;/b&gt;, Elmasri, Ramez and Shamkant Navathe, 3d Ed., Reading, Mass.: Addison Wesley, 2000. ISBN 0805317554 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Software Reuse: Architecture, Process and Optimization for Business Success&lt;/b&gt;, Jacobsen, Ivar, Martin Griss, and Patrik Jonson, Reading, Massachusetts: Addison-Wesley, 1997. ISBN 0201924765 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;strong&gt;4. Software Construction (&lt;/strong&gt;10-12% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Construction planning &lt;/li&gt;      &lt;li&gt;Code design &lt;/li&gt;      &lt;li&gt;Data design and management &lt;/li&gt;      &lt;li&gt;Error processing &lt;/li&gt;      &lt;li&gt;Source code organization &lt;/li&gt;      &lt;li&gt;Code documentation &lt;/li&gt;      &lt;li&gt;Construction QA &lt;/li&gt;      &lt;li&gt;System integration and deployment &lt;/li&gt;      &lt;li&gt;Code tuning &lt;/li&gt;      &lt;li&gt;Construction tools &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="V"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;The following book is recommended on this topic&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Code Complete&lt;/b&gt;, McConnell, Steve, Microsoft Press, 1993. ISBN 1-5561-5484-4 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;The following books are suggested as supplemental reading in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;The Practice of Programming&lt;/b&gt;, Kernighan, Brian W. and Rob Pike, Reading, Mass.: Addison Wesley, 1999. ISBN 020161586X &lt;/li&gt;      &lt;li&gt;&lt;b&gt;The Pragmatic Programmer: From Journeyman to Master&lt;/b&gt;, Hunt, Andrew, David Thomas, and Ward Cunningham, Reading, Mass.: Addison Wesley, 1999. ISBN020161622X &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;5. Software Testing (&lt;/strong&gt;15-17% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Types of Tests &lt;/li&gt;      &lt;li&gt;Test Levels &lt;/li&gt;      &lt;li&gt;Testing Strategies &lt;/li&gt;      &lt;li&gt;Test Design &lt;/li&gt;      &lt;li&gt;Test Coverage of Code &lt;/li&gt;      &lt;li&gt;Test Coverage of Specifications &lt;/li&gt;      &lt;li&gt;Test Execution &lt;/li&gt;      &lt;li&gt;Test Documentation &lt;/li&gt;      &lt;li&gt;Test Management &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="VI"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;Any one of the following books is recommended in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Testing Object-Oriented Systems&lt;/b&gt;, Binder, Robert V. Reading, Massachusetts: Addison-Wesley, 2000. ISBN 0201809389 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Complete Guide to Software Testing&lt;/b&gt;, 2nd Ed., Hetzel, Bill, New York, New York: John Wiley &amp;amp; Son, 1993. ISBN 0471565679 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Software Testing : A Craftsman's Approach&lt;/b&gt;, Jorgensen, Paul C. CRC Press, 1995. ISBN 0849308097 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Testing Computer Software&lt;/b&gt;, 2nd Ed., Kaner, Clem, Jack Falk, and Hung Quoc Nguyen, New York, New York: John Wiley and Sons, 1999. ISBN 0471358460 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Software Testing and Continuous Quality Improvement&lt;/b&gt;, Lewis, William. CRC Press 2000. ISBN 0849398339 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;The Craft of Software Testing: Subsystems Testing Including Object-Based and Object-Oriented Testing&lt;/b&gt;, Marick, Brian. Prentice Hall, 1997. ISBN 0131774115 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;6. Software Maintenance (&lt;/strong&gt;3-5% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Software Maintainability &lt;/li&gt;      &lt;li&gt;Software Maintenance Process &lt;/li&gt;      &lt;li&gt;Software Maintenance Measurement &lt;/li&gt;      &lt;li&gt;Software Maintenance Planning &lt;/li&gt;      &lt;li&gt;Software Maintenance Management &lt;/li&gt;      &lt;li&gt;Software Maintenance Documentation &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="VII"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;The following book is recommended on this topic&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Practical Software Maintenance&lt;/b&gt;, Pigoski, Thomas M. New York, Wiley Computer Publishing, 1997. ISBN 0471170011 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;7. Software Configuration Management (&lt;/strong&gt;3-4% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Management of SCM Process &lt;/li&gt;      &lt;li&gt;Software Configuration Identification &lt;/li&gt;      &lt;li&gt;Software Configuration Control &lt;/li&gt;      &lt;li&gt;Software Configuration Status Accounting &lt;/li&gt;      &lt;li&gt;Software Configuration Auditing &lt;/li&gt;      &lt;li&gt;Software Release Management and Delivery &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;a name="VIII"&gt;&lt;/a&gt;&lt;b&gt;IEEE Standards 828-1990 and 1042-1987&lt;/b&gt; &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;8. Software Engineering Management (&lt;/strong&gt;10-12% questions in CSDP&lt;strong&gt;)&lt;/strong&gt; &lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Measurement &lt;/li&gt;      &lt;li&gt;Organizational Management and Coordination &lt;/li&gt;      &lt;li&gt;Initiation and Scope Definition &lt;/li&gt;      &lt;li&gt;Planning &lt;/li&gt;      &lt;li&gt;Software Acquisition &lt;/li&gt;      &lt;li&gt;Enactment &lt;/li&gt;      &lt;li&gt;Risk Management &lt;/li&gt;      &lt;li&gt;Review and Evaluation &lt;/li&gt;      &lt;li&gt;Project Close Out &lt;/li&gt;      &lt;li&gt;Post-closure Activities &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="IX"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;Any one of the following books is recommended in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Principles of Software Engineering Management&lt;/b&gt;, Gilb, Tom, Reading, Mass.: Addison Wesley, 1988. ISBN 0-2011-9246-2 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Rapid Development&lt;/b&gt;, McConnell, Steve, Microsoft Press, 1996. ISBN 1556159005 &lt;/li&gt;      &lt;li&gt;&lt;a href="http://www.computer.org/cspress/CATALOG/bp08000.htm"&gt;&lt;b&gt;Software Engineering Project Management&lt;/b&gt;&lt;/a&gt;, 2d ed, Thayer, Richard H., IEEE Computer Society Press, Los Alamitos, CA 1997. ISBN 0-8186-8000-8 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Quality Software Management, Vol. 1, Systems Thinking&lt;/b&gt;, Weinberg, Gerald M.. New York: Dorset House, 1992. ISBN 0932633226 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;The following book is suggested as supplemental reading in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;Developing Managerial Skills in Engineers and Scientists&lt;/b&gt;, Badawy, Michael K. Van Hostrand, NY, 1995. ISBN 0471286346 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;9. Software Engineering Process (&lt;/strong&gt;2-4% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Process Infrastructure &lt;/li&gt;      &lt;li&gt;Process Measurement &lt;/li&gt;      &lt;li&gt;Process Definition &lt;/li&gt;      &lt;li&gt;Qualitative Process Analysis &lt;/li&gt;      &lt;li&gt;Process Implementation and Change &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a name="X"&gt;&lt;/a&gt;&lt;i&gt;&lt;u&gt;Either of the following books is recommended in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;b&gt;The Capability Maturity Model: Guidelines for Improving the Software Process&lt;/b&gt;, Paulk, Mark, et al (Carnegie Mellon University / Software Engineering Institute). Reading, Mass.: Addison Wesley, 1995. ISBN 0201546647 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Managing the Software Process&lt;/b&gt;, Humphrey, Watts S. Reading, Massachusetts, Addison-Wesley, 1989. ISBN 0201180952 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;10. Software Engineering Tools and Methods (&lt;/strong&gt;2-4% questions in CSDP&lt;strong&gt;)&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Management Tools and Methods &lt;/li&gt;      &lt;li&gt;Development Tools and Methods &lt;/li&gt;      &lt;li&gt;Maintenance Tools and Methods &lt;/li&gt;      &lt;li&gt;Support Tools and Methods &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;&lt;a name="XI"&gt;&lt;/a&gt;&lt;strong&gt;11. Software Quality (&lt;/strong&gt;6-8% questions in CSDP&lt;strong&gt;) &lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;Software Quality Concepts &lt;/li&gt;      &lt;li&gt;Planning for SQA and V&amp;amp;V &lt;/li&gt;      &lt;li&gt;Methods for SQA and V&amp;amp;V &lt;/li&gt;      &lt;li&gt;Measurement Applied to SQA and V&amp;amp;V &lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;No book-length references are recommended for this topic:&lt;/u&gt;&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;i&gt;&lt;u&gt;The following books are suggested as supplemental reading in this area&lt;/u&gt;:&lt;/i&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;ul&gt;     &lt;li&gt;&lt;a href="http://www.computer.org/cspress/CATALOG/bp07609.htm"&gt;&lt;b&gt;Software Engineering&lt;/b&gt;&lt;/a&gt;, Dorfman, M. &amp;amp; Thayer, R., eds. Los Alamitos, California: IEEE Computer Society Press, 1997. ISBN 0-8186-7609-4 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Software Inspection&lt;/b&gt;, Gilb, Tom., and Dorothy Graham, Reading, MA, Addison-Wesley, 1994. ISBN 0-2016-3181-4 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Practical Guide to Software Quality Management&lt;/b&gt;, Horch, John, Artech House, 1996. ISBN 0890068658 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;A Discipline for Software Engineering&lt;/b&gt;, Humphrey, Watts S. Reading, Massachusetts: Addison-Wesley, 1995. ISBN 0201546108 &lt;/li&gt;      &lt;li&gt;&lt;b&gt;Metrics and Models in Software Quality Engineering&lt;/b&gt;, Kan, Stephen H. Addison Wesley, 1995. ISBN 0201729156 &lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-5312930658805659364?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/29zs3MDXh2g" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/29zs3MDXh2g/what-is-swebok-guide.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_R1exOFT_lVs/S8Eo5LTsBBI/AAAAAAAAAHk/mNo-1A_yNc8/s72-c/SWEBOK_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/04/what-is-swebok-guide.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-1220734471215358176</guid><pubDate>Sat, 10 Apr 2010 22:48:00 +0000</pubDate><atom:updated>2010-04-11T00:49:00.070+02:00</atom:updated><title>I’m Certified Software Development Professional:CSDP</title><description>&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="untitled" border="0" alt="untitled" align="left" src="http://lh4.ggpht.com/_R1exOFT_lVs/S8EAWkpM_FI/AAAAAAAAAHc/kmQxTSKV3uw/untitled_thumb%5B1%5D.png?imgmax=800" width="224" height="82" /&gt;It is my pleasure to share with you my recent achievement. Now I’m &lt;b&gt;IEEE Computer Society Certified Software Development Professional&lt;/b&gt; &lt;b&gt;CSDP&lt;/b&gt;. &lt;/p&gt;  &lt;p&gt;The CSDP credential is intended for mid-career software development professionals that want to confirm their proficiency of standard software development practices and advance in their careers.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;Certified Software Development Professional should&lt;/u&gt;:&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Possesses fundamental &lt;font color="#0000ff"&gt;knowledge&lt;/font&gt; and &lt;font color="#0000ff"&gt;understanding&lt;/font&gt; of computing principles and concepts and their application to the &lt;font color="#0000ff"&gt;definition&lt;/font&gt;, &lt;font color="#0000ff"&gt;design&lt;/font&gt;, &lt;font color="#0000ff"&gt;construction&lt;/font&gt;, &lt;font color="#0000ff"&gt;testing&lt;/font&gt; of software. &lt;/li&gt;    &lt;li&gt;Be able to &lt;font color="#0000ff"&gt;apply design principles&lt;/font&gt; with &lt;font color="#0000ff"&gt;technical&lt;/font&gt; and &lt;font color="#0000ff"&gt;economic tradeoffs&lt;/font&gt; to modules, subsystems, and systems in accordance with &lt;font color="#0000ff"&gt;standards&lt;/font&gt; of practice, specifications, and principles of behavior of software as required to perform the functions as stated in the software requirements. &lt;/li&gt;    &lt;li&gt;Has met the IEEE CS CSDP education, experience, and examination requirements. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;How to become an CSDP&lt;/u&gt;:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Application Steps      &lt;ol&gt;       &lt;li&gt;Obtain CSDP candidate brochure from IEEE Computer Society &lt;a href="http://www.computer.org/certification/"&gt;http://www.computer.org/certification/&lt;/a&gt; &lt;/li&gt;        &lt;li&gt;Review requirements &lt;/li&gt;        &lt;li&gt;Complete application &lt;/li&gt;        &lt;li&gt;Send completed application to IEEE CS by deadline with fee &lt;/li&gt;        &lt;li&gt;Acknowledgement of payment &lt;/li&gt;        &lt;li&gt;Review of application &lt;/li&gt;        &lt;li&gt;If accepted, authorization to test sent to candidate &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Requirements for Certification      &lt;ol&gt;       &lt;li&gt;&lt;b&gt;Education: (need at least one)&lt;/b&gt;           &lt;ul&gt;           &lt;li&gt;You have a bachelor's degree &lt;/li&gt;            &lt;li&gt;You are a CSDA certificate holder &lt;/li&gt;            &lt;li&gt;You are an educator at the post-baccalaureate level &lt;/li&gt;            &lt;li&gt;You are a full member of the IEEE &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;        &lt;li&gt;&lt;b&gt;Experience: (need at least one)&lt;/b&gt;           &lt;ul&gt;           &lt;li&gt;You have an advanced degree in software engineering and at least two years (about 3,500 hrs) of experience in software engineering/development &lt;/li&gt;            &lt;li&gt;You have at least four years (about 7,000 hrs) experience in software engineering/development &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Preparation for Examination      &lt;ol&gt;       &lt;li&gt;Depends on level of expertise in areas covered by Body of Knowledge &lt;/li&gt;        &lt;li&gt;Focus on areas needing the most review &lt;/li&gt;        &lt;li&gt;Study from the suggested reference material &lt;/li&gt;        &lt;li&gt;Can take a refresher course (see CSDP website) &lt;/li&gt;        &lt;li&gt;Unless your background covers most of the Body of Knowledge, allow three months of 2-4 hours a week for study &lt;/li&gt;        &lt;li&gt;Last week: assemble test access materials &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Examination      &lt;ol&gt;       &lt;li&gt;Length: 4 hours &lt;/li&gt;        &lt;li&gt;Closed book, computer based &lt;/li&gt;        &lt;li&gt;At a Prometric training center (in Egypt only &lt;a href="http://www.amideast.org/offices/egypt/cairo/default.htm" target="_blank"&gt;AMIDEAST&lt;/a&gt; Cairo branch) &lt;/li&gt;        &lt;li&gt;Format          &lt;br /&gt;• 180 multiple-choice questions that are based on concepts and experiences that should be familiar to a proficient software engineering professional with 7,000 or more hours of experience. The topic areas are grouped by question development into domains. The percentage range of questions in each area is as follows:           &lt;ul&gt;           &lt;li&gt;Business Practices and Engineering Economics (3–4%) &lt;/li&gt;            &lt;li&gt;Software Requirements (13–15%) &lt;/li&gt;            &lt;li&gt;Software Design (22–24%) &lt;/li&gt;            &lt;li&gt;Software Construction (10–12%) &lt;/li&gt;            &lt;li&gt;Software Testing (15–17%) &lt;/li&gt;            &lt;li&gt;Software Maintenance (3–5%) &lt;/li&gt;            &lt;li&gt;Software Configuration Management (3–4%) &lt;/li&gt;            &lt;li&gt;Software Engineering Management (10–12%) &lt;/li&gt;            &lt;li&gt;Software Engineering Process (2–4%) &lt;/li&gt;            &lt;li&gt;Software Engineering Tools and Methods (2–4%) &lt;/li&gt;            &lt;li&gt;Software Quality (6–8%) &lt;/li&gt;         &lt;/ul&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;&lt;u&gt;General References&lt;/u&gt; :&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Software Engineering: A Practitioner’s Approach, Latest ed&lt;/strong&gt;.,       &lt;br /&gt;Pressman, Roger S., New York, New York: McGraw-Hill. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Software Engineering, Sommerville, I., Latest ed&lt;/strong&gt;.       &lt;br /&gt;Reading, Massachusetts: Addison-Wesley. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;SE Body of knowledge&lt;/strong&gt; &lt;a href="http://www.swebok.org"&gt;http://www.swebok.org&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Online review course&lt;/strong&gt; &lt;a href="http://www.computer.org/certification/DistanceLearning/index.htm"&gt;http://www.computer.org/certification/DistanceLearning/index.htm&lt;/a&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Yahoo study group&lt;/strong&gt; &lt;a href="http://groups.yahoo.com/group/ieee_csdp/"&gt;http://groups.yahoo.com/group/ieee_csdp/&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Software Engineering Vol. 1: The Development Process, 2nd Edition&lt;/strong&gt;.       &lt;br /&gt;Dorfman, M. &amp;amp; Thayer, R., editors,       &lt;br /&gt;Los Alamitos, California: IEEE Computer Society Press, 2002. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Software Engineering Vol. 2: The Supporting Processes, 2nd Edition&lt;/strong&gt;.       &lt;br /&gt;Thayer, R. and M. Christensen, editors,       &lt;br /&gt;Los Alamitos,California: IEEE Computer Society Press, 2002. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;IEEE Software Engineering Collection, 1999, vols. 1-4&lt;/strong&gt;.       &lt;br /&gt;Piscataway, New Jersey: IEEE. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;u&gt;&lt;b&gt;Comprehensive Reading List &lt;/b&gt;&lt;b&gt;:&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;b&gt;&amp;quot;Goal-Question-Metric (GQM) Approach&amp;quot;&lt;/b&gt; | Software Tech News, vol. 11, no. 4, Dec. 2008 &lt;/li&gt;    &lt;li&gt;&lt;b&gt;A Specifier's Introduction to Formal Methods&lt;/b&gt; | Jeannette M. Wing | Computer, vol. 23, no. 9, Sept. 1990, pp.10-23 &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Algebraic Structures&lt;/b&gt; | Alex Lopez-Ortiz | Alex-Lopez Ortiz web page &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Applied Statistics and Probability for Engineers, 4th&lt;/b&gt; Edition | Douglas C. Montgomery and George C. Runger | Wiley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Applying Formal Specifications to Real-World Software Development&lt;/b&gt; | Girish Keshav Palshikar | IEEE Software, vol. 18, no. 6, Nov. 2001, pp. 89-9.7 &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Balancing Agility and Discipline: A Guide for the Perplexed&lt;/b&gt; | Barry Boehm and Richard Turner | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Code Complete, 2nd Edition&lt;/b&gt; | Steve McConnell | Microsoft Press &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Computer Algorithms, 2nd Edition&lt;/b&gt; | Ellis Horowitz, Sartaj Sahni, and Sanguthevar Rajasekaran | Silicon Press &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Computer Science: An Overview, 10th Edition&lt;/b&gt; | J. Glenn Brookshear | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Computer Security: Art and Science&lt;/b&gt; | Matt Bishop | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Configuration Management Principles and Practices&lt;/b&gt; | Anne Mette Jonassen Hass | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Design Patterns: Elements of Reusable Object-Oriented Software&lt;/b&gt; | Erich Gamma et al | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Discrete Mathematics and its Applications, 6th Edition&lt;/b&gt; | Kenneth Rosen | McGraw-Hill &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Documenting Software Architectures: Views and Beyond&lt;/b&gt; | Paul Clements et al | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Engineering by Design, 2nd Edition&lt;/b&gt; | Gerard Voland | Prentice Hall &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Executable UML: A Foundation for Model-Driven Architecture&lt;/b&gt; | Stephen J. Mellor and Marc J. Balcer | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Fundamental of Object-Oriented Design in UML, 2nd Edition&lt;/b&gt; | Meilir Page-Jones | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Guide to the Software Engineering Body of Knowledge&lt;/b&gt; | CS Press&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;IEEE-CS/ACM Joint Task Force on Software Engineering Ethics and Professional Practices&lt;/b&gt; | IEEE/ACM &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Managing and Leading Software Projects&lt;/b&gt; | Richard E. Fairley | CS Press &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Metrics and Models in Software Quality Engineering, 2nd Edition&lt;/b&gt; | Stephen H. Kan | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Numerical Mathematics and Computing, 6th Edition&lt;/b&gt; | E. Ward Cheney and David R. Kincaid | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Operating System Concepts, 8th Edition&lt;/b&gt; | Abraham Silberschatz, Peter Baer Galvin, and Greg Gagne | Wiley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Professional Issues in Software Engineering&lt;/b&gt; | Frank Bott, Allison Coleman, Jack Eaton, and Diane Rowland | Taylor and Francis &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Return on Software: Maximizing the Return on Your Software Investment&lt;/b&gt; | Steve Tockey | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Security-Oriented Pointwise Changes to SWEBOK Guide&lt;/b&gt; | Samuel Redwine &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Design, 2nd Edition&lt;/b&gt; | David Budgen | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Engineering, 8th Edition&lt;/b&gt; | Ian Sommerville | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Inspection&lt;/b&gt; | Tom Gilb and D. Graham | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Maintenance: Concepts and Practice, 2nd Edition&lt;/b&gt; | Penny Grubb and Armstrong A. Takang | World Scientific Publishing &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Security Engineering: A Guide for Project Managers&lt;/b&gt; | Julia H. Allen, Sean Barnum, Robert J. Ellison, Gary McGraw, and Nancy R. Mead | Addison Wesley &lt;/li&gt;    &lt;li&gt;&lt;b&gt;The Essentials of Computer Organization and Architecture, 2&lt;sup&gt;nd&lt;/sup&gt; Edition&lt;/b&gt; | Linda Null and Julia Lobur | Jones &amp;amp; Bartlett &lt;/li&gt;    &lt;li&gt;&lt;b&gt;The Road Map to Software Engineering: A Standards-Based Guide&lt;/b&gt; | James W. Moore | CS Press &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Usability Engineering&lt;/b&gt; | Jakob Nielsen | Morgan Kaufmann &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Hope you all the best,&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-1220734471215358176?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/VyML_yKtbPI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/VyML_yKtbPI/im-certified-software-development.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_R1exOFT_lVs/S8EAWkpM_FI/AAAAAAAAAHc/kmQxTSKV3uw/s72-c/untitled_thumb%5B1%5D.png?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/04/im-certified-software-development.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-7204544663646247028</guid><pubDate>Fri, 26 Mar 2010 14:54:00 +0000</pubDate><atom:updated>2010-03-26T17:36:04.870+02:00</atom:updated><title>Bugs in the generated code of Repository Factory for Visual Studio 2008</title><description>&lt;p&gt;If you are considering using repository factory for visual studio 2008 in your next project, please be aware that it had 2 bugs in code generation. These are in the generated Remove method in the repository classes of the DAL. This happens only if the corresponding table is a bridge table that links two tables together. &lt;/p&gt;  &lt;p&gt;If you had simple three tables in your database like the following:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_R1exOFT_lVs/S6zKpa2im8I/AAAAAAAAAHU/d7SDYuqwKLE/s1600-h/tables3.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="tables" border="0" alt="tables" src="http://lh4.ggpht.com/_R1exOFT_lVs/S6zKqTtOmDI/AAAAAAAAAHY/5GpqcMKnLVg/tables_thumb1.jpg?imgmax=800" width="392" height="273" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The generated Remove method in User_RoleRepository class will be like the following:&lt;/p&gt;  &lt;pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Remove(System.Int32 role_IDSystem.Int32 user_ID)&lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;{ &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   IDeleteFactory&amp;lt;DeleteUser_RoleIdentity&amp;gt; deleteFactory = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   User_RoleDeleteFactory(); &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   &lt;span style="color: #0000ff"&gt;try&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;     { &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      DeleteUser_RoleIdentity deleteUser_RoleIdentity = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;         DeleteUser_RoleIdentity(role_IDField, user_IDField); &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;.Remove(deleteFactory, deleteUser_RoleIdentity); &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;     } &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;   &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (SqlException ex) &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;     { &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;      HandleSqlException(ex, deleteFactory); &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;     }            &lt;br /&gt;&lt;/pre&gt;&lt;pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&amp;#39;Courier New&amp;#39;,courier,monospace; font-size: 12px"&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;The parameter separator had been omitted &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;The parameters passed to deleteUser_RoleIdentity had been suffixed by “Field” although it should be the same name as the passed parameters. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Take care of these 2 bugs in the generated code, you have to correct them in every class.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-7204544663646247028?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/L1RVLXIrjLI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/L1RVLXIrjLI/bugs-in-generated-code-of-repository.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_R1exOFT_lVs/S6zKqTtOmDI/AAAAAAAAAHY/5GpqcMKnLVg/s72-c/tables_thumb1.jpg?imgmax=800" height="72" width="72" /><feedburner:origLink>http://ebeid-soliman.blogspot.com/2010/03/bugs-in-generated-code-of-repository.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-2078821232241504010</guid><pubDate>Wed, 26 Aug 2009 05:32:00 +0000</pubDate><atom:updated>2009-08-26T07:34:52.068+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Programming Thoughts</category><title>Wisdom of Experts: Programming in vs. Programming into a language</title><description>&lt;p&gt;David Gries (1981)&amp;#160; pointed out that “your programming tools don’t have to determine how you think about programming”. He also made a distinction between programming &lt;em&gt;&lt;strong&gt;in&lt;/strong&gt;&lt;/em&gt; a language vs. programming &lt;strong&gt;&lt;em&gt;into &lt;/em&gt;&lt;/strong&gt;a language.&lt;/p&gt;  &lt;p&gt;Programmers who program “in” a language limit their thoughts to constructs that the language directly supports. If the language tools are primitive, the programmers thoughts will be also be primitive.&lt;/p&gt;  &lt;p&gt;Programmers who program “into” a language, first decide what thoughts they want to express, and then they determine how to express thoughts using the tools provided by their specific language.&lt;/p&gt;  &lt;p&gt;So, try as much as you can to program &lt;strong&gt;into &lt;/strong&gt;the language you use. If your language lacks constructs that you want to to use or is prone to other kinds of problems, try to compensate for them. Invent your own coding conventions, standards, class libraries, and other augmentations.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-2078821232241504010?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/-pQ_wJB1NTA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/-pQ_wJB1NTA/wisdom-of-experts-programming-in-vs.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>2</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/08/wisdom-of-experts-programming-in-vs.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-2403505418848381278</guid><pubDate>Sat, 15 Aug 2009 05:31:00 +0000</pubDate><atom:updated>2009-08-15T08:34:50.715+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Python</category><title>Using Python Scripts with IIS 7</title><description>&lt;p&gt;Here are the steps you should make if you want to use python scripts on IIS 7:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Please make sure Python is installed properly or refer to &lt;a href="http://ebeid-soliman.blogspot.com/2009/03/python-notes-1.html" target="_blank"&gt;Python Notes – 1 : Setup&lt;/a&gt; for installation steps.&lt;/li&gt;    &lt;li&gt;Make sure CGI module is installed in IIS 7. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Control Panel&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Programs&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Program and Features&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Turn Windows features on and off&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Internet Information Services&lt;/strong&gt; -&amp;gt; &lt;strong&gt;World Wide Web Services&lt;/strong&gt; -&amp;gt; &lt;strong&gt;Application Development Features&lt;/strong&gt; -&amp;gt; &lt;strong&gt;CGI module &lt;/strong&gt;(Ensure that it is selected).&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_R1exOFT_lVs/SoZHsToFdQI/AAAAAAAAAFg/lNjCD_Z3qnI/s1600-h/Screenshot%20Studio%20capture%20%231%5B5%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #1" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="425" alt="Screenshot Studio capture #1" src="http://lh4.ggpht.com/_R1exOFT_lVs/SoZHuXw03LI/AAAAAAAAAFk/K1DABd0FU6M/Screenshot%20Studio%20capture%20%231_thumb%5B3%5D.jpg?imgmax=800" width="362" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;Add web application for Python, In IIS Manager, right click &lt;strong&gt;Sites&lt;/strong&gt; –&amp;gt; &lt;strong&gt;Add Web Site..&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_R1exOFT_lVs/SoZHzBoygmI/AAAAAAAAAE4/Rw7jkbRy-kM/s1600-h/Screenshot%20Studio%20capture%20%232%5B3%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #2" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="240" alt="Screenshot Studio capture #2" src="http://lh4.ggpht.com/_R1exOFT_lVs/SoZH1C71TqI/AAAAAAAAAE8/pBjhfzy6APw/Screenshot%20Studio%20capture%20%232_thumb%5B1%5D.jpg?imgmax=800" width="242" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;In the &lt;strong&gt;Add Web Site&lt;/strong&gt; dialog box, set &lt;strong&gt;Site name &lt;/strong&gt;e.g.: PythonTest, and make it pointing to some folder like C:\PythonTest, then click &lt;strong&gt;OK&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_R1exOFT_lVs/SoZH3Cw_GFI/AAAAAAAAAFw/1t7xc7r9ktA/s1600-h/Screenshot%20Studio%20capture%20%234%5B4%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #4" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="446" alt="Screenshot Studio capture #4" src="http://lh6.ggpht.com/_R1exOFT_lVs/SoZH54qE0pI/AAAAAAAAAF4/ms6ART8wQGM/Screenshot%20Studio%20capture%20%234_thumb%5B2%5D.jpg?imgmax=800" width="461" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;In &lt;strong&gt;Features View&lt;/strong&gt;, open &lt;strong&gt;Handler Mappings&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_R1exOFT_lVs/SoZH7iHBHQI/AAAAAAAAAGA/cbowuq2B8bg/s1600-h/Screenshot%20Studio%20capture%20%235%5B6%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #5" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="381" alt="Screenshot Studio capture #5" src="http://lh6.ggpht.com/_R1exOFT_lVs/SoZH-VDW9TI/AAAAAAAAAGI/aRzXPDOXwS8/Screenshot%20Studio%20capture%20%235_thumb%5B4%5D.jpg?imgmax=800" width="563" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;On the left pane click &lt;strong&gt;Add Script Map ...&lt;/strong&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_R1exOFT_lVs/SoZIArQsLjI/AAAAAAAAAGQ/E5u9nOTSKqA/s1600-h/Screenshot%20Studio%20capture%20%237%5B4%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #7" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="237" alt="Screenshot Studio capture #7" src="http://lh4.ggpht.com/_R1exOFT_lVs/SoZIC_AiACI/AAAAAAAAAGU/CPKVDH-QLRs/Screenshot%20Studio%20capture%20%237_thumb%5B2%5D.jpg?imgmax=800" width="295" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;In &lt;strong&gt;Request&lt;/strong&gt; path, put &amp;quot;*.py&amp;quot; as the script files extension, In &lt;strong&gt;Executable&lt;/strong&gt; select &amp;quot;C:\Python25\Python.exe %s %s&amp;quot;, or change it according to your Python installation path. (The two &amp;quot;%s&amp;quot; after the executable are required for console-based script interpreters but would not be required for an Internet Server API [ISAPI]-based script interpreter). Then give the script mapping an appropriate Name, like Python. Click &lt;strong&gt;OK&lt;/strong&gt;.&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_R1exOFT_lVs/SoZIEUnp4NI/AAAAAAAAAFY/Ua1obD_qe24/s1600-h/Screenshot%20Studio%20capture%20%238%5B3%5D.jpg"&gt;&lt;img title="Screenshot Studio capture #8" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="369" alt="Screenshot Studio capture #8" src="http://lh3.ggpht.com/_R1exOFT_lVs/SoZIFyPZSVI/AAAAAAAAAFc/qzJr3U5nlJY/Screenshot%20Studio%20capture%20%238_thumb%5B1%5D.jpg?imgmax=800" width="452" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;Create a test.py into the virtual directory (C:\PythonTest), and copy the following script into it.&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;print &lt;/p&gt;    &lt;p&gt;print 'Status: 200 OK' &lt;/p&gt;    &lt;p&gt;print 'Content-type: text/html' &lt;/p&gt;    &lt;p&gt;print &lt;/p&gt;    &lt;p&gt;print '&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;TITLE&amp;gt;Python Sample CGI&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;' print '&amp;lt;BODY&amp;gt;' &lt;/p&gt;    &lt;p&gt;print '&amp;lt;H1&amp;gt;This is a header&amp;lt;/H1&amp;gt;' &lt;/p&gt;    &lt;p&gt;print '&amp;lt;p&amp;gt;' #this is a comment &lt;/p&gt;    &lt;p&gt;print 'See this is just like most other HTML' &lt;/p&gt;    &lt;p&gt;print '&amp;lt;br&amp;gt;' &lt;/p&gt;    &lt;p&gt;print '&amp;lt;/BODY&amp;gt;'&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;Then type &lt;a title="http://localhost/pythontest/test.py" href="http://localhost/pythontest/test.py"&gt;http://localhost/pythontest/test.py&lt;/a&gt; into your IE URL bar to verify it is working.&lt;/li&gt; &lt;/ul&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-2403505418848381278?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/yGcq2pIaqxI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/yGcq2pIaqxI/using-python-scripts-with-iis-7.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_R1exOFT_lVs/SoZHuXw03LI/AAAAAAAAAFk/K1DABd0FU6M/s72-c/Screenshot%20Studio%20capture%20%231_thumb%5B3%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/08/using-python-scripts-with-iis-7.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-6631312720756478463</guid><pubDate>Mon, 25 May 2009 04:10:00 +0000</pubDate><atom:updated>2009-05-25T07:10:15.906+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Peer-to-Peer</category><title>Introduction to Peer-to-Peer Computing</title><description>&lt;p&gt;Peer-to-peer (P2P) has become a buzzword that subsumes concepts used in communication technologies, distributed system models, applications, platforms, etc. It is not a particular initiative, nor architecture or specific technology; it rather describes a set of concepts and mechanisms for decentralized distributed computing and direct peer-to-peer information and data interchange. &lt;/p&gt;  &lt;p&gt;So, &lt;strong&gt;What is Peer-to-Peer Computing ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There is no one definition of P2P.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;P2P is a class of applications that takes advantages of resources available at the edges of the internet. &lt;/li&gt;    &lt;li&gt;The sharing of computer resources and services by direct exchange between systems. &lt;/li&gt;    &lt;li&gt;In Peer-to-Peer computing, each computer can serve the function of both client and server. Any computer in the network can initiate questions (as a client), receive questions and transmit data (as a server). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We could conclude these definitions by proposing the following definition.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;P2P computing is a distributed computing architecture with the following properties:&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;Resource sharing &lt;/li&gt;    &lt;li&gt;Dual client/server role of network nodes. &lt;/li&gt;    &lt;li&gt;Decentralization/Autonomy &lt;/li&gt;    &lt;li&gt;Scalability &lt;/li&gt;    &lt;li&gt;Robustness/Self-Organization &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Once you understood that P2P is a distributed computing architecture, you might be started asking: &lt;strong&gt;Why P2P architecture since we have Client/Server architecture ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Client/Server architecture is a well known, powerful architecture. In it, the server is the data and functionality source, and clients requests data and processing from it. Although Client/Server architecture is very successful (www [HTTP], FTP, Web services, etc…), it have some issues; which are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Scalability is hard to achieve. &lt;/li&gt;    &lt;li&gt;Presents a single point of failure. &lt;/li&gt;    &lt;li&gt;Requires administration. &lt;/li&gt;    &lt;li&gt;Didn’t utilize the resources at the network edges (clients). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Although many of these issues had been solved with many solutions, P2P tried to address these issues in a new and very different way. The following figure emphasizes the general difference between P2P and client/server architecture models.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_R1exOFT_lVs/ShoaHaBowYI/AAAAAAAAAEo/SRIc5hmTU70/s1600-h/P2P%5B4%5D.jpg"&gt;&lt;img title="P2P" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="268" alt="P2P" src="http://lh6.ggpht.com/_R1exOFT_lVs/ShoaJVKe6zI/AAAAAAAAAEs/VPkR3Vhciws/P2P_thumb%5B2%5D.jpg?imgmax=800" width="540" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Why P2P Now ?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Since the concepts that underlie the P2P computing model is not a new computing concept, it is reasonable to ask why the P2P model has burst on the scene at this time. This happened due to many recent changes in the computing world:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The ubiquity of connected computers has come close to enabling anywhere, anytime access to the Net and its resources. &lt;/li&gt;    &lt;li&gt;The critical mass of computer users. &lt;/li&gt;    &lt;li&gt;Improvements in communications bandwidth, still on a fast-track growth curve, make it possible to move large amounts of data and rich media content from one location to another. &lt;/li&gt;    &lt;li&gt;Today’s PCs are sufficiently robust, in terms of processing power and storage capacity, to handle the extra services required in a P2P environment. &lt;/li&gt;    &lt;li&gt;the emergence of complementary technologies, including recent advances in wireless and software agents that provide more avenues for interesting P2P applications. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;While all of these are necessary conditions for P2P computing, something more is required. History shows that a trigger is needed for a new technology to really take off. In the computer industry this is often referred to as a “killer app” The electronic spreadsheet triggered the proliferation of the PC, and the Mosaic browser triggered the transformation of the Internet into the World Wide Web.&lt;/p&gt;  &lt;p&gt;The P2P computing model found its trigger with &lt;strong&gt;Napster&lt;/strong&gt;, followed by &lt;strong&gt;Gnutella&lt;/strong&gt;. Their huge popularity got everyone talking about P2P and helped further stimulate other P2P applications such as &lt;strong&gt;Freenet&lt;/strong&gt; and &lt;strong&gt;&lt;a href="mailto:SETI@home"&gt;SETI@home&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;P2P Benefits&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Efficient use of resources.      &lt;ul&gt;       &lt;li&gt;Unused bandwidth, storage, processing power at the edge of the network. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Scalability      &lt;ul&gt;       &lt;li&gt;Consumers of resources also donate resources. &lt;/li&gt;        &lt;li&gt;Aggregate resources grow naturally with utilization. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Reliability      &lt;ul&gt;       &lt;li&gt;Replicas of data. &lt;/li&gt;        &lt;li&gt;Geographic distribution &lt;/li&gt;        &lt;li&gt;No single point of failure. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt;    &lt;li&gt;Ease of administration      &lt;ul&gt;       &lt;li&gt;Nodes are self organize. &lt;/li&gt;        &lt;li&gt;No need to deploy servers to satisfy demand. &lt;/li&gt;        &lt;li&gt;Built-in fault tolerance, replication, and load balancing. &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Since P2P is not well-defined and it is only defined by set of characteristics and properties that are attributed to P2P systems, we will discuss some of common characteristics of P2P systems. This does not imply that every P2P system has to comply with all of these characteristics or even with a fixed number of them. These are general features that can be used to identify P2P systems.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Structural Characteristics&lt;/strong&gt;       &lt;ol&gt;       &lt;li&gt;&lt;strong&gt;Decentralization&lt;/strong&gt;           &lt;ol&gt;           &lt;li&gt;This includes distributed storage, processing, information sharing, etc.. &lt;/li&gt;            &lt;li&gt;&lt;strong&gt;Advantage&lt;/strong&gt;&amp;#160; &lt;ol&gt;               &lt;li&gt;increased extensibility. &lt;/li&gt;                &lt;li&gt;Higher system availability and resilience. &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;            &lt;li&gt;&lt;strong&gt;Disadvantage&lt;/strong&gt;               &lt;ol&gt;               &lt;li&gt;Difficult to get or maintain a global view of the system state. &lt;/li&gt;                &lt;li&gt;System behavior is not deterministic. &lt;/li&gt;                &lt;li&gt;Interoperability is a big issue. &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;        &lt;li&gt;&lt;strong&gt;Self-organizing&lt;/strong&gt;           &lt;ol&gt;           &lt;li&gt;The different system components work together without any central management instance assigning roles and tasks. &lt;/li&gt;            &lt;li&gt;&lt;strong&gt;Disadvantage&lt;/strong&gt;               &lt;ol&gt;               &lt;li&gt;It is difficult to determine the system structure or predict the system behavior as long as no system-wide, governing policies apply. &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;        &lt;li&gt;&lt;strong&gt;Fault-tolerance&lt;/strong&gt;           &lt;ol&gt;           &lt;li&gt;Since there is no central point of failure.              &lt;ol&gt;               &lt;li&gt;The system performance variance due to nodes leaves and joins might imply that there is a lack of consistency. &lt;/li&gt;             &lt;/ol&gt;           &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Operational Characteristics&lt;/strong&gt;       &lt;ol&gt;       &lt;li&gt;&lt;strong&gt;Transparency&lt;/strong&gt;           &lt;ol&gt;           &lt;li&gt;This means transparency to the application or user in forms of communications, location, access, replication transparency for data and information.&amp;#160; Also the scale of system should be kept transparent to the user. &lt;/li&gt;            &lt;li&gt;This requires a middleware layer. &lt;/li&gt;         &lt;/ol&gt;       &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;P2P Applications&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;P2P is good for:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;File sharing (Napster, Gnutella, Kazza)&lt;/li&gt;    &lt;li&gt;Multiplayer games (Unreal Tournament, DOOM)&lt;/li&gt;    &lt;li&gt;Collaborative applications (ICQ, share whiteboard)&lt;/li&gt;    &lt;li&gt;Distributed computation (&lt;a href="mailto:Seti@home"&gt;Seti@home&lt;/a&gt;)&lt;/li&gt;    &lt;li&gt;Ad-hoc networks&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;P2P can be applied in many areas, and there are massive work in many areas to utilize the P2P concepts and techniques.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-6631312720756478463?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/txGsBDvHzK8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/txGsBDvHzK8/introduction-to-peer-to-peer-computing.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_R1exOFT_lVs/ShoaJVKe6zI/AAAAAAAAAEs/VPkR3Vhciws/s72-c/P2P_thumb%5B2%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/05/introduction-to-peer-to-peer-computing.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-2949036957063661811</guid><pubDate>Sat, 16 May 2009 02:07:00 +0000</pubDate><atom:updated>2009-05-16T05:07:07.291+03:00</atom:updated><title>Python Notes 14: Advanced Network Operations</title><description>&lt;p&gt;We have explored the usual issues in network programming, both on client side and server side. In this post we will discuss some advanced topics in network programming.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Half-Open Sockets&lt;/strong&gt;     &lt;br /&gt;Normally, sockets are bidirectional—data can be sent across them in both directions. Sometimes, you may want to make a socket be unidirectional so data can only be sent in one direction. A socket that's unidirectional is said to be a half-open socket. A socket is made half-open by calling shutdown(), and that procedure is irreversible for that socket. Half-open sockets are useful when &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;You want to ensure that all data written has been transmitted. When shutdown() is called to close the output channel of a socket, it will not return until all buffered data has been successfully transmitted. &lt;/li&gt;    &lt;li&gt;You want to have a way to catch potential programming errors that may cause the program to write to a socket that shouldn't be written to, or read from a socket that shouldn't be read from.&lt;/li&gt;    &lt;li&gt;Your program uses fork() or multiple threads, and you want to prevent other processes or threads from doing certain operations, or you want to force a socket to be closed immediately.      &lt;br /&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The socket. shutdown() call is used to accomplish all of these tasks. &lt;/p&gt;  &lt;p&gt;The call to shutdown() requires a single argument that indicates how you want to shut down the socket. Its possible values are as follows: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;0 to prevent future reads &lt;/li&gt;    &lt;li&gt;1 to prevent future writes &lt;/li&gt;    &lt;li&gt;2 to prevent future reads and writes &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Once shut down in a given direction, the socket can never be reopened in that direction. Calls to shutdown() are cumulative; calling shutdown(0) followed by shutdown(1) will achieve the same effect as calling shutdown(2). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Timeouts&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;TCP connections can be held open indefinitely, even if there's no traffic flowing across them. Timeouts are useful    &lt;br /&gt;for discovering error conditions or communication problems in some instances. &lt;/p&gt;  &lt;p&gt;To enable timeout detection on a Python socket, you call settimeout() on the socket, passing it the number of seconds until a timeout is reached. Later, when you make a socket call and nothing has happened for that amount of time, a socket.timeout exception is raised. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Transmitting Strings&lt;/strong&gt;     &lt;br /&gt;One common problem that arises when sending data across the network is that of transmitting variable-length strings. When you read information from a TCP stream you don't know when the sender has finished giving you a piece of data unless you build some sort of indication into your protocol. There are two common approaches to solving this problem: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;End-of-string identifier&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Terminate the string with ‘\n’ or NULL&lt;/li&gt;      &lt;li&gt;Problem: Terminator might occur in the data if we transmit binary data.&lt;/li&gt;      &lt;li&gt;Solutions:&lt;/li&gt;      &lt;ul&gt;       &lt;li&gt;Escape the identifier.&lt;/li&gt;        &lt;li&gt;Encode data in base64&lt;/li&gt;        &lt;li&gt;use different if found in data and send the new identifier before the data.&lt;/li&gt;     &lt;/ul&gt;   &lt;/ul&gt;    &lt;li&gt;Leading fixed-length size indicator&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Send a constant number of bytes containing the size of the string.&lt;/li&gt;      &lt;li&gt;The “size” itself could be sent as characters or as binary data, characters are simpler, however you have to pad them to get a constant length.&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Using Broadcast Data&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When you broadcast a UDP packet, it's sent to all machines    &lt;br /&gt;connected to your LAN. The underlying transport, such as Ethernet, will have a special mode that lets you do this without having to repeat the packet for each computer.     &lt;br /&gt;On the receiver's side, when a broadcast packet is received, the kernel looks at the destination port number. If it has a process listening to that port, the packet is sent to that process. Otherwise, it's silently discarded. Therefore, simply sending out a broadcast packet will not harm or impact machines that don't have a server listening for it.     &lt;br /&gt;Broadcast packets are often used for the following types of activities: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Automatic service discovery: For instance, a computer might send out a broadcast packet looking for all print servers of a particular type.&lt;/li&gt;    &lt;li&gt;Automatic service announcements: A server providing a service for a LAN might periodically broadcast the availability of that service. Clients would listen for those broadcasts.&lt;/li&gt;    &lt;li&gt;Searching for LAN computers that implement a specific protocol. For instance, a chat program might send out a broadcast packet looking for other people on the LAN with the same chat program. It might then compile a list and present it to the user. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;To be able to broadcast data, you need to set the socket option on client and server as follows:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;On the sender, instead of sending to a particular IP, send to ‘&amp;lt;broadcast&amp;gt;’&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;s.sendto(‘&amp;lt;broadcast&amp;gt;’,123)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#000000"&gt;In this post we dealt with a few advanced issues in network programming.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-2949036957063661811?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/2cIyLNoQ-L0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/2cIyLNoQ-L0/python-notes-14-advanced-network.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/05/python-notes-14-advanced-network.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-6913611243490403259</guid><pubDate>Fri, 15 May 2009 23:38:00 +0000</pubDate><atom:updated>2009-05-16T02:38:24.178+03:00</atom:updated><title>Python Notes 13: Network servers</title><description>&lt;p&gt;For a client, the process of establishing a TCP connection is a two-step process that includes the creation of the socket object and a call to connect() to establish a connection to the server. For a server, the process requires the following four steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create the socket object. &lt;/li&gt;    &lt;li&gt;Set the socket options (optional). &lt;/li&gt;    &lt;li&gt;Bind to a port (and, optionally, a specific network card). &lt;/li&gt;    &lt;li&gt;Listen for connections. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Example of these steps:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;host = '' # Bind to all interfaces        &lt;br /&gt;port = 51423         &lt;br /&gt;# Step 1 (Create the socket object)         &lt;br /&gt;s = socket. socket(socket.AF_INET, socket.SOCK_STREAM)         &lt;br /&gt;# Step 2 (Set the socket options)         &lt;br /&gt;s.setsockopt(socket.SOL_SOCKET, socket.SOREUSEADDR, l)         &lt;br /&gt;# Step 3 (Bind to a port and interface)         &lt;br /&gt;s.bind((host, port))         &lt;br /&gt;# Step 4 (Listen for connections)         &lt;br /&gt;s.listen(5)&lt;/font&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Setting and Getting Socket Options &lt;/strong&gt;    &lt;br /&gt;There are many different options that can be set for a socket. For general-purpose servers, the socket option of greatest interest is called SOREUSEADDR. Normally, after a server process terminates, the operating system reserves its port for a few minutes, thereby preventing any other process (even another instance of your server itself) from opening it until the timeout expires. If you set the SOREUSEADDR flag to true, the operating system releases the server port as soon as the server socket is closed or the server process terminates.This is done through:&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;s.setsockopt(socket.SOL_SOCKET, socket.SOJEUSEADDR, l) &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Binding the Socket&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The next step is to claim a port number for the server. This process is called binding. To bind to a port, you call:    &lt;br /&gt;s.bind((‘’, 111)) &lt;/p&gt;  &lt;p&gt;The first argument to bind() specifies the IP address to bind to it. It's generally left blank, which means &amp;quot;bind to all interfaces and addresses.&amp;quot; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Listening for Connections &lt;/strong&gt;    &lt;br /&gt;The last step before actually accepting client connections is to call listen(). This call tells the operating system to prepare to receive connections. It takes a single parameter, which indicates how many pending connections the operating system should allow to remain in queue before the server actually gets around to processing them. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Accepting Connections&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Most servers designed to run indefinitely and service multiple connections, this is usually done with a carefully designed infinite loop. Example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import socket        &lt;br /&gt;host = '' # Bind to all interfaces         &lt;br /&gt;port = 51423         &lt;br /&gt;s = socket.socket(socket.AF_INET, socket.SOCKJTREAM)         &lt;br /&gt;s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, l)         &lt;br /&gt;s.bind((host, port))         &lt;br /&gt;print &amp;quot;Waiting for connections...&amp;quot;         &lt;br /&gt;s.listen(l)         &lt;br /&gt;while l:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; clientsock, clientaddr = s.acceptQ         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Got connection from&amp;quot;, clientsock.getpeername()         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; clientsock.close()&lt;/font&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Using User Datagram Protocol&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To use UDP on the server, you create a socket, set the options, and bind () just like with TCP However, there's no need for listen () or accept ()—just use recvf rom().    &lt;br /&gt;This function actually returns two pieces of information: the received data, and the address and port number of the program that sent the data. Because UDP is connectionless, this is all you need to be able to send back a reply. Example, echo server:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import socket, traceback        &lt;br /&gt;host = '' # Bind to all interfaces         &lt;br /&gt;port = 51423         &lt;br /&gt;s = socket.socket(socket.AF_INET, socket.SOCK_DCRAM)         &lt;br /&gt;s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, l)         &lt;br /&gt;s.bind((host, port))         &lt;br /&gt;while l:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; message, address = s.recvfrom(8l92)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Cot data from&amp;quot;, address         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; s.sendto(message, address)&amp;#160;&amp;#160; #Echo it back        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; except (Keyboardlnterrupt, SystemExit):         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; raise         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; except:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; traceback.print_exc()&lt;/font&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In this post we have clarified some points in network servers.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-6913611243490403259?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/Teh5Uh1bm_k" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/Teh5Uh1bm_k/python-notes-13-network-servers.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/05/python-notes-13-network-servers.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-1836562697685840799</guid><pubDate>Fri, 15 May 2009 23:17:00 +0000</pubDate><atom:updated>2009-05-16T02:17:49.335+03:00</atom:updated><title>Python Notes 12 : Network clients</title><description>&lt;p&gt;After we have explored the basics of network programming in brief in the &lt;a href="http://ebeid-soliman.blogspot.com/2009/05/python-notes-11-introduction-to-network.html"&gt;previous post&lt;/a&gt;, we will discuss network clients in more details in this post.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Understanding Sockets&lt;/strong&gt;     &lt;br /&gt;Sockets are an extension to the operating system's I/O system that enable communication between processes and machines. It can be treated the same as standard files, with the same interface methods so in many cases, a program need not know whether it's writing data to a file, the terminal, or a TCP connection. While many files are opened with the open () call, sockets are created with the socket () call and additional calls are needed to connect and activate them. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Creating Sockets&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;For a client program, creating a socket is generally a two-step process. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create the actual socket object. &lt;/li&gt;    &lt;li&gt;Connect the socket to the remote server. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;When you create a socket object, you need to tell the system two things: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The communication type: the underlying protocol used to transmit data. Examples of protocols include IPv4 (current Internet standard), IPv6 (future Internet standard), IPX/ SPX (NetWare), and AFP (Apple file sharing). By far the most common is IPv4. &lt;/li&gt;    &lt;li&gt;The protocol family: defines how data is transmitted.      &lt;br /&gt;For Internet communications, which make up the bulk of this book, the communication type is almost always AF_INET (corresponding to IPv4). The protocol family is typically either:       &lt;ul&gt;       &lt;li&gt;SOCK_STREAM for TCP communications or &lt;/li&gt;        &lt;li&gt;SOCK_DGRAM for UDP communications &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;For a TCP connection, creating a socket generally uses    &lt;br /&gt;code like this: &lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff"&gt;s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) &lt;/font&gt;To connect the socket, you'll generally need to provide a tuple containing the remote hostname or IP address and the remote port. Connecting a socket typically looks like this:     &lt;br /&gt;&lt;font color="#0000ff"&gt;s.connect((&amp;quot;www.example.com&amp;quot;, 80))&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Finding the port number&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Most operating systems ship with a list of well-known server port numbers which you can query. On windows systems, you can find this file at C:\Windows\System32\drivers\etc\services. To query this list, you need two parameters: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;A protocol name &lt;/li&gt;    &lt;li&gt;A port name. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This query is like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt;print socket.getservbyname(‘ftp’,’tcp’)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;21&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You didn't have to know in advance that FTP uses port 80. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Getting Information from a Socket&lt;/strong&gt;     &lt;br /&gt;Once you've established a socket connection, you can find out some useful information from it.&lt;/p&gt;  &lt;p&gt;s.getsockname() #Get your IP address and port number&lt;/p&gt;  &lt;p&gt;s.getpeername() #Get the remote machine IP address and port number&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Socket Exceptions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Different network calls can raise different exceptions when network errors occur. Python's socket module actually defines four possible exceptions: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;socket.error for general I/O and communication problems. &lt;/li&gt;    &lt;li&gt;socket.gaierror for errors looking up address information &lt;/li&gt;    &lt;li&gt;socket.herror for other addressing errors. &lt;/li&gt;    &lt;li&gt;socket.timeout for handling timeouts that occur after settimeout() has been called on a socket. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Complete Example&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The example program takes three command-line arguments: a host to which it will connect, a port number or name on the server, and a file to request from the server. The program will connect to the server, send a simple HTTP    &lt;br /&gt;request for the given filename, and display the result. Along the way, it exercises care to handle various types of potential errors. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import socket, sys        &lt;br /&gt;host = sys.argv[l]         &lt;br /&gt;textport = sys.argv[2]         &lt;br /&gt;filename = sys.argv[3]         &lt;br /&gt;try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)         &lt;br /&gt;except socket.error, e:&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Strange error creating socket: %s&amp;quot; % e&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.exit(l)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; # Try parsing it as a numeric port number.         &lt;br /&gt;try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; port = int(textport)         &lt;br /&gt;except ValueError:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; # That didn't work, so it's probably a protocol name.         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; # Look it up instead,         &lt;br /&gt;try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; port = socket.getservbyname(textport, 'tcp')         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; except socket.error, e:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Couldn't find your port: %s&amp;quot; % e         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.exit(i) &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;try:        &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; s.connect((host, port))         &lt;br /&gt;except socket.gaierror, e:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Address-related error connecting to server: %s&amp;quot; % e         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.exit(i)         &lt;br /&gt;except socket.error, e:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Connection error: %s&amp;quot; % e         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.exit(l)         &lt;br /&gt;try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; s.sendall(&amp;quot;CET %s HTTP/1.0\r\n\r\n&amp;quot; % filename)         &lt;br /&gt;except socket.error, e:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Error sending data: %s&amp;quot; % e         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.exit(i)         &lt;br /&gt;while 1:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; try:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; buf = s.recvB048)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; except socket.error, e:         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Error receiving data: %s&amp;quot; % e         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sys.exit(l)         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if not len(buf):         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break         &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; sys.stdout.write(buf)&lt;/font&gt; &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Using User Datagram Protocol&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In UDP there is no sufficient control over how data is sent and received. Working with UDP clients differs than TCP clients in the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When create the socket ask for SOCKDGRAM      &lt;br /&gt;instead of SOCKSTREAM; this indicates to the operating system that the socket will       &lt;br /&gt;be used for UDP instead of TCP communications. &lt;/li&gt;    &lt;li&gt;When call socket.getservbyname(), pass ‘udp’ instead of ‘tcp’. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In this post we discussed network clients in a little bit depth. In the next post we will discuss network servers.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-1836562697685840799?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/aNqoEU3n3H8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/aNqoEU3n3H8/python-notes-12-network-clients.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/05/python-notes-12-network-clients.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-552760981910291874</guid><pubDate>Fri, 15 May 2009 08:00:00 +0000</pubDate><atom:updated>2009-05-15T12:48:15.361+03:00</atom:updated><title>Python Notes 11 : Introduction to Network Programming</title><description>&lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Network Overview&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Python provides a wide assortment of network support.&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Low-level programming with sockets (if you want to create a protocol).&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Support for existing network protocols (HTTP, FTP, SMTP, etc...).&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Web programming (CGI scripting and HTTP servers).&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Data encoding&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Network Basics: TCP/IP&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Python’s networking modules primarily support TCP/IP.&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;TCP - A reliable connection-oriented protocol (streams).&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;UDP - An unreliable packet-oriented protocol (datagrams).&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;TCP is the most common (HTTP, FTP, SMTP, etc...). Both protocols are supported using &amp;quot;sockets&amp;quot;.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;A socket is a file-like object. Allows data to be sent and received across the network like a file. But it also includes functions to accept and establish connections. Before two machines can establish a connection, both must create a socket&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Network Basics: Ports&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;In order to receive a connection, a socket must be bound to a port (by the server). A port is a number in the range 0-65535 that’s managed by the OS. Used to identify a particular network service (or listener). Ports 0-1023 are reserved by the system and used for common protocols:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;FTP Port 20&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Telnet Port 23&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;SMTP (Mail) Port 25&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;HTTP (WWW) Port 80&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;Ports above 1024 are reserved for user processes.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Socket programming in a nutshell&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Server creates a socket, binds it to some well-known port number, and starts listening.&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Client creates a socket and tries to connect it to the server (through the above port).&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Server-client exchange some data.&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;Close the connection (of course the server continues to listen for more clients).&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;Socket Programming Example&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;The socket module&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Provides access to low-level network programming functions. The following example is a simple server that returns the current time&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;import time, socket&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s = socket(AF_INET, SOCK_STREAM)#Create TCP socket&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s.bind((&amp;quot;&amp;quot;,8888))&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Bind to port 8888&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s.listen(5)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Start listening&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;while 1:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; client,addr = s.accept()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Wait for a connection&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Got a connection from &amp;quot;, addr&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; client.send(time.ctime(time.time())) #Send time back&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; client.close()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Notes&lt;/strong&gt;: The socket first opened by server is not the same one used to exchange data.Instead, the accept() function returns a new socket for this (’client’ above).listen() specifies max number of pending connections&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;The following example is the client program for the above time server which connect to time server and get current time.&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;from socket import *&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s = socket(AF_INET,SOCK_STREAM) #Create TCP socket&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s.connect((&amp;quot;google.com&amp;quot;,8888))&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Connect to server&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;tm = s.recv(1024)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Receive up to 1024 bytes&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;s.close()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Close connection&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;print &amp;quot;The time is&amp;quot;, tm&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Notes: &lt;/strong&gt;Once connection is established, server/client communicate using send() and recv(). Aside from connection process, it’s relatively straightforward. Of course, the devil is in the details. And are there ever a LOT of details.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;font size="2"&gt;The Socket Module&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;The socket module used for all low-level networking, cr&lt;/font&gt;&lt;font size="2"&gt;eation and manipulation of sockets, and g&lt;/font&gt;&lt;font size="2"&gt;eneral purpose network functions (hostnames, data conversion, etc...). It’s a&lt;/font&gt;&lt;font size="2"&gt; direct translation of the BSD socket interface.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Utility Functions&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.gethostbyname(hostname) # Get IP address for a host&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.gethostname() # Name of local machine&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.ntohl(x) # Convert 32-bit integer to host order&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.ntohs(x) # Convert 16-bit integer to host order&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.htonl(x) # Convert 32-bit integer to network order&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff" size="2"&gt;socket.htons(x) # Convert 16-bit integer to network order&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Comments: &lt;/strong&gt;&lt;/font&gt;&lt;font size="2"&gt;Network order for integers is big-endian. &lt;/font&gt;&lt;font size="2"&gt;Host order may be little-endian or big-endian (depends on the machine).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;The &lt;font color="#0000ff"&gt;socket(family, type, proto)&lt;/font&gt; function c&lt;/font&gt;&lt;font size="2"&gt;reates a new socket object. &lt;em&gt;F&lt;/em&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;em&gt;amily&lt;/em&gt; is usually set to &lt;font color="#0000ff"&gt;AF_INET&lt;/font&gt;&lt;font color="#000000"&gt;. &lt;em&gt;T&lt;/em&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;em&gt;ype&lt;/em&gt; is one of:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;SOCK_STREAM&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Stream socket (TCP)&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;SOCK_DGRAM&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Datagram socket (UDP)&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;SOCK_RAW&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Raw socket&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;em&gt;Proto&lt;/em&gt; is usually only used with raw sockets:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;IPPROTO_ICMP&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;IPPROTO_IP&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;IPPROTO_RAW&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;IPPROTO_TCP&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;IPPROTO_UDP&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Socket methods&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;s.accept()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Accept a new connection&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.bind(address)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Bind to an address and port&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.close()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Close the socket&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.connect(address)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Connect to remote socket&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.fileno()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Return integer file descriptor&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.getpeername()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Get name of remote machine&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.getsockname()&amp;#160;&amp;#160;&amp;#160; #Get socket address as (ipaddr,port)&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.getsockopt(...)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Get socket options&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.listen(backlog)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Start listening for connections&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.makefile(mode)&amp;#160;&amp;#160; # Turn socket into a file like object&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.recv(bufsize)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Receive data&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.recvfrom(bufsize)&amp;#160;&amp;#160;&amp;#160; # Receive data (UDP)&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.send(string)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Send data&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.sendto(string, address)&amp;#160;&amp;#160;&amp;#160; # Send packet (UDP)&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.setblocking(flag)&amp;#160;&amp;#160; #Set blocking or nonblocking mode&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.setsockopt(...)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Set socket options&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;s.shutdown(how)&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Shutdown one or both halves of connection&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;There are a huge variety of configuration/connection options. &lt;/font&gt;&lt;font size="2"&gt;You’ll definitely want a good reference at your side&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;The SocketServer Module&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Provides a high-level class-based interface to sockets. E&lt;/font&gt;&lt;font size="2"&gt;ach protocol is e&lt;font size="2"&gt;ncapsulated &lt;/font&gt;in a class (TCPServer, UDPServer, etc.). It also p&lt;/font&gt;&lt;font size="2"&gt;rovides a series of handler classes that specify additional server behavior.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;To create a network service, need to inherit from both a protocol and handler class. &lt;/font&gt;&lt;font size="2"&gt;Example, the same time server we done before:&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;import SocketServer&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;import time&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;# This class actually implements the server functionality&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;class TimeHandler(SocketServer.BaseRequestHandler):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; def handle(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; self.request.send(time.ctime(time.time()))&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;# Create the server&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;server = SocketServer.TCPServer(&amp;quot;&amp;quot;,8888),TimeHandler)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;server.serve_forever()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Notes: &lt;/strong&gt;&lt;/font&gt;&lt;font size="2"&gt;The module provides a number of specialized server and handler types. &lt;/font&gt;&lt;font size="2"&gt;Ex: ForkingTCPServer, ThreadingTCPServer, StreamRequestHandler, etc.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Common Network Protocols&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Modules are available for a variety of network protocols:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;ftplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FTP protocol&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;smtplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SMTP (mail) protocol&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;nntplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; News&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;gopherlib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Gopher&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;poplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; POP3 mail server&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;imaplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; IMAP4 mail server&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;telnetlib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Telnet protocol&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;httplib&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HTTP protocol&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;These modules are built using sockets, but operate on a very low-level. Working with them r&lt;/font&gt;&lt;font size="2"&gt;equires a good understand of the underlying protocol. &lt;/font&gt;&lt;font size="2"&gt;But can be quite powerful if you know exactly what you are doing&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;The httplib Module&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Implements the HTTP 1.0 protocol and c&lt;/font&gt;&lt;font size="2"&gt;an use to talk to a web server.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;HTTP in two bullets:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Client (e.g., a browser) sends a request to the server&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;GET /index.html HTTP/1.0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Connection: Keep-Alive&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Host: &lt;/font&gt;&lt;a href="http://www.python.org"&gt;&lt;font size="2"&gt;www.python.org&lt;/font&gt;&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;User-Agent: Mozilla/4.61 [en] (X11; U; SunOS 5.6 sun4u)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;[blank line]&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;Server responds with something like this:&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;HTTP/1.0 200 OK&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Content-type: text/html&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Content-length: 72883&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Headers: blah&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;[blank line]&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;Data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Making an HTTP connection&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;import httplib&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;h = httplib.HTTP(&amp;quot;www.python.org&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;h.putrequest(’GET’,’/index.html’)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;h.putheader(’User-Agent’,’Lame Tutorial Code’)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;h.putheader(’Accept’,’text/html’)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;h.endheaders()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;errcode,errmsg, headers = h.getreply()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;f = h.getfile()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Get file object for reading data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;data = f.read()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;f.close()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;You should understand some HTTP to work with httplib.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;The urllib Module&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;A high-level interface to HTTP and FTP which p&lt;/font&gt;&lt;font size="2"&gt;rovides a file-like object that can be used to connect to remote servers&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;import urllib&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;f = urllib.urlopen(&amp;quot;http://www.python.org/index.html&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;data = f.read()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;f.close()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;Utility functions&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;urllib.quote(str)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Quotes a string for use in a URL&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;urllib.quote_plus(str)&amp;#160;&amp;#160;&amp;#160; # Also replaces spaces with ’+’&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;urllib.unquote(str)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Opposite of quote()&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;urllib.unquote_plus(str)&amp;#160; # Opposite of quote_plus()&lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;urllib.urlencode(dict) &lt;/font&gt;&lt;/li&gt;    &lt;li&gt;&lt;font size="2"&gt;# Turns a dictionary of key=value&lt;/font&gt;&lt;font size="2"&gt; pairs into a HTTP query-string&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="2"&gt;Examples&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;urllib.quote(&amp;quot;ebeid@ieee&amp;quot;)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #Produces &amp;quot;ebeid%40ieee&amp;quot;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#0000ff" size="2"&gt;urllib.unquote(&amp;quot;%23%21/bin/sh&amp;quot;)&amp;#160;&amp;#160;&amp;#160; #Produces &amp;quot;/bin/sh&amp;quot;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;strong&gt;The urlparse Module&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Contains functions for manipulating URLs&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;URL’s have the following general format&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;scheme:/netloc/path;parameters?query#fragment&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;urlparse(urlstring) - Parses a URL into components&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;import urlparse&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;t = urlparse.urlparse(&amp;quot;http://www.python.org/index.html&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;#Produces (’http’,’&lt;/font&gt;&lt;a href="http://www.python.org&amp;rsquo;,&amp;rsquo;/index.html"&gt;&lt;font size="2"&gt;www.python.org’,’/index.html&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;’,’’,’’,’’)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;urlunparse(tuple) - Turns tuple of components back into a URL string&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;url = urlparse.urlunparse((’http’,’www.python.org’,’foo.html’, &lt;/font&gt;&lt;font size="2"&gt;’bar=spam’,’’))&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;# Produces &amp;quot;&lt;/font&gt;&lt;a href="http://www.python.org/foo.html?bar=spam"&gt;&lt;font size="2"&gt;http://www.python.org/foo.html?bar=spam&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="2"&gt;urljoin(base, url) - Combines a base and relative URL&lt;/font&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;urlparse.urljoin(&amp;quot;http://www.python.org/index.html&amp;quot;,&amp;quot;help.html&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="2"&gt;# Produces &amp;quot;&lt;/font&gt;&lt;a href="http://www.python.org/help.html"&gt;&lt;font size="2"&gt;http://www.python.org/help.html&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;In this note we explored horizontally the network programming capabilities of Python. Every single module and topic mentioned here, needs multiple posts to cover it. In the upcoming posts, we will dig into network programming in detail.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-552760981910291874?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/LgeN9guC5HU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/LgeN9guC5HU/python-notes-11-introduction-to-network.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/05/python-notes-11-introduction-to-network.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-7354092816140833782</guid><pubDate>Sat, 28 Mar 2009 12:55:00 +0000</pubDate><atom:updated>2009-05-15T09:37:34.743+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Python</category><title>Python Notes – 10 : Threading</title><description>&lt;p&gt;Welcome to our tenth note in the Python learning process. In this note we will talk about threading, threads communication and synchronization.&lt;/p&gt;  &lt;h4&gt;Threads basics&lt;/h4&gt;  &lt;p&gt;A running program is called a &amp;quot;process&amp;quot;. Each process has memory, list of open files, stack, program counter, etc…. Normally, a process executes statements in a single sequence of control-flow. &lt;/p&gt;  &lt;p&gt;The following commands create an entirely new process: fork(),system(), popen(), etc… This child process runs independently of the parent. Has own set of resources. There is minimal sharing of information between parent and child.&lt;/p&gt;  &lt;p&gt;On the other side, a thread is kind of like a process (it’s a sequence of control-flow). Except that it exists entirely inside a process and shares resources. A single process may have multiple threads of execution. This is extremely useful when an application wants to perform many concurrent tasks on shared data.&lt;/p&gt;  &lt;h4&gt;Problems with Threads&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;Scheduling : To execute a threaded program, must rapidly switch between threads. This can be done by the user process (user-level threads) or Can be done by the kernel (kernel-level threads). &lt;/li&gt;    &lt;li&gt;Resource Sharing : Since threads share memory and other resources, you must be very careful. Operation performed in one thread could cause problems in another. &lt;/li&gt;    &lt;li&gt;Synchronization : Threads often need to coordinate actions because they can get &amp;quot;race conditions&amp;quot; (outcome dependent on order of thread execution). You will often need to use locking primitives (mutual exclusion locks, semaphores, etc...) &lt;/li&gt; &lt;/ul&gt;  &lt;h4&gt;Python Threads&lt;/h4&gt;  &lt;p&gt;Python supports threads on the following platforms : Solaris, Windows, systems that support the POSIX threads library (pthreads).&lt;/p&gt;  &lt;p&gt;Thread scheduling is tightly controlled by a global interpreter lock and scheduler. Only a single thread is allowed to be executing in the Python interpreter at once. Thread switching only occurs between the execution of individual byte-codes. Long-running calculations in C/C++ can block execution of all other threads. However, most I/O operations do not block.&lt;/p&gt;  &lt;p&gt;Python threads are somewhat more restrictive than in C. Effectiveness may be limited on multiple CPUs (due to interpreter lock). Threads can interact strangely with other Python modules (especially signal handling). Not all extension modules are thread-safe.&lt;/p&gt;  &lt;h4&gt;The thread module&lt;/h4&gt;  &lt;p&gt;The thread module provides low-level access to threads, thread creation, and Simple mutex locks.&lt;/p&gt;  &lt;h5&gt;Creating a new thread&lt;/h5&gt;  &lt;p&gt;Thread.start_new_thread(func,[args [,kwargs]]) Executes a function in a new thread. Syntax like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import thread&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import time&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def print_time(delay):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; while 1:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; time.sleep(delay)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print time.ctime(time.time())&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;thread.start_new_thread(print_time,(5,))&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;# Start the thread&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;# Go do something else&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;statements&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;…….&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The function print_time will execute in a separate thread and will continue printing the time every 5 seconds. Python will continue executing our statements also.&lt;/p&gt;  &lt;h5&gt;Thread termination&lt;/h5&gt;  &lt;p&gt;Thread silently exits when the function returns. Thread can explicitly exit by calling thread.exit() or sys.exit(). Also uncaught exception causes thread termination (and prints error message). Other threads continue to run even if one had an error.&lt;/p&gt;  &lt;h5&gt;Simple locks&lt;/h5&gt;  &lt;p&gt;allocate_lock(). Creates a lock object, initially unlocked. Only one thread can acquire the lock at once. Threads block indefinitely until lock becomes available.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import thread&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;lk = thread.allocate_lock()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def foo():&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lk.acquire()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Acquire the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; …&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; #critical section&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lk.release()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Release the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You might use this if two or more threads were allowed to update a shared data structure.&lt;/p&gt;  &lt;h5&gt;The main thread&lt;/h5&gt;  &lt;p&gt;When Python starts, it runs as a single thread of execution. This is called the &amp;quot;main thread.&amp;quot; which is on its own and it’s not a big deal. However, if you launch other threads it has some special properties.&lt;/p&gt;  &lt;h5&gt;Termination of the main thread&lt;/h5&gt;  &lt;p&gt;If the main thread exits and other threads are active, the behavior is system dependent. Usually, this immediately terminates the execution of all other threads without cleanup. Cleanup actions of the main thread may be limited as well.&lt;/p&gt;  &lt;h5&gt;Signal handling&lt;/h5&gt;  &lt;p&gt;Signals can only be caught and handled by the main thread of execution. Otherwise you will get an error (in the signal module). The keyboard-interrupt can be caught by any thread (non-deterministically).&lt;/p&gt;  &lt;h4&gt;The threading module&lt;/h4&gt;  &lt;p&gt;The threading module is a high-level threads module that implements threads as classes (similar to Java). It provides an assortment of synchronization and locking primitives. It is built using the low-level thread module.&lt;/p&gt;  &lt;h5&gt;Creating a new thread (as a class)&lt;/h5&gt;  &lt;p&gt;When defining threads as classes all you need to supply is the following:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A constructor that calls threading.Thread.__init__(self) &lt;/li&gt;    &lt;li&gt;A run() method that performs the actual work of the thread. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;A few additional methods are also available&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#0000ff"&gt;t.join([timeout])&amp;#160;&amp;#160;&amp;#160; # Wait for thread t to terminate&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;t.getName()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Get the name of the thread&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;t.setName(name)&amp;#160;&amp;#160; # Set the name of the thread&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;t.isAlive()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Return 1 if thread is alive.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;t.isDaemon()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Return daemonic flag&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;t.setDaemon(val)&amp;#160;&amp;#160; # Set daemonic flag&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Example: Inherit from the &amp;quot;Thread&amp;quot; class, provide required methods, and utilize the available methods.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import threading, time&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class PrintTime(threading.Thread):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def __init__(self,interval):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; threading.Thread.__init__(self)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Required&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; self.interval = interval&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def run(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; while 1:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; time.sleep(self.interval)&amp;#160; &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; print time.ctime(time.time())&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;t = PrintTime(5)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Create a thread object&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;t.start()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Start it&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;Daemon threads&lt;/h5&gt;  &lt;p&gt;Normally, interpreter exits only when all threads have terminated. However, a thread can be flagged as a daemon thread (runs in background). Interpreter really only exits when all non-daemonic threads exit. You can use this to launch threads that run forever, but which can be safely killed.&lt;/p&gt;  &lt;h4&gt;Threads synchronization&lt;/h4&gt;  &lt;p&gt;The threading module provides the following synchronization primitives:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Mutual exclusion locks &lt;/li&gt;    &lt;li&gt;Reentrant locks &lt;/li&gt;    &lt;li&gt;Conditional variables &lt;/li&gt;    &lt;li&gt;Semaphores &lt;/li&gt;    &lt;li&gt;Events &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;When would you need these threads synchronization mechanisms ? &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;When threads are updating shared data structures. &lt;/li&gt;    &lt;li&gt;When threads need to coordinate their actions in some manner (events). &lt;/li&gt; &lt;/ul&gt;  &lt;h5&gt;The Lock object&lt;/h5&gt;  &lt;p&gt;Provides a simple mutual exclusion lock. Only one thread is allowed to acquire the lock at once. Most useful for coordinating access to shared data.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import threading&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;data = [ ]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Some data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;lck = threading.Lock()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Create a lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def put_obj(obj):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.acquire()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; data.append(obj)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.release()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def get_obj():&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.acquire()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; r = data.pop()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.release()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; return r&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;The RLock object&lt;/h5&gt;  &lt;p&gt;A mutual-exclusion lock that allows repeated acquisition by the same thread. Allows nested acquire(), release() operations in the thread that owns the lock. Only the outermost release() operation actually releases the lock.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import threading&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;data = [ ]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Some data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;lck = threading.Lock()&amp;#160; # Create a lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def put_obj(obj):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.acquire()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; data.append(obj)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; ...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; put_obj(otherobj)&amp;#160;&amp;#160; # Some kind of recursion&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; ...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.release()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def get_obj():&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.acquire()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; r = data.pop()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; lck.release()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; return r&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;The Condition object&lt;/h5&gt;  &lt;p&gt;Creates a condition variable. Synchronization primitive typically used when a thread is interested in an event or state change. Could help in the producer-consumer classic problem.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;data = []&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Create data queue and a condition variable&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;cv = threading.Condition()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;# Consumer thread&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def consume_item():&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; cv.acquire()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Acquire the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; while not len(data):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cv.wait()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Wait for data to show up&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; r = data.pop()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; cv.release()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Release the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; return r&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;# Producer thread&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def produce_item(obj):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; cv.acquire()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Acquire the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; data.append(obj)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; cv.notify()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Notify a consumer&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; cv.release()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Release the lock&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;Semaphores&lt;/h5&gt;  &lt;p&gt;A locking primitive based on a counter. Each acquire() method decrements the counter. Each release() method increments the counter. If the counter reaches zero, future acquire() methods block. Common use: limiting the number of threads allowed to execute code&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;sem = threading.Semaphore(5)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # No more than 5 threads allowed&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def fetch_file(host,filename):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; sem.acquire()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Decrements count or blocks if zero&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; ...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; sem.release()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Increment count&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;Events&lt;/h5&gt;  &lt;p&gt;A communication primitive for coordinating threads. One thread signals an &amp;quot;event&amp;quot; while other threads wait for it to happen.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;e = Event()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;# Create an event object&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def signal_event():&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;# Signal the event&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.set()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def wait_for_event():&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Wait for event&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; e.wait()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def clear_event():&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;# Clear event&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; e.clear()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Event is similar to a condition variable, but all threads waiting for event are awakened.&lt;/p&gt;  &lt;h5&gt;Locks and Blocking&lt;/h5&gt;  &lt;p&gt;By default, all locking primitives block until lock is acquired. In general, this is uninterruptible. Fortunately, most primitives provide a non-blocking option&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;if not lck.acquire(0):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # lock couldn’t be acquired!&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This works for Lock, RLock, and Semaphore objects. On the other hand condition variables and events provide a timeout option&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;cv = Condition()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;...&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;cv.wait(60.0)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Wait 60 seconds for notification&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;On timeout, the function simply returns. Up to caller to detect errors.&lt;/p&gt;  &lt;h4&gt;The Queue Module&lt;/h4&gt;  &lt;p&gt;Provides a multi-producer, multi-consumer FIFO queue object. It can be used to safely exchange data between multiple threads.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#0000ff"&gt;q = Queue(maxsize)&amp;#160;&amp;#160;&amp;#160; # Create a queue.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.qsize()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Return current size.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.empty()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Test if empty.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.full()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Test if full.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.put(item)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Put an item on the queue.&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.get()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Get item from queue&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The Queue object also supports non-blocking put/get.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font color="#0000ff"&gt;q.put_nowait(item).&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font color="#0000ff"&gt;q.get_nowait()&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These raise the Queue.Full or Queue.Empty exceptions if an error occurs. Return values for qsize(), empty(), and full() are approximate.&lt;/p&gt;  &lt;h4&gt;Things to consider when using threads&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;Global interpreter lock makes it difficult to fully utilize multiple CPUs. &lt;/li&gt;    &lt;li&gt;You don’t get the degree of parallelism you might expect. &lt;/li&gt;    &lt;li&gt;Not all modules are thread-friendly. Example: gethostbyname() blocks all threads if nameserver down. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In this note we will talked about threading, threads communication and synchronization. In the upcoming notes, we will talk about more advanced topics in Python programming.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-7354092816140833782?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/bD3rleUE36M" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/bD3rleUE36M/python-notes-10.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/03/python-notes-10.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-4108398750179200674</guid><pubDate>Fri, 27 Mar 2009 06:53:00 +0000</pubDate><atom:updated>2009-05-15T09:50:04.566+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Python</category><title>Python Notes – 9 : Serialization</title><description>&lt;p&gt;Welcome to our ninth note in our Python learning process. We talked previously about files and how to handle it but we talked about writing and reading only the primitive data types as integers and strings. We also talked about objects and classes. Now, what if we want to write a compound data type or a complex object to a file. This note will talk about writing objects to files, which is called object serialization.&lt;/p&gt;  &lt;h4&gt;pickle&lt;/h4&gt;  &lt;p&gt;The pickle module is a Python built-in module that object serialization and de-serialization. To store a data structure, use the dump method and then close the file in the usual way:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; pickle.dump(12.3, f)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; pickle.dump([1,2,3], f)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; f.close()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Then we can open the file for reading and load the data structures we dumped:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; f = open(&amp;quot;test.pck&amp;quot;,&amp;quot;r&amp;quot;)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; x = pickle.load(f)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;12.3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; type(x)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;lt;type 'float'&amp;gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; y = pickle.load(f)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;[1, 2, 3]&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; type(y)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;lt;type 'list'&amp;gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Each time we invoke load, we get a single value from the file, complete with its original type.&lt;/p&gt;  &lt;h4&gt;What can be serialized and de-serialized&lt;/h4&gt;  &lt;p&gt;The following types can be serialized and de-serialized using pickle:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;None, True, and False &lt;/li&gt;    &lt;li&gt;integers, long integers, floating point numbers, complex numbers &lt;/li&gt;    &lt;li&gt;normal and Unicode strings &lt;/li&gt;    &lt;li&gt;tuples, lists, sets, and dictionaries containing only picklable objects &lt;/li&gt;    &lt;li&gt;functions defined at the top level of a module &lt;/li&gt;    &lt;li&gt;built-in functions defined at the top level of a module &lt;/li&gt;    &lt;li&gt;classes that are defined at the top level of a module &lt;/li&gt;    &lt;li&gt;instances of such classes whose__dict__ or __setstate__() is picklable &lt;/li&gt; &lt;/ul&gt;  &lt;h4&gt;Things to consider when using pickle&lt;/h4&gt;  &lt;ul&gt;   &lt;li&gt;Attempts to pickle unpicklable objects will raise the picklingError exception; when this happens, an unspecified number of bytes may have already been written to the underlying file. &lt;/li&gt;    &lt;li&gt;Trying to pickle a highly recursive data structure may exceed the maximum recursion depth, a RuntimeError will be raised in this case. You can carefully raise this limit with sys.setrecursionlimit(). &lt;/li&gt; &lt;/ul&gt;  &lt;h4&gt;cPickle&lt;/h4&gt;  &lt;p&gt;The cPickle is an optimized version of pickle written in C, so it can be up to 1000 faster than pickle.&lt;/p&gt;  &lt;h4&gt;marshal&lt;/h4&gt;  &lt;p&gt;The marshal module can also be used for serialization. Marshal is similar to pickle, but is intended only for simple objects. Can’t handle recursion or class instances. On the plus side, it’s pretty fast if you just want to save simple objects to a file. Data is stored in a binary architecture independent format.To serialize:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import marshal&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;marshal.dump(obj,file)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Write obj to file&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To unserialize:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;obj = marshal.load(file)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;shelve&lt;/h4&gt;  &lt;p&gt;The shelve module provides a persistent dictionary. It is works like a dictionary, but data is stored on disk. &lt;/p&gt;  &lt;p&gt;Keys must be strings. Data can be any object serializable with pickle.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;import shelve&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d = shelve.open(&amp;quot;data&amp;quot;) # Open a ’shelf’&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d[’foo’] = 42 # Save data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;x = d[’bar’] # Retrieve data&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Shelf operations&lt;/h4&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d[key] = obj&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Store an object&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;obj = d[key]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Retrieve an object&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;del d[key]&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Delete an object&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d.has_key(key)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Test for existence of key&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d.keys()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Return a list of all keys&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;d.close()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; # Close the shelf&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In this note will talked about writing objects to files, which is called object serialization. Object serialization is very useful in persisting your application logic to resume its execution later, transfer of execution to remote machine, and many other applications scenarios.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-4108398750179200674?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/N32nJYmFHRY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/N32nJYmFHRY/python-notes-9.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/03/python-notes-9.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-59384554657271185.post-4961343029819958668</guid><pubDate>Fri, 27 Mar 2009 06:51:00 +0000</pubDate><atom:updated>2009-05-15T09:55:26.784+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Python</category><title>Python Notes – 8 : Object-Oriented Basics</title><description>&lt;p&gt;Welcome to our eighth note in our Python learning process. This note will talk about object oriented features in Python.&lt;/p&gt;  &lt;h4&gt;Classes and Objects&lt;/h4&gt;  &lt;p&gt;A class definition looks like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class Point:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; pass&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Class definitions can appear anywhere in a program, but they are usually near the beginning (after the import statements). By creating the Point class, we created a new type, also called Point. The members of this type are called instances of the type or objects. Creating a new instance is called instantiation. To instantiate a Point object, we call a function named Point:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; blank = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The variable blank is assigned a reference to a new Point object. A function like Point that creates new objects is called a constructor. If you tried to get the type of blank, you got instance:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; type(blank)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;lt;type 'instance'&amp;gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you tried to print blank:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; print blank&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;lt;__main__.point instance at 0x01922AF8&amp;gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The result indicates that blank is an instance of the Point class and it was defined in __main__ . 0x01922AF8 is the unique identifier for this object, written in hexadecimal (base 16).&lt;/p&gt;  &lt;h4&gt;Attributes&lt;/h4&gt;  &lt;p&gt;We can add new data to an instance using dot notation:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; blank.x = 3.0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; blank.y = 4.0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;These new data items called attributes.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; print blank.y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;4.0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; x = blank.x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; print x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;3.0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Sameness&lt;/h4&gt;  &lt;p&gt;To find out if two references refer to the same object, use the == operator. For example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.x = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.y = 4&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2 = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2.x = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2.y = 4&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 == p2&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Even though p1 and p2 contain the same coordinates, they are not the same object. If we assign p1 to p2, then the two variables are aliases of the same object:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2 = p1&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 == p2&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;1&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This type of equality is called &lt;em&gt;shallow equality&lt;/em&gt; because it compares only the references, not the contents of the objects. To compare the contents of the objects - &lt;em&gt;deep equality&lt;/em&gt; - we can write our own function to do that, like that:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def samePoint(p1, p2) :&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; return (p1.x == p2.x) and (p1.y == p2.y)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now if we create two different objects that contain the same data, we can use samePoint to find out if they represent the same point.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.x = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.y = 4&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2 = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2.x = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2.y = 4&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; samePoint(p1, p2)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;1&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Copying&lt;/h4&gt;  &lt;p&gt;Aliasing can make a program difficult to read because changes made in one place might have unexpected effects in another place. Copying an object is often an alternative to aliasing. The copy module contains a function called copy that can duplicate any object:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; import copy&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 = Point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.x = 3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1.y = 4&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2 = copy.copy(p1)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 == p2&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; samePoint(p1, p2)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;1&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Copy works fine for objects that doesn't contain any embedded objects. If the object contains references to other objects, Copy will copy the embedded references to the destination. This ends up that the both copies reference the same internal objects. &lt;/p&gt;  &lt;p&gt;You can use deepcopy which copies not only the object but also any embedded objects. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; b2 = copy.deepcopy(b1)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now b1 and b2 are completely separate objects.&lt;/p&gt;  &lt;h4&gt;The initialization method&lt;/h4&gt;  &lt;p&gt;The initialization method is a special method that is invoked when an object is created. The name of this method is __init__.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class point:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def __init__(self, x = 0, y = 0):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Self.x = x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Slef.y = y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;When we invoke the point constructor, the arguments we provide are passed along to init:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; first = point(5,7)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; first.x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;5&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; first.y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;7&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Because the parameters are optional, we can omit them:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; second = point()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; second.x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; second.y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;We can also provide a subset of the parameters by naming them explicitly:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; third = point(y=10)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; third.x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;0&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; third.y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;10&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;The __str__ method&lt;/h4&gt;  &lt;p&gt;The __str__ method of any class is called by the Python in any operation that requires the class instance to be converted to string. Operations like that are print. Syntax like that:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class xyz:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def __str__(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return &amp;quot;Our class xyz&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; a = xyz()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; a&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;lt;__main__.xyz instance at 0x02627300&amp;gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; print y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;Our class xyz&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Instances as parameters&lt;/h4&gt;  &lt;p&gt;You can pass an instance as a parameter in the usual way. For example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def printPoint(p):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print '(' + str(p.x) + ', ' + str(p.y) + ')'&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Instances as return values&lt;/h4&gt;  &lt;p&gt;Functions can return instances. For example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;def sumPoints(A,B)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; Z = Point ()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; Z.x = A.x + B.x&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; Z.y = A.y + B.y&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; return Z&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h4&gt;Operator overloading&lt;/h4&gt;  &lt;p&gt;Operator overloading means changing the definition and behavior of the built-in operators when they are applied to user-defined types. For example, to override the addition operator + , we provide a method named __add__ in our point class :&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class Point:&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def __add__(self, other):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return Point(self.x + other.x, self.y + other.y)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;the first parameter is the object on which the method is invoked. The second parameter is conveniently named other to distinguish it from self. Now, when we apply the + operator to Point objects, Python invokes add :&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p1 = Point(3, 4)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p2 = Point(5, 7)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; p3 = p1 + p2&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; print p3&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;(8, 11)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The expression p1 + p2 is equivalent to p1. add (p2), but obviously more elegant. You can change the behavior of many operators through overloading their respective functions, which are available at &lt;a href="http://www.python.org/doc/2.2/ref/numeric-types.html"&gt;http://www.python.org/doc/2.2/ref/numeric-types.html&lt;/a&gt;&lt;/p&gt;  &lt;h4&gt;Inheritance&lt;/h4&gt;  &lt;p&gt;Inheritance is the ability to define a new class that is a modified version of an existing class. The new class inherits all of the methods of the existing class. The new class may be called child class or subclass. The syntax is like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class class1(object):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; K = 7&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def __init__(self, color='green'):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Self.color = color&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def Hello1(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Print &amp;quot;Hello from class1&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def printColor(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;preferred &amp;quot;, self.color&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;class class2(class1):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; def Hello2(self):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Hello from class2&amp;quot;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print self.k&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Here class2 is the child of class1. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c1 = class1('blue')&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c2 = class2('red')&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c1.Hello1()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;Hello from class1&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c2.Hello2()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;Hello from class2&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;7&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Child class can access parent class methods&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c2.Hello1() &lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;Hello from class1&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;The parent constructor called automatically for Childs, as following:&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c1.printColor()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;preferred blue&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;gt;&amp;gt;&amp;gt; c2.printColor()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;preferred red&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You can check for class methods, attributes using hasattr method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;if hasattr(class1, &amp;quot;Hello2&amp;quot;):&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print c1.Hello2()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;else:&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; print &amp;quot;Class1 does not contain method Hello2()&amp;quot;&lt;/font&gt;&lt;/p&gt;    &lt;p&gt;&lt;font color="#0000ff"&gt;Class1 does not contain method Hello2()&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;To check the inheritance relation between two class :&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;if issubclass(class2, class1)&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Print &amp;quot;Class2 is a subclass of Class1”&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In this note we tried to cover as much as we can of the Python object oriented features. We give it a more advanced note in the future.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/59384554657271185-4961343029819958668?l=ebeid-soliman.blogspot.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/ebeid-soliman/~4/TccvaM-fOxY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/ebeid-soliman/~3/TccvaM-fOxY/python-notes-8.html</link><author>noreply@blogger.com (Ebeid Soliman)</author><thr:total>0</thr:total><feedburner:origLink>http://ebeid-soliman.blogspot.com/2009/03/python-notes-8.html</feedburner:origLink></item></channel></rss>

