<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Alkampfer's Place</title>
	
	<link>http://www.nablasoft.com/alkampfer</link>
	<description>Blog about programming and .NET</description>
	<pubDate>Wed, 19 Nov 2008 09:32:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<geo:lat>43.44754459758882</geo:lat><geo:long>12.84752368927002</geo:long><image><url>http://www.feedburner.com/fb/images/pub/fb_pwrd.gif</url></image><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/AlkampferEng" type="application/rss+xml" /><feedburner:emailServiceId>1380532</feedburner:emailServiceId><feedburner:feedburnerHostname>http://www.feedburner.com</feedburner:feedburnerHostname><item>
		<title>Dynamic Linq expression generator</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/458204188/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/19/dynamic-linq-expression-generator/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 09:32:17 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/19/dynamic-linq-expression-generator/</guid>
		<description><![CDATA[Some time ago I read this post that speaks about dynamic query library. I downloaded the code sample, it is interesting, but is bound to Linq to Sql, now I need an implementation of a Dynamic Linq Query generator. Since I already worked in the past with expression parser and I have some generic Infix [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago I read th<a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx">is post</a> that speaks about dynamic query library. I downloaded the code sample, it is interesting, but is bound to Linq to Sql, now I need an implementation of a Dynamic Linq Query generator. Since I already worked in the past with expression parser and I have some generic Infix to postfix converter in a three hours of work I was able to put everything in a project and now I can write</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:86b428f9-16ad-4daf-a707-61093dc5eddd" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> f </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Name == 'Gian Maria'</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
Assert.That(f(aCustomer), Is.True);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This example shows that I can build a simple function that accepts an entity (Customer) and a string representing some conditions, then I’m able to dynamically parse the string, create an expression tree and call Compile on it to obtain a function that I can use in code. As soon as possible I’ll show a complete example if you are interested in.</p>
<p>alk.</p>
</p>
</p>
<p>Tags: <a href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a> <a href="http://technorati.com/tag/Dynamic Expression Generation" rel="tag">Dynamic Expression Generation</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=5HinN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=5HinN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=oRBFN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=oRBFN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=voLgn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=voLgn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=Pj95N"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=Pj95N" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=j9tmN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=j9tmN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/458204188" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/19/dynamic-linq-expression-generator/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F19%2Fdynamic-linq-expression-generator%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/19/dynamic-linq-expression-generator/</feedburner:origLink></item>
		<item>
		<title>Missing RemovedItem from BindingList and BindingSource</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/457086927/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/18/missing-removeditem-from-bindinglist-and-bindingsource/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 11:35:51 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[.NET framework]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/18/missing-removeditem-from-bindinglist-and-bindingsource/</guid>
		<description><![CDATA[If you work with Windows Forms you cannot live without binding :D, but something is missing from the base structure. I want to create a simple form with a BindingSource, then bind a list of object to a grid and some textboxes to create a master detail form, moreover I set up a simple navigator [...]]]></description>
			<content:encoded><![CDATA[<p>If you work with Windows Forms you cannot live without binding :D, but something is missing from the base structure. I want to create a simple form with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx">BindingSource</a>, then bind a list of object to a grid and some textboxes to create a master detail form, moreover I set up a simple navigator to make simple for the user navigating the structure, the navigator has the addnew and remove button and the user likes them. </p>
<p>The user can Add and Remove element from the navigator, or directly from a DataGridView, and I simply need to call the appropriate method of Repository to save and delete objects. This structure is really clean, but you encounter a problem: nor the BindingSource nor the <a href="http://msdn.microsoft.com/en-us/library/ms132679.aspx">BindingList&lt;T&gt;</a> raises a RemovingItem event. I do not know why these classes are designed in this way, you have an <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.addingnew.aspx">AddingNew</a> Event in the Binding Source and BindingList&lt;T&gt; but no corresponding RemovingItem. You have a ListChanged event that tells you when an element is removed, but the element is already gone from the list, so you cannot use to detect object removal.</p>
<p>If you look at the events of <a href="http://msdn.microsoft.com/en-us/library/ms132740.aspx">BindingList&lt;T&gt;</a> you can notice that there is no way to be informed of the exact element that gets removed. A simple solution is to inherit from the BindingList&lt;T&gt; </p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:732e3139-1989-47db-acb4-42ec08593c2f" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> RemoveItemEventArgs : EventArgs
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Object RemovedItem
    {
        </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> removedItem;}
    }
    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> Object removedItem;

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> RemoveItemEventArgs(</span><span style="color: #0000FF;">object</span><span style="color: #000000;"> removedItem)
    {
        </span><span style="color: #0000FF;">this</span><span style="color: #000000;">.removedItem </span><span style="color: #000000;">=</span><span style="color: #000000;"> removedItem;
    }
}

</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> MyBindingList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> : BindingList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">event</span><span style="color: #000000;"> EventHandler</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">RemoveItemEventArgs</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> RemovingItem;
    </span><span style="color: #0000FF;">protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">virtual</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> OnRemovingItem(RemoveItemEventArgs args)
    {
        EventHandler</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">RemoveItemEventArgs</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> RemovingItem;
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (temp </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
        {
            temp(</span><span style="color: #0000FF;">this</span><span style="color: #000000;">, args);
        }
    }

    </span><span style="color: #0000FF;">protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">override</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> RemoveItem(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> index)
    {
        OnRemovingItem(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> RemoveItemEventArgs(</span><span style="color: #0000FF;">this</span><span style="color: #000000;">[index]));
        </span><span style="color: #0000FF;">base</span><span style="color: #000000;">.RemoveItem(index);
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> MyBindingList(IList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> list) : </span><span style="color: #0000FF;">base</span><span style="color: #000000;">(list)
    {
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> MyBindingList()
    {
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As you can see implementing such a functionality is a breeze, you should simply override the RemoveItem method and raise a custom RemovingItem event. Now I can use this new BindingList in this way.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:d967c371-c88e-4d6e-8dc9-6a29484f5292" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">MyBindingList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">UIDomainProxy</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> source </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> MyBindingList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">UIDomainProxy</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(
    Repository</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Domain</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">.GetAll().Select(d </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> UIDomainProxy(d)).ToList());
source.AddingNew </span><span style="color: #000000;">+=</span><span style="color: #000000;">source_AddingNew;
source.RemovingItem </span><span style="color: #000000;">+=</span><span style="color: #000000;">source_RemovingItem;
domainBindingSource.DataSource </span><span style="color: #000000;">=</span><span style="color: #000000;"> source;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
</p>
<p>I gets all the element from the repository, then I wrap then in a UIProxy (I’ll explain this in a future post), then I intercept the addingNew and RemovingItem events simply calling Save and Delete methods from the repository. Finally I set the BindingList as the source of the BindingSource created by the designer.</p>
<p>I wonder why this event is missing from the basic BindingList&lt;T&gt; implementation.</p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/BindingList&lt;T&gt;" rel="tag">&quot; rel=tag&gt;BindingList<t></a> <a href="http://technorati.com/tag/.NET" rel="tag">.NET</a> <a href="http://technorati.com/tag/Binding" rel="tag">Binding</a> <a href="http://technorati.com/tag/Windows Forms" rel="tag">Windows Forms</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=lhgKN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=lhgKN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=VnBsN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=VnBsN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=5Msvn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=5Msvn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=HjfvN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=HjfvN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=BA6XN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=BA6XN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/457086927" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/18/missing-removeditem-from-bindinglist-and-bindingsource/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F18%2Fmissing-removeditem-from-bindinglist-and-bindingsource%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/18/missing-removeditem-from-bindinglist-and-bindingsource/</feedburner:origLink></item>
		<item>
		<title>Validate HTML input with Linq2XML</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/456227988/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/17/validate-html-input-with-linq2xml/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 17:32:58 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/?p=522</guid>
		<description><![CDATA[Suppose you have a very simple page where user can add comments to an issue, user can enter plain text and also they can use th HTML tag &#60;b&#62; to render in bold some text. In the example code you can see a very simple implementation (default.aspx). It use a xml file for back end [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have a very simple page where user can add comments to an issue, user can enter plain text and also they can use th HTML tag &lt;b&gt; to render in bold some text. In the <a href="http://www.nablasoft.com/alkampfer/storage/validatesample.zip">example code</a> you can see a very simple implementation (default.aspx). It use a xml file for back end storage (so you can run the example without a database) and in Default.aspx all the text that was entered by the user was stored in a CData section of the XML STorage file. When the comments are rendered on the page we simply output all the content. The result is good but have some problems. First of all you can use every html tag, such as &lt;i&gt; moreover, if some hacker enter the text “you were &lt;script&gt;alert(&#8217;hacked&#8217;);&lt;/script&gt;” into the textbox, all the user that read the page will execute that script, this is a&#160; simple sample of cross site scripting attack.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image8.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="216" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb8.png" width="450" border="0" /></a> </p>
<p>Here is the content of the storage file</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e12f39c0-ff0c-4b5c-acc5-f447d3baa00e" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;?</span><span style="color: #FF00FF;">xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</span><span style="color: #0000FF;">?&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comments</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">Hacker</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #0000FF;">&lt;![CDATA[</span><span style="color: #808080;">you where &lt;script&gt;alert('hacked');&lt;/script&gt;</span><span style="color: #0000FF;">]]&gt;</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comments</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
</p>
<p>In Default2.Aspx there is a simple solution to mitigate this problem, trying to remove all node that are not &lt;b&gt;, but with this solution if a user enter “this is a &lt;b&gt;good comment&lt;/b&gt; with &lt;i&gt;italic text&lt;/i&gt;“, we have a big problem, the part in the &lt;i&gt;&lt;/i&gt; tag gets completely removed. Moreover if some hacker gets a way to change your storage file, or can insert some data in database you still have problem. A better solution was showed in Default3.aspx. Since I consider data in the file as <em>untrusted input</em> because it is under direct control of the user I need to sanitize the comment before I can render text to the user. The goal is having a SanitizeComment function that gets a html fragment as input and return a new fragment with all tags removed, except those that are permitted.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:75c5869e-9bc0-4fc1-9ec0-9a9c6b2e7df2" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #008080;"> 1</span> <span style="color: #0000FF;">private</span><span style="color: #000000;"> String SanitizeComment(String commentText)
</span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{
</span><span style="color: #008080;"> 3</span> <span style="color: #000000;">  </span><span style="color: #0000FF;">try</span><span style="color: #000000;">
</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">  {
</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">      XElement doc </span><span style="color: #000000;">=</span><span style="color: #000000;"> XElement.Parse(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">&lt;span&gt;</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> commentText </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">&lt;/span&gt;</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">      doc.Descendants().Where(elem </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> elem.Name </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">b</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)
</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">         .ToList().ForEach(elem </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">         {
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">             elem.AddAfterSelf(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> XText((String)elem));
</span><span style="color: #008080;">10</span> <span style="color: #000000;">             elem.Remove();
</span><span style="color: #008080;">11</span> <span style="color: #000000;">         });
</span><span style="color: #008080;">12</span> <span style="color: #000000;">
</span><span style="color: #008080;">13</span> <span style="color: #000000;">      String retvalue </span><span style="color: #000000;">=</span><span style="color: #000000;"> doc.ToString();
</span><span style="color: #008080;">14</span> <span style="color: #000000;">      </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> retvalue;
</span><span style="color: #008080;">15</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">16</span> <span style="color: #000000;">  </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (System.Xml.XmlException)
</span><span style="color: #008080;">17</span> <span style="color: #000000;">  {
</span><span style="color: #008080;">18</span> <span style="color: #000000;">      </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> AntiXss.HtmlEncode(commentText);
</span><span style="color: #008080;">19</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">20</span> <span style="color: #000000;">}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
</p>
</p>
<p>This code use Linq2Xml; first of all in line 5 I create a XElement with a concatenation of a &lt;span&gt; tag and the original content of the comment. Then I select in line 6 all the XML nodes that have a name different from “b”,(the only permitted tag in the output). Then for each of the unpermitted nodes I simply add a new XML node of type XText after the node, and then remove the original node.</p>
<p>If some XMLException occurs, it means that the input is not a well formed XML fragment, so I default to use the AntiXss HtmlEncode function that avoid any cross site scripting risk.</p>
<p>Here is a result of the page when the storage file contains this comments.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3dbb3f50-e17e-4b52-a677-3394aa9571a5" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;?</span><span style="color: #FF00FF;">xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</span><span style="color: #0000FF;">?&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comments</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">Hacker</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #0000FF;">&lt;![CDATA[</span><span style="color: #808080;">you where &lt;script&gt;alert('hacked');&lt;/script&gt;</span><span style="color: #0000FF;">]]&gt;</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;&lt;/</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #0000FF;">&lt;![CDATA[</span><span style="color: #808080;">this is a &lt;b&gt;good comment&lt;/b&gt;</span><span style="color: #0000FF;">]]&gt;</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;&lt;/</span><span style="color: #800000;">Author</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #0000FF;">&lt;![CDATA[</span><span style="color: #808080;">this is a &lt;b&gt;good comment&lt;/b&gt; with &lt;i&gt;italic text&lt;/i&gt; and &lt;b&gt;again Bold&lt;i&gt; italicbold&lt;/i&gt;&lt;/b&gt;</span><span style="color: #0000FF;">]]&gt;</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">CommentText</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comment</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">Comments</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>You can see that the content of the file contains dangerous html code, but here is what is rendered by Default3.aspx that calls SanitizeComment function.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image9.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="223" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb9.png" width="546" border="0" /></a> </p>
<p>As you can notice the alert(‘hacked’) was shown without the &lt;script&gt; tag, moreover all the text between &lt;i&gt; and &lt;/i&gt; gets no removed. SanitizeComment function leaves only the tag &lt;b&gt; and remove all other unwanted tags.</p>
<p><a href="http://www.nablasoft.com/alkampfer/storage/validatesample.zip">sample code here.</a></p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/Linq2XML" rel="tag">Linq2XML</a> <a href="http://technorati.com/tag/Security" rel="tag">Security</a> <a href="http://technorati.com/tag/Validation" rel="tag">Validation</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=vu1VN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=vu1VN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=WSOzN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=WSOzN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=Hyjtn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=Hyjtn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=Jm5KN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=Jm5KN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=v5SfN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=v5SfN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/456227988" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/17/validate-html-input-with-linq2xml/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F17%2Fvalidate-html-input-with-linq2xml%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/17/validate-html-input-with-linq2xml/</feedburner:origLink></item>
		<item>
		<title>Three levels master detail with WPF</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/446421268/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 11:18:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/</guid>
		<description><![CDATA[Binding in WPF is really more powerful respect its counterpart in windows forms. Suppose you have to show this hierarchy of classes in an interface.

 
This is a simple structure where a LogGroup have a collection of LogMessages and each LogMessage has a collection of StackSteps. I have a collection of LogGroup and I need [...]]]></description>
			<content:encoded><![CDATA[<p>Binding in WPF is really more powerful respect its counterpart in windows forms. Suppose you have to show this hierarchy of classes in an interface.</p>
</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image6.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb6.png" width="673" border="0" /></a> </p>
<p>This is a simple structure where a LogGroup have a collection of LogMessages and each LogMessage has a collection of StackSteps. I have a collection of LogGroup and I need to create a <em>three level master detail</em> interface to make the user browse through all objects in hierarchy.</p>
<p>Thanks to powerful WPF binding you can create such an interface without procedural code. I used three ListView enclosed in a StackPanel. The first peculiarity of WPF binding is that you can set an ObservableCollection&lt;LogGroup&gt; object into the BindingContext on the StackPanel that contains the three listviews . The first listview have this declaration.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:bf0ea2a2-d4b5-486a-8d0e-e5fafb1bf011" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;">
         IsSynchronizedWithCurrentItem</span><span style="color: #0000FF;">=&quot;True&quot;</span><span style="color: #FF0000;">
         HorizontalContentAlignment</span><span style="color: #0000FF;">=&quot;Stretch&quot;</span><span style="color: #FF0000;">
         ScrollViewer.HorizontalScrollBarVisibility</span><span style="color: #0000FF;">=&quot;Disabled&quot;</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The IsSynchronizedWithCurrentItem is true to enable automatic synchronization, the binding is set with the instruction<strong> {Binding},</strong> that means “create a binding object with default properties”. A default binding walks controls stack and bind to the first Datacontext property that is not null, in our situation the ObservableCollection&lt;LogGroup&gt; set in the StackPanel that contains the ListView.</p>
<p>Now the second ListView should bind to the property <em>Messages </em>of the item currently selected in the previous ListView, the solution is really simple</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:02134a54-99b5-44b1-9274-ed20ebf02445" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=Messages}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;">
     HorizontalContentAlignment</span><span style="color: #0000FF;">=&quot;Stretch&quot;</span><span style="color: #FF0000;">
     ScrollViewer.HorizontalScrollBarVisibility</span><span style="color: #0000FF;">=&quot;Disabled&quot;</span><span style="color: #FF0000;">
     IsSynchronizedWithCurrentItem</span><span style="color: #0000FF;">=&quot;true&quot;</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The binding is <strong>{Binding Path=Messages}</strong> that means, “build a Binding object and set the Path Property to the value Messages”, since the IsSynchronizedWithCurrentItem is true you get this master detail binding for free. Thus when you change selection of the first ListView the second ListView updates accordingly and shows the element of the Messages collection of selected LogGroup. To bind the third and last ListView I have to use a special syntax of WPF Path binding</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b88f0fbf-2e91-4734-9ceb-709da98265da" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=Messages/StackSteps}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;">
     HorizontalContentAlignment</span><span style="color: #0000FF;">=&quot;Stretch&quot;</span><span style="color: #FF0000;">
     ScrollViewer.HorizontalScrollBarVisibility</span><span style="color: #0000FF;">=&quot;Disabled&quot;</span><span style="color: #FF0000;">
     IsSynchronizedWithCurrentItem</span><span style="color: #0000FF;">=&quot;true&quot;</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>the binding now is <strong>{Binding Path=Messages/StackSteps}</strong>. If you wonder why I had not written the path “Messages.StackSteps” take a look back at the object model. Messages is a collection, <em>if you write Messages.StackSteps</em> WPF binding engine throws and errors. <strong>Now you need to be aware that every exception that is thrown during binding operation is swallowed by the binding engine, and you should look into the debug windows of visual studio to see binding errors</strong>. In fact you can find this error </p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
</p>
<p>System.Windows.Data Error: 39 : BindingExpression path error: &#8216;StackSteps&#8217; property not found on &#8216;object&#8217; &#8221;List`1&#8242; (HashCode=58377472)&#8217;. BindingExpression:Path=Messages.StackSteps; DataItem=&#8217;ObservableCollection`1&#8242; (HashCode=7457061); target element is &#8216;ListView&#8217; (Name=&#8217;GroupsView&#8217;); target property is &#8216;ItemsSource&#8217; (type &#8216;IEnumerable&#8217;)</p>
<p>the binding engine told you that he could not find a StackSteps Property in the object List&lt;Messages&gt;. In such a situation the WPF binding engine permits you to use slash “/” character to indicate “The current selected item in the view” (<a href="http://msdn.microsoft.com/en-us/library/ms752347.aspx">read here for more details</a>).&#160; Thus writing the path “Messages/StackSteps” told the engine to take selected LogGroup from the first level, then take the Messages property, then the slash indicates to take the current selected element of Messages collection and finally bind to the StackSteps collection of that element. </p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image7.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="328" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb7.png" width="331" border="0" /></a> </p>
<p>Whoa, you get three level master detail with not a single line of procedural code <img src='http://www.nablasoft.com/alkampfer/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/WPF" rel="tag">WPF</a> <a href="http://technorati.com/tag/Master Detail Binding" rel="tag">Master Detail Binding</a> <a href="http://technorati.com/tag/Binding" rel="tag">Binding</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/';</script><script type="text/javascript">var dzone_title = 'Three levels master detail with WPF';</script><script type="text/javascript">var dzone_blurb = 'Three levels master detail with WPF';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=TUV5N"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=TUV5N" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=Zr5XN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=Zr5XN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=h4Tfn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=h4Tfn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=yXHqN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=yXHqN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=l8q9N"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=l8q9N" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/446421268" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F08%2Fthree-levels-master-detail-with-wpf%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/three-levels-master-detail-with-wpf/</feedburner:origLink></item>
		<item>
		<title>WPF and wrapping text inside elements of a ListView</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/446297278/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 07:55:58 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/</guid>
		<description><![CDATA[TextBlock have the possibility to wrap, but sometimes you can get surprised by its behaviour. When I first began to work in WPF I started to use ListView to show complex object, because of the rich possibility to format the output with great flexibility. One day I created this little listview.


&#60;ListView x:Name=&#34;GroupsView&#34; ItemsSource=&#34;{Binding}&#34; &#62;
  [...]]]></description>
			<content:encoded><![CDATA[<p>TextBlock have the possibility to wrap, but sometimes you can get surprised by its behaviour. When I first began to work in WPF I started to use ListView to show complex object, because of the rich possibility to format the output with great flexibility. One day I created this little listview.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:02e5724d-0150-4b14-b5e1-8b74ddfda883" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">StackPanel </span><span style="color: #FF0000;">Orientation</span><span style="color: #0000FF;">=&quot;Horizontal&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock  </span><span style="color: #FF0000;">TextWrapping</span><span style="color: #0000FF;">=&quot;Wrap&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=LogIdentifier}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;">  </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;Count:&quot;</span><span style="color: #FF0000;"> FontWeight</span><span style="color: #0000FF;">=&quot;Bold&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;"> FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=Messages.Count}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">StackPanel</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It is a simple list and each element is composed by three textblock, the first should be the long one and I want it to wrap when the form is resized, the other two are small and should stay to the right. When I launched the form the result is not the desidered one, the first text does not wrap , nor the Count: is aligned to the right. The first error is that you should use DockPanel when you want elements to stay somewhere (right in this situation) and you want one of the element to fill remaining space, then I moved to this solution.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:4b0e933a-e754-4311-aa73-5bb08374fbca" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">DockPanel </span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock  </span><span style="color: #FF0000;">TextWrapping</span><span style="color: #0000FF;">=&quot;Wrap&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=LogIdentifier}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;">  </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">DockPanel.Dock</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;Count:&quot;</span><span style="color: #FF0000;"> FontWeight</span><span style="color: #0000FF;">=&quot;Bold&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;"> FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">DockPanel.Dock</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=Messages.Count}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">DockPanel</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>You can be still surprised that the result is not the desidered one, the first textblock does not gets wrapped, and “count: X” text is still not aligned to the right. To understand why the layout is arranged in such a way you should consider how the various container constrain dimension for their content. In <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d4bf491c-bd72-4bad-8dc1-57dbb2e6ad24/">this post</a> you can find a good description. We have to face two problems, the first is that we want the content of our ListView to have X size equal to that of the listview itself, this can be accomplished with <strong>HorizontalContentAlignment=&quot;Stretch&quot; </strong>attribute, that instructs the ListView to stretch the X size of its content on the horizontal size of the list itself. Then you should instruct the ListView not to show horizontal scrollbar. When scrollbar are enabled the listview grabs the “desidered size” of the controls, then it renders scrollbars accordingly, thus avoiding the word wrapping. To disable scrollbar you can simply use the attribute <strong>ScrollViewer.HorizontalScrollBarVisibility=&quot;Disabled&quot;</strong>. Finally you should notice that the order of the elements in the DockPanel is wrong. The dockPanel starts aligning from the first control to the last, and if the last has no DockPanel.Dock specified it is considered to fill remaining space. I need to work from right to left, first element is the number of count, then the Count: string, both with right alignment, finally the text that I wanted to wrap with no Dock specified.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:320f7c67-5499-483c-8260-7f573bfd28ff" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView </span><span style="color: #FF0000;">x:Name</span><span style="color: #0000FF;">=&quot;GroupsView&quot;</span><span style="color: #FF0000;"> ItemsSource</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;">
         HorizontalContentAlignment</span><span style="color: #0000FF;">=&quot;Stretch&quot;</span><span style="color: #FF0000;">
         ScrollViewer.HorizontalScrollBarVisibility</span><span style="color: #0000FF;">=&quot;Disabled&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">DockPanel </span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">DockPanel.Dock</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=Messages.Count}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;"> HorizontalAlignment</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">DockPanel.Dock</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;Count:&quot;</span><span style="color: #FF0000;"> FontWeight</span><span style="color: #0000FF;">=&quot;Bold&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;"> HorizontalAlignment</span><span style="color: #0000FF;">=&quot;Right&quot;</span><span style="color: #FF0000;"> FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">TextBlock </span><span style="color: #FF0000;">TextWrapping</span><span style="color: #0000FF;">=&quot;WrapWithOverflow&quot;</span><span style="color: #FF0000;"> Margin</span><span style="color: #0000FF;">=&quot;2,0,2,0&quot;</span><span style="color: #FF0000;"> Text</span><span style="color: #0000FF;">=&quot;</span><span style="color: #808000;">{Binding Path=LogIdentifier}</span><span style="color: #0000FF;">&quot;</span><span style="color: #FF0000;"> VerticalAlignment</span><span style="color: #0000FF;">=&quot;Center&quot;</span><span style="color: #FF0000;">  FontSize</span><span style="color: #0000FF;">=&quot;14&quot;</span><span style="color: #FF0000;">  </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
            </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">DockPanel</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">DataTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView.ItemTemplate</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">ListView</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Finally I got what I wanted.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image5.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="145" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb5.png" width="354" border="0" /></a> </p>
<p>I’m doing this post because this morning I woke up early, and wrote again the same wrong XAML (errare umanum est …), when I launch the application I though “mmm I solved this long time ago…” and begin crawling in my projects, then I preferred to do a post, so the next time I can search in my blog. <img src='http://www.nablasoft.com/alkampfer/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Alk.</p>
<p>Tags: <a href="http://technorati.com/tag/WPF" rel="tag">WPF</a> <a href="http://technorati.com/tag/.NET Framework" rel="tag">.NET Framework</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/';</script><script type="text/javascript">var dzone_title = 'WPF and wrapping text inside elements of a ListView';</script><script type="text/javascript">var dzone_blurb = 'WPF and wrapping text inside elements of a ListView';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=7S7pN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=7S7pN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=CJ9tN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=CJ9tN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=KzDWn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=KzDWn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=AbFLN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=AbFLN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=3X1ON"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=3X1ON" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/446297278" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F08%2Fwpf-and-wrapping-text-inside-elements-of-a-listview%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/08/wpf-and-wrapping-text-inside-elements-of-a-listview/</feedburner:origLink></item>
		<item>
		<title>Visual studio 2010</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/445695583/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 17:27:32 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[.NET framework]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/</guid>
		<description><![CDATA[Well, in this period I have a little time to spent on this, but I could not resist, 
 
With my new 500GB usb disk now I have enough space to work with the virtual machine of the Visual Studio 2010 CTP  
alk.
var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/';var dzone_title = 'Visual studio 2010';var dzone_blurb = 'Visual [...]]]></description>
			<content:encoded><![CDATA[<p>Well, in this period I have a little time to spent on this, but I could not resist, </p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image4.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="415" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb4.png" width="714" border="0" /></a> </p>
<p>With my new 500GB usb disk now I have enough space to work with the virtual machine of the Visual Studio 2010 CTP <img src='http://www.nablasoft.com/alkampfer/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>alk.</p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/';</script><script type="text/javascript">var dzone_title = 'Visual studio 2010';</script><script type="text/javascript">var dzone_blurb = 'Visual studio 2010';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=rqB9N"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=rqB9N" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=q07ON"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=q07ON" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=L3qun"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=L3qun" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=9h9tN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=9h9tN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=LuOHN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=LuOHN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/445695583" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F07%2Fvisual-studio-2010%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/visual-studio-2010/</feedburner:origLink></item>
		<item>
		<title>ye old XCopy</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/445500181/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 13:59:32 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/</guid>
		<description><![CDATA[My new external usb disk is just arrived, so I begin to move stuff into it……since I’m a great fan of command line tool I fired an XCopy of a really big big folder with really lot of files and the result is
E:\Codice Libri\WCF Unleashed\Examples\Windows Communication Foundation Unleashed     \ef_Chapter5_and_6_WindowsWorkflowFoundation\WCFUnleashedSamples\Chapter 6\Basic  [...]]]></description>
			<content:encoded><![CDATA[<p>My new external usb disk is just arrived, so I begin to move stuff into it……since I’m a great fan of command line tool I fired an XCopy of a really big big folder with really lot of files and the result is</p>
<blockquote><p>E:\Codice Libri\WCF Unleashed\Examples\Windows Communication Foundation Unleashed     <br />\ef_Chapter5_and_6_WindowsWorkflowFoundation\WCFUnleashedSamples\Chapter 6\Basic      <br />WCFHostedWorkflow\BasicWCFHostedWorkflow\bin\Debug\Interfaces.pdb      <br /><strong>Insufficient memory</strong></p>
</blockquote>
</p>
</p>
</p>
<p>whoa…it is the first time that xcopy gives me an insufficient memory…</p>
<p>alk.</p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/';</script><script type="text/javascript">var dzone_title = 'ye old XCopy';</script><script type="text/javascript">var dzone_blurb = 'ye old XCopy';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=39KGN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=39KGN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=ZgSHN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=ZgSHN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=JhMNn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=JhMNn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=W2DIN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=W2DIN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=pVrzN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=pVrzN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/445500181" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F07%2Fye-old-xcopy%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/ye-old-xcopy/</feedburner:origLink></item>
		<item>
		<title>Performance of logging system</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/445427054/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 12:13:22 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[.NET framework]]></category>

		<category><![CDATA[Tools and library]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/</guid>
		<description><![CDATA[When you insert a logging system into your application you should do some preliminary test for performance issue. The most important thing is that logging infrastructure should not introduce too much overhead when the log is disabled. With log4net you have different way to handle the log, suppose you have a logger and the DEBUG [...]]]></description>
			<content:encoded><![CDATA[<p>When you insert a logging system into your application you should do some preliminary test for performance issue. The most important thing is that logging infrastructure should not introduce too much overhead when the log is disabled. With log4net you have different way to handle the log, suppose you have a logger and the DEBUG level is disabled; you can use one of these three form</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:17594aaf-b964-4821-81b2-5e582dbcdec5" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (log1.IsDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);

Boolean isDebugEnabled </span><span style="color: #000000;">=</span><span style="color: #000000;"> log1.IsDebugEnabled;
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (isDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The first version rely on the fact that log4net infrastructure internally checks if debug is enabled so you can call Debug function directly. The second technique is to check if log1.IsDebugEnabled before calling log1.Debug(), and the third stores the IsDebugEnabled in a local variable to check that variable instead of calling IsDebugEnabled each time. If you do one million log calls you will obtain these rough measurements in milliseconds.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3c323cfc-e103-4463-9fff-3af2062c170d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">828</span><span style="color: #000000;">.</span><span style="color: #800080;">125</span><span style="color: #000000;">
if (log1.IsDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">750</span><span style="color: #000000;">
if (isDebugEnabled)  log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">31</span><span style="color: #000000;">.</span><span style="color: #800080;">25</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Thus solution 1 and 2 are quite similar in performances, while the third version is obviously the winner because it has not to call the log1.IsDebugEnabled at each run.</p>
<p>Now suppose that the messages is not a constant string but it has to be build</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:42fe116e-3170-4c94-9bba-727115e060d1" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">for</span><span style="color: #000000;"> (Int32 I </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; I </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> iterations; </span><span style="color: #000000;">++</span><span style="color: #000000;">I)
{
    log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> I.ToString() </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>In this situation the first test is important</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:100b1ce2-f78e-4d9a-946e-0a06cc27b9fe" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> + I.ToString() + </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">2968</span><span style="color: #000000;">.</span><span style="color: #800080;">75</span><span style="color: #000000;">
if (log1.IsDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> + I.ToString() + </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">75</span><span style="color: #000000;">
if (isDebugEnabled)  log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> + I.ToString() + </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) = </span><span style="color: #800080;">31</span><span style="color: #000000;">.</span><span style="color: #800080;">25</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The second and third results have the same timing as before, the log is disabled so both conditions evaluates to false as before. The important fact is that the first technique is much more slower, this because even if the debug level is not enable, you have to pay the time to do string concatenation to create the message to send to log.Debug function. </p>
<p>The conclusion is: <em>always check if the log is enabled</em>. Moreover do not forget to use log4net compiled in release mode to production code, the above timing with release version of log4net are the followings. </p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:789313d8-b65e-4cef-b038-ffd1aa8ce8e2" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">359.375</span><span style="color: #000000;">
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (log1.IsDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">250</span><span style="color: #000000;">
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (isDebugEnabled)  log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">
log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> I.ToString() </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2296.875</span><span style="color: #000000;">
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (log1.IsDebugEnabled) log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> I.ToString() </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">250</span><span style="color: #000000;">
</span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (isDebugEnabled)  log1.Debug(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">TEST</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> I.ToString() </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">other</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">) </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">15.625</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>As you can expect, the release version have really better performance.</p>
</p>
<p>Alk.</p>
<p>Tags: <a href="http://technorati.com/tag/Logging" rel="tag">Logging</a> <a href="http://technorati.com/tag/log4net" rel="tag">log4net</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/';</script><script type="text/javascript">var dzone_title = 'Performance of logging system';</script><script type="text/javascript">var dzone_blurb = 'Performance of logging system';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=dSgUN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=dSgUN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=aq78N"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=aq78N" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=b6fBn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=b6fBn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=thjiN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=thjiN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=dCRRN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=dCRRN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/445427054" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F07%2Fperformance-of-logging-system%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/07/performance-of-logging-system/</feedburner:origLink></item>
		<item>
		<title>MySQL server has gone away during restore</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/444388250/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 13:56:33 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/</guid>
		<description><![CDATA[
Today I was upgrading a Mantis bug tracking system, to avoid problems I backup the db and tried to restore in a different catalog. The restore procedure fail with the error
MySQL server has gone away

My first question is “gone away? and where? it is gone in vacation?” this is a perfect example of strange error [...]]]></description>
			<content:encoded><![CDATA[</p>
<p>Today I was upgrading a Mantis bug tracking system, to avoid problems I backup the db and tried to restore in a different catalog. The restore procedure fail with the error</p>
<blockquote><p>MySQL server has gone away</p>
</blockquote>
<p>My first question is “gone away? and where? it is gone in vacation?” this is a perfect example of strange error message, fortunately I suspect that the problem was due to a table with BLOB field with great data, and in fact the restore process generated this error during the restore of&#160; a very big row. after some search I found that I can increase the packet size for my server going into the server directory, edit the <em>my.ini</em> file adding this line </p>
<p>set-variable=<b>max_allowed_packet</b>=5M</p>
<p>that increase the maximum packet size to 5 M then everything went ok. </p>
<p>alk.</p>
</p>
</p>
</p>
<p>Tags: <a href="http://technorati.com/tag/MySql" rel="tag">MySql</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/';</script><script type="text/javascript">var dzone_title = 'MySQL server has gone away during restore';</script><script type="text/javascript">var dzone_blurb = 'MySQL server has gone away during restore';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=HIEPN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=HIEPN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=bqrUN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=bqrUN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=pIb4n"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=pIb4n" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=mHIHN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=mHIHN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=xHtsN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=xHtsN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/444388250" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F06%2Fmysql-server-has-gone-away-during-restore%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/mysql-server-has-gone-away-during-restore/</feedburner:origLink></item>
		<item>
		<title>Improving the WCF logger</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/444111027/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 07:35:12 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[.NET framework]]></category>

		<category><![CDATA[Nhibernate]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/</guid>
		<description><![CDATA[One of the first improvement to the WCF logger is the ability to choose the list of logger to monitor during registration. A real application can generate tons of logs, suppose you are interested only in looking at the NHibernate generated query. The solution is to add another registration method and internally use an object [...]]]></description>
			<content:encoded><![CDATA[<p>One of the first improvement to the WCF logger is the ability to choose the list of logger to monitor during registration. A real application can generate tons of logs, suppose you are interested only in looking at the NHibernate generated query. The solution is to add another registration method and internally use an object that keeps care of the registration and senting log progress. Here is the new interface</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:9fcbc7e6-42c0-4649-88d0-47a66f1b13ce" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">[ServiceContract(SessionMode </span><span style="color: #000000;">=</span><span style="color: #000000;"> SessionMode.Required, CallbackContract </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(IL4NClient))]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> IL4NServer
{
    [OperationContract]
    Boolean Register();

    [OperationContract]
    Boolean RegisterForSpecificLogger(</span><span style="color: #0000FF;">params</span><span style="color: #000000;"> String[] loggerNameList);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Now I can choose the name of the logger I’m interested to filter messages log.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image2.png"><img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="562" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb2.png" width="877" border="0" /></a></p>
</p>
</p>
<p>But this is not the only improvement I need. Quite often it happens that developer X called you telling “When I press the button Y the application responds after 20 seconds”, then you see with the SQL profiler that tons of query gets issued to the database. For a layered and complex application it can be difficult to understand where that queries are issued in the code, so I need a way to get a valid stack trace from the logger. To accomplish this task I modified the LogMessage object </p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e58ba77e-50be-4922-9ba3-34b53c0b3df2" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">[DataContract]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> LogMessage
{
    [DataMember]
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> String Message { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }

    [DataMember]
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StackStep</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">  StackSteps { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> LogMessage()
    {
        StackSteps </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">StackStep</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
        StackTrace trace </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StackTrace(</span><span style="color: #0000FF;">true</span><span style="color: #000000;">);
        var interestingFrames </span><span style="color: #000000;">=</span><span style="color: #000000;"> from StackFrame frame </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> trace.GetFrames()
                                </span><span style="color: #0000FF;">where</span><span style="color: #000000;"> frame.GetMethod().DeclaringType.Assembly.GetName().Name </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">LiveLogger4Log4Net</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">
                                      </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> frame.GetMethod().DeclaringType.Assembly.GetName().Name </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;">log4net</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">
                                                </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> frame.GetFileLineNumber() </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">
                                select </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> StackStep()
                                           {
                                               SourceFile </span><span style="color: #000000;">=</span><span style="color: #000000;"> frame.GetFileName(),
                                               LineNumber </span><span style="color: #000000;">=</span><span style="color: #000000;"> frame.GetFileLineNumber()
                                           };
        StackSteps.AddRange(interestingFrames);
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
<p>First I have inserted in the message a list of stacksteps, a class that contains filename and line number, in the constructor of the message I use the StackTrace .NET class to obtain the stack trace, then I do a linq query to remove all the steps that have no source file or are part of my logging system. The result is really interesting.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image3.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="220" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb3.png" width="982" border="0" /></a> </p>
<p>Not only I’m able to intercept NHibernate generated SQL, but I have a full stack trace that tells me where the query is generated, now the next step is to build a better client interface to present the result in a more usable and readable form.</p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/log4net" rel="tag">log4net</a> <a href="http://technorati.com/tag/NHibernate" rel="tag">NHibernate</a> <a href="http://technorati.com/tag/" rel="tag"></a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/';</script><script type="text/javascript">var dzone_title = 'Improving the WCF logger';</script><script type="text/javascript">var dzone_blurb = 'Improving the WCF logger';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=TlcMN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=TlcMN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=UTpJN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=UTpJN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=MJjzn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=MJjzn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=Mp2uN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=Mp2uN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=kEtsN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=kEtsN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/444111027" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F06%2Fimproving-the-wcf-logger%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/06/improving-the-wcf-logger/</feedburner:origLink></item>
		<item>
		<title>Live logging with log4net and WCF part 2</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/443447444/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 17:21:49 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[.NET framework]]></category>

		<category><![CDATA[Frameworks]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/</guid>
		<description><![CDATA[In previous post I showed how to create a publish/subscribe logger with WCF and log4net, now I show you my first use of this particular appender.
When you work with nhibernate you can use show-sql configuration parameter to instruct nhibernate to show generated sql, but how can you see this output in a running windows application? [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.nablasoft.com/alkampfer/index.php/2008/11/05/a-custom-publishsubscriber-appender-for-log4net/">previous post</a> I showed how to create a publish/subscribe logger with WCF and log4net, now I show you my first use of this particular appender.</p>
<p>When you work with nhibernate you can use show-sql configuration parameter to instruct nhibernate to show generated sql, but how can you see this output in a running windows application? You should know that NHibernate has various log4Net logger, and you can instruct the session to dump all sql code to a logger, here it is a possible configuration</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:eeaf4151-e559-4404-b23f-b9478c90fe48" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">appender </span><span style="color: #FF0000;">name</span><span style="color: #0000FF;">=&quot;WCFAppender&quot;</span><span style="color: #FF0000;"> type</span><span style="color: #0000FF;">=&quot;LiveLogger4Log4Net.WCFAppender, LiveLogger4Log4Net&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">layout </span><span style="color: #FF0000;">type</span><span style="color: #0000FF;">=&quot;log4net.Layout.PatternLayout&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">conversionPattern </span><span style="color: #FF0000;">value</span><span style="color: #0000FF;">=&quot;%date [%thread] %-5level %logger %ndc - %message%newline&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">layout</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">appender</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">logger </span><span style="color: #FF0000;">name</span><span style="color: #0000FF;">=&quot;NHibernate.SQL&quot;</span><span style="color: #FF0000;"> additivity</span><span style="color: #0000FF;">=&quot;false&quot;</span><span style="color: #0000FF;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">level </span><span style="color: #FF0000;">value</span><span style="color: #0000FF;">=&quot;DEBUG&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000FF;">&lt;</span><span style="color: #800000;">appender-ref </span><span style="color: #FF0000;">ref</span><span style="color: #0000FF;">=&quot;WCFAppender&quot;</span><span style="color: #FF0000;"> </span><span style="color: #0000FF;">/&gt;</span><span style="color: #000000;">
</span><span style="color: #0000FF;">&lt;/</span><span style="color: #800000;">logger</span><span style="color: #0000FF;">&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>With this configuration I instruct NHibernate to log to WCFAppender all generated SQL. Now I can attach a log viewer to a running application and look in real time to the sql instruction that nhibernate generates. You can attach more than one application, whenever you want.</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image1.png"><img title="image" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="558" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb1.png" width="758" border="0" /></a> </p>
<p>Welcome to log4net live logging monitor <img src='http://www.nablasoft.com/alkampfer/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/log4Net" rel="tag">log4Net</a> <a href="http://technorati.com/tag/NHibernate" rel="tag">NHibernate</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/';</script><script type="text/javascript">var dzone_title = 'Live logging with log4net and WCF part 2';</script><script type="text/javascript">var dzone_blurb = 'Live logging with log4net and WCF part 2';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </div>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><a href="http://www.dotnetkicks.com/kick/?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/&amp;bgcolor=0080C0&amp;fgcolor=FFFFFF&amp;border=000000&amp;cbgcolor=D4E1ED&amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/"></a></div>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~f/AlkampferEng?a=mXGvN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=mXGvN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=5wlKN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=5wlKN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=7keOn"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=7keOn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=qlaUN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=qlaUN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/AlkampferEng?a=JbGkN"><img src="http://feeds.feedburner.com/~f/AlkampferEng?i=JbGkN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/443447444" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/feed/</wfw:commentRss>
		<feedburner:awareness>http://api.feedburner.com/awareness/1.0/GetItemData?uri=AlkampferEng&amp;itemurl=http%3A%2F%2Fwww.nablasoft.com%2Falkampfer%2Findex.php%2F2008%2F11%2F05%2Flive-logging-with-log4net-and-wcf-part-2%2F</feedburner:awareness><feedburner:origLink>http://www.nablasoft.com/alkampfer/index.php/2008/11/05/live-logging-with-log4net-and-wcf-part-2/</feedburner:origLink></item>
		<item>
		<title>A custom publish/subscriber appender for log4net</title>
		<link>http://feeds.feedburner.com/~r/AlkampferEng/~3/443000124/</link>
		<comments>http://www.nablasoft.com/alkampfer/index.php/2008/11/05/a-custom-publishsubscriber-appender-for-log4net/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 08:24:39 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.nablasoft.com/alkampfer/index.php/2008/11/05/a-custom-publishsubscriber-appender-for-log4net/</guid>
		<description><![CDATA[Logging is one of the most important part of an application, the ability to detect cause of failures or malfunctions from the log is invaluable, but generating good logs is not easy.
The fundamental step to obtain a good logging infrastructure, is to use one of the libraries available in the open source world, and probably [...]]]></description>
			<content:encoded><![CDATA[<p>Logging is one of the most important part of an application, the ability to detect cause of failures or malfunctions from the log is invaluable, but generating good logs is not easy.</p>
<p>The fundamental step to obtain a good logging infrastructure, is to use one of the libraries available in the open source world, and probably you will end using <a href="http://logging.apache.org/log4net/index.html">log4net</a>&#160; because it is really good. One of the most important characteristic of a good infrastructure for logging is the ability to change the destination of logs without recompiling, and the possibility to extend the basic logging facility with custom destination storage.</p>
<p>One of the most useful appender is the AdoNet one, that permits you to store logs into database, then you can query them with the full power of SQL, even from a remote machine. This is really good but sometime testers call me telling “hey, when I do this and that the page raise an error” so I need to connect to log database, then look into the log trying to see what’s happened…&#160; this is good but not enough, I need a live logger, a way to attach to a remote process and look at the logs in realtime, in this way I can tell to the tester, please do again all the action that lead to the bug, while I’m watching at all the logging that are generated. log4Net has some network appender, the UDPAppender but I need to connect to a internet machine, and I want the log to travel in the network only when there is attached client.</p>
<p>With this need in mind I proceed to create a Wcf <a href="http://logging.apache.org/log4net/release/sdk/log4net.Appender.html">appender</a> for log4net using a publish subscribe technique, here is the two contract interfaces.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a7fbb4fc-c56d-4578-8dfb-d92f77381acf" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">[ServiceContract(SessionMode </span><span style="color: #000000;">=</span><span style="color: #000000;"> SessionMode.Required, CallbackContract </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(IL4NClient))]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> IL4NServer
{
    [OperationContract]
    Boolean Register();
}

[ServiceContract]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">interface</span><span style="color: #000000;"> IL4NClient
{
    [OperationContract(IsOneWay </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">)]
    </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> ReceiveLog(LogMessage log);

}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>For those not familiar with wcf the CallbackContract is a technique for the server to obtain a callback to sent <strong>one way messages</strong> to clients. Here it is a simple server that uses these two interfaces</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:7cefa835-8a3d-4bea-a63e-9bfd925922ad" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> AppenderService : IL4NServer
{
    </span><span style="color: #0000FF;">#region</span><span style="color: #000000;"> IL4NServer Members</span><span style="color: #000000;">

    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">IL4NClient</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> clients </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">IL4NClient</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
    </span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> IL4NServer.Register()
    {
        </span><span style="color: #0000FF;">lock</span><span style="color: #000000;"> (clients)
            clients.Add(OperationContext.Current.GetCallbackChannel</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">IL4NClient</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">());
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;">

    </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span><span style="color: #008000;">
    </span><span style="color: #808080;">///</span><span style="color: #008000;"> Called by the appender, it append a log.
    </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span><span style="color: #008000;">
    </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;param name=&quot;loggingEvent&quot;&gt;&lt;/param&gt;</span><span style="color: #808080;">
</span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Append(LoggingEvent loggingEvent)
    {
        LogMessage message </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> LogMessage() {Message </span><span style="color: #000000;">=</span><span style="color: #000000;"> loggingEvent.MessageObject.ToString()};
        </span><span style="color: #0000FF;">lock</span><span style="color: #000000;"> (clients)
        {
            </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> (Int32 I </span><span style="color: #000000;">=</span><span style="color: #000000;"> clients.Count </span><span style="color: #000000;">-</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">; I </span><span style="color: #000000;">&gt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #000000;">--</span><span style="color: #000000;">I)
            {
                </span><span style="color: #0000FF;">try</span><span style="color: #000000;">
                {
                    clients[I].ReceiveLog(message);
                }
                </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (Exception)
                {
                    clients.RemoveAt(I);
                }
            }
        }
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>thanks to the <strong>OperationContext.Current.GetCallbackChannel</strong>, the server is able to obtain an instance to a proxy that can be used to communicate back to the client through the IL2NClient interface. For each client that calls Register(), the server add the client callback to a inner static list. The Append() static method can be called from everywhere to send a log to all registered clients. As you can see, the code cycle trough all registered clients, and for each clients that generates exception that client is removed from the list.</p>
<p>Now I can create a very simple appender for Log4Net</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e4f6db6f-e77f-4a4b-b72f-deb0477750a0" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> WCFAppender : AppenderSkeleton
    {
        </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> ServiceHost host;

        </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> WCFAppender()
        {
            </span><span style="color: #0000FF;">try</span><span style="color: #000000;">
            {
                host </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ServiceHost(</span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(AppenderService));
                host.Open();
            }
            </span><span style="color: #0000FF;">catch</span><span style="color: #000000;"> (Exception ex)
            {

                </span><span style="color: #0000FF;">throw</span><span style="color: #000000;">;
            }
        }

        </span><span style="color: #0000FF;">protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">override</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Append(LoggingEvent loggingEvent)
        {
            AppenderService.Append(loggingEvent);
        }

        </span><span style="color: #0000FF;">override</span><span style="color: #000000;"> </span><span style="color: #0000FF;">protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> RequiresLayout
        {
            </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">true</span><span style="color: #000000;">; }
        }

        </span><span style="color: #0000FF;">protected</span><span style="color: #000000;"> </span><span style="color: #0000FF;">override</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> OnClose()
        {
            host.Close();
            </span><span style="color: #0000FF;">base</span><span style="color: #000000;">.OnClose();
        }

        </span><span style="color: #0000FF;">#endregion</span><span style="color: #000000;"> Override implementation of AppenderSkeleton</span><span style="color: #000000;">

    }</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This appender is really simple, it host a communication foundation server for the AppenderService in his constructor and in Append() method it simply called static Append() method of the AppenderService.</p>
<p>With this simple class I can now configure an application to use this custom appender, and then you can connect to it and intercept all the log from another application, creating a live logging listener. Here is a screenshot of a running demo</p>
<p><a href="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image.png"><img title="image" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="410" alt="image" src="http://www.nablasoft.com/alkampfer/wp-content/uploads/2008/11/image-thumb.png" width="700" border="0" /></a> </p>
<p>As you can see I create a simple windows form with a button to generate warn, then I can open a simple console application that connects and listen to all generated logs. You can attach how many listener program as you want.</p>
<p>A complete example can be found here <a title="http://nablasoft.googlecode.com/svn/trunk/Log4NetLive" href="http://nablasoft.googlecode.com/svn/trunk/Log4NetLive">http://nablasoft.googlecode.com/svn/trunk/Log4NetLive</a>.</p>
<p>alk.</p>
<p>Tags: <a href="http://technorati.com/tag/log4Net" rel="tag">log4Net</a> <a href="http://technorati.com/tag/Appenders:Logging" rel="tag">Appenders:Logging</a></p>
<div class="wlWriterHeaderFooter" style="text-align:left; margin:0px; padding:4px 4px 4px 4px;"><script type="text/javascript">var dzone_url = 'http://www.nablasoft.com/alkampfer/ind