<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brian Low &#187; .Net</title>
	<atom:link href="http://www.brianlow.com/index.php/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brianlow.com</link>
	<description>Software Development Notes</description>
	<lastBuildDate>Fri, 16 Jul 2010 22:02:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Find Conflicting Assembly References</title>
		<link>http://www.brianlow.com/index.php/2010/01/25/find-conflicting-assembly-references/</link>
		<comments>http://www.brianlow.com/index.php/2010/01/25/find-conflicting-assembly-references/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:28:22 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/?p=199</guid>
		<description><![CDATA[A small test to help resolve the compiler warning:
warning MSB3247: Found conflicts between different versions of the same dependent assembly.

Point it to the build folder of your project and it will look for referenced assemblies with the same short name but different full names.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
&#160;
namespace MyProject
&#123;
    &#91;TestFixture&#93;
 [...]]]></description>
			<content:encoded><![CDATA[<p>A small test to help resolve the compiler warning:</p>
<blockquote><p>warning MSB3247: Found conflicts between different versions of the same dependent assembly.
</p></blockquote>
<p>Point it to the build folder of your project and it will look for referenced assemblies with the same short name but different full names.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">NUnit.Framework</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> MyProject
<span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> UtilityTest
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> FindConflictingReferences<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var assemblies <span style="color: #008000;">=</span> GetAllAssemblies<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;E:\dev\myapp\myproject\bin\debug&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var references <span style="color: #008000;">=</span> GetReferencesFromAllAssemblies<span style="color: #000000;">&#40;</span>assemblies<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var groupsOfConflicts <span style="color: #008000;">=</span> FindReferencesWithTheSameShortNameButDiffererntFullNames<span style="color: #000000;">&#40;</span>references<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var group <span style="color: #0600FF;">in</span> groupsOfConflicts<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Console.<span style="color: #0600FF;">Out</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Possible conflicts for {0}:&quot;</span>, group.<span style="color: #0000FF;">Key</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var reference <span style="color: #0600FF;">in</span> group<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    Console.<span style="color: #0600FF;">Out</span>.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0} references {1}&quot;</span>,
                                          reference.<span style="color: #0000FF;">Assembly</span>.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">PadRight</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">25</span><span style="color: #000000;">&#41;</span>,
                                          reference.<span style="color: #0000FF;">ReferencedAssembly</span>.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> IEnumerable<span style="color: #008000;">&lt;</span>IGrouping<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, Reference<span style="color: #008000;">&gt;&gt;</span> FindReferencesWithTheSameShortNameButDiffererntFullNames<span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>Reference<span style="color: #008000;">&gt;</span> references<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> from reference <span style="color: #0600FF;">in</span> references
                   group reference by reference.<span style="color: #0000FF;">ReferencedAssembly</span>.<span style="color: #0000FF;">Name</span>
                       into referenceGroup
                       where referenceGroup.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>reference <span style="color: #008000;">=&gt;</span> reference.<span style="color: #0000FF;">ReferencedAssembly</span>.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Distinct</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span>
                       select referenceGroup<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>Reference<span style="color: #008000;">&gt;</span> GetReferencesFromAllAssemblies<span style="color: #000000;">&#40;</span>List<span style="color: #008000;">&lt;</span>Assembly<span style="color: #008000;">&gt;</span> assemblies<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var references <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Reference<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var assembly <span style="color: #0600FF;">in</span> assemblies<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var referencedAssembly <span style="color: #0600FF;">in</span> assembly.<span style="color: #0000FF;">GetReferencedAssemblies</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    references.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Reference
                    <span style="color: #000000;">&#123;</span>
                        Assembly <span style="color: #008000;">=</span> assembly.<span style="color: #0000FF;">GetName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>,
                        ReferencedAssembly <span style="color: #008000;">=</span> referencedAssembly
                    <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">return</span> references<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>Assembly<span style="color: #008000;">&gt;</span> GetAllAssemblies<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var files <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>FileInfo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var directoryToSearch <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DirectoryInfo<span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            files.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>directoryToSearch.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;*.dll&quot;</span>, SearchOption.<span style="color: #0000FF;">AllDirectories</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            files.<span style="color: #0000FF;">AddRange</span><span style="color: #000000;">&#40;</span>directoryToSearch.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;*.exe&quot;</span>, SearchOption.<span style="color: #0000FF;">AllDirectories</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> files.<span style="color: #0000FF;">ConvertAll</span><span style="color: #000000;">&#40;</span>file <span style="color: #008000;">=&gt;</span> Assembly.<span style="color: #0000FF;">LoadFile</span><span style="color: #000000;">&#40;</span>file.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">class</span> Reference
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">public</span> AssemblyName Assembly <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">public</span> AssemblyName ReferencedAssembly <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2010/01/25/find-conflicting-assembly-references/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Resharper live templates for NUnit</title>
		<link>http://www.brianlow.com/index.php/2009/04/06/resharper-live-templates-for-nunit/</link>
		<comments>http://www.brianlow.com/index.php/2009/04/06/resharper-live-templates-for-nunit/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 08:57:51 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/?p=173</guid>
		<description><![CDATA[
aae       Assert.AreEqual
aane      Assert.AreNotEqual
aif       Assert.IsFalse
ait       Assert.IsTrue
ain       Assert.IsNull
ainn      Assert.IsNotNull
setup     NUnit [Setup] method
teardown  NUnit [TearDown] method
test    [...]]]></description>
			<content:encoded><![CDATA[<pre>
aae       Assert.AreEqual
aane      Assert.AreNotEqual
aif       Assert.IsFalse
ait       Assert.IsTrue
ain       Assert.IsNull
ainn      Assert.IsNotNull
setup     NUnit [Setup] method
teardown  NUnit [TearDown] method
test      NUnit [Test] method
</pre>
<p><a href="/wordpress/wp-content/uploads/2009/03/brian-live-templates.xml">For ReSharper 3.0</a><br/><br />
<a href='/wordpress/wp-content/uploads/nunit-live-template-45.xml'>For ReSharper 4.5</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2009/04/06/resharper-live-templates-for-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Build ASP.NET WebService from the Command-line</title>
		<link>http://www.brianlow.com/index.php/2008/08/31/build-aspnet-webservice-from-the-command-line/</link>
		<comments>http://www.brianlow.com/index.php/2008/08/31/build-aspnet-webservice-from-the-command-line/#comments</comments>
		<pubDate>Sun, 31 Aug 2008 20:30:24 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/?p=123</guid>
		<description><![CDATA[This NAnt target will build the project into a new folder with a (single) precompiled DLL. All source files and other non-deployment files are removed.

&#60;target name=&#34;publish.webservice&#34;&#62;
    &#60;exec basedir=&#34;.&#34;
          program=&#34;${msbuild.exe}&#34;
          commandline=&#34; ${web.project.file}
    [...]]]></description>
			<content:encoded><![CDATA[<p>This NAnt target will build the project into a new folder with a (single) precompiled DLL. All source files and other non-deployment files are removed.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;publish.webservice&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exec</span> <span style="color: #000066;">basedir</span>=<span style="color: #ff0000;">&quot;.&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">program</span>=<span style="color: #ff0000;">&quot;${msbuild.exe}&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">commandline</span>=<span style="color: #ff0000;">&quot; ${web.project.file}</span>
<span style="color: #009900;">                        /nologo</span>
<span style="color: #009900;">                        /p:OutDir=${publish.webservice.dir}bin</span>
<span style="color: #009900;">                        /p:WebProjectOutputDir=${publish.webservice.dir}&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">workingdir</span>=<span style="color: #ff0000;">&quot;.&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">failonerror</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2008/08/31/build-aspnet-webservice-from-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HttpContext.Cache vs. HttpContext.Items</title>
		<link>http://www.brianlow.com/index.php/2007/06/25/httpcontextcache-vs-httpcontextitems/</link>
		<comments>http://www.brianlow.com/index.php/2007/06/25/httpcontextcache-vs-httpcontextitems/#comments</comments>
		<pubDate>Mon, 25 Jun 2007 23:09:10 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/06/25/httpcontextcache-vs-httpcontextitems/</guid>
		<description><![CDATA[HttpContext.Cache is simply a pointer the HttpRuntime.Cache also known as the application cache. This can be verified by opening System.Web with Reflector. The MSDN documentation for this member is misleading: &#8220;Gets the Cache object for the current HTTP request&#8221;. Information stored here is global to the application domain, not a specific request. 
To store per-request [...]]]></description>
			<content:encoded><![CDATA[<p>HttpContext.Cache is simply a pointer the HttpRuntime.Cache also known as the application cache. This can be verified by opening System.Web with Reflector. The MSDN documentation for this member is misleading: &#8220;Gets the Cache object for the current HTTP request&#8221;. Information stored here is global to the application domain, not a specific request. </p>
<p>To store per-request information use <strong>HttpContext.Items</strong>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/06/25/httpcontextcache-vs-httpcontextitems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Impersonate User</title>
		<link>http://www.brianlow.com/index.php/2007/04/20/impersonate-user/</link>
		<comments>http://www.brianlow.com/index.php/2007/04/20/impersonate-user/#comments</comments>
		<pubDate>Fri, 20 Apr 2007 23:31:10 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/04/20/impersonate-user/</guid>
		<description><![CDATA[
/// &#60;summary&#62;
/// Impersonate a user within the current thread.
/// This class will automatically revert the user to the original user 
/// identity when this class is disposed. Use this class with the 
/// C# using or a try/catch block to ensure the user is reverted
/// after an exception.
/// 
/// Suggested usage:
/// using (new ImpersonateHelper(&#34;domain&#34;, &#34;joe&#34;, [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Impersonate a user within the current thread.</span>
<span style="color: #008080; font-style: italic;">/// This class will automatically revert the user to the original user </span>
<span style="color: #008080; font-style: italic;">/// identity when this class is disposed. Use this class with the </span>
<span style="color: #008080; font-style: italic;">/// C# using or a try/catch block to ensure the user is reverted</span>
<span style="color: #008080; font-style: italic;">/// after an exception.</span>
<span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #008080; font-style: italic;">/// Suggested usage:</span>
<span style="color: #008080; font-style: italic;">/// using (new ImpersonateHelper(&quot;domain&quot;, &quot;joe&quot;, &quot;password&quot;))</span>
<span style="color: #008080; font-style: italic;">/// {</span>
<span style="color: #008080; font-style: italic;">///     ...code to run as joe</span>
<span style="color: #008080; font-style: italic;">/// }</span>
<span style="color: #008080; font-style: italic;">/// ...code to run as orignial user</span>
<span style="color: #008080; font-style: italic;">/// </span>
<span style="color: #008080; font-style: italic;">/// Note, this implementation is vulnerable to &quot;exception filter&quot; attacks</span>
<span style="color: #008080; font-style: italic;">/// as described below. Evaluate it against the risk of other attacks</span>
<span style="color: #008080; font-style: italic;">/// such as the credential store used to impersonate.</span>
<span style="color: #008080; font-style: italic;">/// http://blogs.msdn.com/shawnfa/archive/2005/03/22/400749.aspx</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ImpersonationHelper <span style="color: #008000;">:</span> IDisposable
<span style="color: #000000;">&#123;</span>
    IntPtr m_tokenHandle <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    WindowsImpersonationContext m_impersonatedUser<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080;">#region Win32 API Declarations</span>
&nbsp;
    <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LOGON32_PROVIDER_DEFAULT <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LOGON32_LOGON_INTERACTIVE <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span>    <span style="color: #008080; font-style: italic;">//This parameter causes LogonUser to create a primary token.</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;advapi32.dll&quot;</span>, SetLastError<span style="color: #008000;">=</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> LogonUser<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> lpszUsername, <span style="color: #FF0000;">String</span> lpszDomain, <span style="color: #FF0000;">String</span> lpszPassword, 
    <span style="color: #FF0000;">int</span> dwLogonType, <span style="color: #FF0000;">int</span> dwLogonProvider, <span style="color: #0600FF;">ref</span> IntPtr phToken<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, CharSet<span style="color: #008000;">=</span>CharSet.<span style="color: #0600FF;">Auto</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">extern</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> CloseHandle<span style="color: #000000;">&#40;</span>IntPtr handle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;advapi32.dll&quot;</span>, CharSet<span style="color: #008000;">=</span>CharSet.<span style="color: #0600FF;">Auto</span>, SetLastError<span style="color: #008000;">=</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">extern</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> DuplicateToken<span style="color: #000000;">&#40;</span>IntPtr ExistingTokenHandle, 
    <span style="color: #FF0000;">int</span> SECURITY_IMPERSONATION_LEVEL, <span style="color: #0600FF;">ref</span> IntPtr DuplicateTokenHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Constructor. Impersonates the requested user. Impersonation lasts until</span>
    <span style="color: #008080; font-style: italic;">/// the instance is disposed.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> ImpersonationHelper<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> domain, <span style="color: #FF0000;">string</span> user, <span style="color: #FF0000;">string</span> password<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Call LogonUser to obtain a handle to an access token.</span>
        <span style="color: #FF0000;">bool</span> returnValue <span style="color: #008000;">=</span> LogonUser<span style="color: #000000;">&#40;</span>user, domain, password,
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
            <span style="color: #0600FF;">ref</span> m_tokenHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span> <span style="color: #008000;">==</span> returnValue<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> ret <span style="color: #008000;">=</span> Marshal.<span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">ComponentModel</span></span>.<span style="color: #0000FF;">Win32Exception</span><span style="color: #000000;">&#40;</span>ret<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Impersonate</span>
        m_impersonatedUser <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WindowsIdentity<span style="color: #000000;">&#40;</span>m_tokenHandle<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Impersonate</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#region IDisposable Pattern</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Revert to original user and cleanup.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposing<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposing<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Revert to original user identity</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_impersonatedUser <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                m_impersonatedUser.<span style="color: #0000FF;">Undo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Free the tokens.</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>m_tokenHandle <span style="color: #008000;">!=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span>
            CloseHandle<span style="color: #000000;">&#40;</span>m_tokenHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Explicit dispose.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Destructor</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    ~ImpersonationHelper<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/04/20/impersonate-user/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Log4Net + ASP.Net</title>
		<link>http://www.brianlow.com/index.php/2007/04/17/log4net-aspnet/</link>
		<comments>http://www.brianlow.com/index.php/2007/04/17/log4net-aspnet/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 22:49:45 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/04/17/log4net-aspnet/</guid>
		<description><![CDATA[In web applications, Log4Net&#8217;s ThreadContext properties (typically items like the current user) should be set immediately before each logging call.
Normally these properties are set in Global.Session_Start(). However, log4net internally stores these properties in thread local storage.  Using Reflector to disassemble log4net.dll (v1.2.10), start with log4net.ThreadContext which leads to ThreadContextProperties.get_Item() and then to ThreadContextProperties.GetProperties(). The [...]]]></description>
			<content:encoded><![CDATA[<p>In web applications, Log4Net&#8217;s ThreadContext properties (typically items like the current user) should be set immediately before each logging call.</p>
<p>Normally these properties are set in Global.Session_Start(). However, log4net internally stores these properties in thread local storage.  Using Reflector to disassemble log4net.dll (v1.2.10), start with log4net.ThreadContext which leads to ThreadContextProperties.get_Item() and then to ThreadContextProperties.GetProperties(). The disassembly for this method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">internal</span> PropertiesDictionary GetProperties<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> create<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    PropertiesDictionary data<span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>PropertiesDictionary<span style="color: #000000;">&#41;</span> Thread.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">&#40;</span>s_threadLocalSlot<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>data <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> create<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertiesDictionary<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Thread.<span style="color: #0000FF;">SetData</span><span style="color: #000000;">&#40;</span>s_threadLocalSlot, data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> data<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The call to Thread.GetData() is the problem. ASP.Net, when under load, may switch threads when processing a single request (see <a href="http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html">ThreadStatic, CallContext and HttpContext in ASP.Net</a>). You may initialize log4net with the current user on one thread, then attempt to log on a different thread. Log4net.LogicalThreadContext() suffers a similar problem as it uses CallContext to store its data rather than HttpContext.</p>
<p>For reliable logging information, set these ThreadContext properties each time you log an entry. The additional processing time will be negligable in most cases. Further, as a general rule of thumb, use HttpContext to store request-specific information in ASP.Net applications.</p>
<p>10 minutes after finishing this post I come across this post, also from piers7:<br />
<a href="http://piers7.blogspot.com/2005/12/log4net-context-problems-with-aspnet.html">log4net Context problems with ASP.Net thread agility</a>. It suggests another workaround.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/04/17/log4net-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting File Sizes</title>
		<link>http://www.brianlow.com/index.php/2007/03/25/formatting-file-sizes/</link>
		<comments>http://www.brianlow.com/index.php/2007/03/25/formatting-file-sizes/#comments</comments>
		<pubDate>Sun, 25 Mar 2007 07:27:12 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/03/25/formatting-file-sizes/</guid>
		<description><![CDATA[In Windows Explorer file sizes are displayed in two different formats. In the Properties page, file sizes are displayed in bytes, KB, MB, GB or TB depending on the size. The StrFormatByteSize() Win32 API can reproduce this format. The second format is used under the &#8220;Size&#8221; column when viewing a folder. File sizes are displayed [...]]]></description>
			<content:encoded><![CDATA[<p>In Windows Explorer file sizes are displayed in two different formats. In the Properties page, file sizes are displayed in bytes, KB, MB, GB or TB depending on the size. The <A href="http://msdn2.microsoft.com/en-us/library/ms538647.aspx">StrFormatByteSize()</a> Win32 API can reproduce this format. The second format is used under the &#8220;Size&#8221; column when viewing a folder. File sizes are displayed in whole KB with thousands separators.  I have not found a API call to reproduce this format. The <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/string/strformatkbsize.asp">StrFormatKBSize()</a> function is close but differs (it displays two decimal places).</p>
<p>Here are two pure C# functions to reproduce these formats:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Converts a number value into a string that represents the number </span>
<span style="color: #008080; font-style: italic;">/// expressed in whole kilobytes. This is a format similar to the </span>
<span style="color: #008080; font-style: italic;">///	Windows Explorer &quot;Size&quot; column.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> FileSizeToStringKB<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> fileSize<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0:n0} KB&quot;</span>, Math.<span style="color: #0000FF;">Ceiling</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">double</span><span style="color: #000000;">&#41;</span>fileSize <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Converts a numeric value into a string that represents the number </span>
<span style="color: #008080; font-style: italic;">///	expressed as a size value in bytes, kilobytes, megabytes, gigabytes, </span>
<span style="color: #008080; font-style: italic;">///	or terabytes depending on the size. Output is identical to </span>
<span style="color: #008080; font-style: italic;">///	StrFormatByteSize() in shlwapi.dll. This is a format similar to </span>
<span style="color: #008080; font-style: italic;">/// the Windows Explorer file Properties page. For example:</span>
<span style="color: #008080; font-style: italic;">///	     532 -&gt;  532 bytes</span>
<span style="color: #008080; font-style: italic;">///     1240 -&gt; 1.21 KB</span>
<span style="color: #008080; font-style: italic;">///	  235606 -&gt;  230 KB</span>
<span style="color: #008080; font-style: italic;">///  5400016 -&gt; 5.14 MB</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;</span>
<span style="color: #008080; font-style: italic;">///	It was surprisingly difficult to emulate the StrFormatByteSize() function</span>
<span style="color: #008080; font-style: italic;">/// due to a few quirks. First, the function only displays three digits:</span>
<span style="color: #008080; font-style: italic;">///  - displays 2 decimal places for values under 10	(e.g. 2.12 KB)</span>
<span style="color: #008080; font-style: italic;">///  - displays 1 decimal place for values under 100	(e.g. 88.2 KB)</span>
<span style="color: #008080; font-style: italic;">///	 - displays 0 decimal places for values under 1000	(e.g. 532 KB)</span>
<span style="color: #008080; font-style: italic;">///	 - jumps to the next unit of measure for values over 1000  (e.g. 0.97 MB)</span>
<span style="color: #008080; font-style: italic;">/// The second quirk: insiginificant digits are truncated rather than </span>
<span style="color: #008080; font-style: italic;">/// rounded. The original function likely uses integer math.</span>
<span style="color: #008080; font-style: italic;">/// This implementation was tested to 100 TB.</span>
<span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> FileSizeToString<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">long</span> fileSize<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">double</span> value<span style="color: #008000;">;</span>
	<span style="color: #FF0000;">string</span> unit<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>fileSize <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">1024</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0} bytes&quot;</span>, fileSize<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">else</span> 
	<span style="color: #000000;">&#123;</span>
		value <span style="color: #008000;">=</span> fileSize<span style="color: #008000;">;</span>
		value <span style="color: #008000;">=</span> value <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">;</span>
		unit <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;KB&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			value <span style="color: #008000;">=</span> value <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">;</span>
			unit <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MB&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			value <span style="color: #008000;">=</span> value <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">;</span>
			unit <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;GB&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			value <span style="color: #008000;">=</span> value <span style="color: #008000;">/</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">;</span>
			unit <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;TB&quot;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #008000;">*</span> <span style="color: #FF0000;">100</span> <span style="color: #000000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0:f2} {1}&quot;</span>, value, unit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>value <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">100</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #008000;">*</span> <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0:f1} {1}&quot;</span>, value, unit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">else</span>
		<span style="color: #000000;">&#123;</span>
			value <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Floor</span><span style="color: #000000;">&#40;</span> value <span style="color: #008000;">*</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0:f0} {1}&quot;</span>, value, unit<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/03/25/formatting-file-sizes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CLR Debugging</title>
		<link>http://www.brianlow.com/index.php/2007/01/19/clr-debugging/</link>
		<comments>http://www.brianlow.com/index.php/2007/01/19/clr-debugging/#comments</comments>
		<pubDate>Fri, 19 Jan 2007 21:46:36 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/01/19/clr-debugging/</guid>
		<description><![CDATA[To get started with debugging the CLR see Tess Ferrandez&#8217;s post on getting .NET memory dumps and Associating Windbg with .dmp files. See her other posts for mining these dumps for useful information. 
To debug ASP.Net worker process crashes, create a batch file named debug_crash_iis5.bat in windbg folder with:
cscript.exe adplus.vbs -crash -pn aspnet_wp.exe -FullOnFirst
To dump [...]]]></description>
			<content:encoded><![CDATA[<p>To get started with debugging the CLR see Tess Ferrandez&#8217;s post on getting <a href="http://blogs.msdn.com/tess/archive/2006/01/11/511773.aspx">.NET memory dumps</a> and <a href="http://blogs.msdn.com/tess/archive/2005/12/05/500020.aspx">Associating Windbg with .dmp files</a>. See her other posts for mining these dumps for useful information. </p>
<p>To debug ASP.Net worker process crashes, create a batch file named <b>debug_crash_iis5.bat</b> in windbg folder with:<br />
cscript.exe adplus.vbs -crash -pn aspnet_wp.exe -FullOnFirst</p>
<p>To dump (non-crashing) exceptions from ASP.Net, create a batch file named <b>debug_monitor_iis5.bat</b> with:<br />
cscript.exe adplus.vbs -pn aspnet_wp.exe -c TrackCLR.cfg</p>
<p>and another file named <b>TrackCLR.cfg</b> with:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ADPLUS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SETTINGS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RUNMODE<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CRASH<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RUNMODE<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SETTINGS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PRECOMMANDS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CMD<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>!load clr10\sos<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CMD<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PRECOMMANDS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;EXCEPTIONS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OPTION<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NoDumpOnFirstChance<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/OPTION<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OPTION<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>NoDumpOnSecondChance<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/OPTION<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CONFIG<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #808080; font-style: italic;">&lt;!-- This is for the CLR exception --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CODE<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>clr<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CODE<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ACTIONS1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ACTIONS1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;CUSTOMACTIONS1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>!cen;!clrstack;.dump /ma /u c:\exceptiondump.dmp;gn <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CUSTOMACTIONS1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RETURNACTION1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GN<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/RETURNACTION1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/CONFIG<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/EXCEPTIONS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ADPLUS<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>After opening a .dmp file with windbg, the most useful commands are:<br />
!clrstack<br />
!dae<br />
!help</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/01/19/clr-debugging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Catching Unhandled ASP.Net Exceptions</title>
		<link>http://www.brianlow.com/index.php/2007/01/11/catching-unhandled-aspnet-exceptions/</link>
		<comments>http://www.brianlow.com/index.php/2007/01/11/catching-unhandled-aspnet-exceptions/#comments</comments>
		<pubDate>Thu, 11 Jan 2007 21:22:05 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/01/11/catching-unhandled-aspnet-exceptions/</guid>
		<description><![CDATA[Exceptions that occur off the worker process thread are silently swallowed by ASP.NET 1.1. Use the method described in MS article 911816 to create an HttpModule to catch the exceptions. The follow is adapted for ASP.NET 1.1 without requiring registration in the GAC and a strong name. It also logs to the EventLog as well [...]]]></description>
			<content:encoded><![CDATA[<p>Exceptions that occur off the worker process thread are silently swallowed by ASP.NET 1.1. Use the method described in <a href="http://support.microsoft.com/?id=911816">MS article 911816</a> to create an HttpModule to catch the exceptions. The follow is adapted for ASP.NET 1.1 without requiring registration in the GAC and a strong name. It also logs to the EventLog as well as sending an email as </p>
<p>Add the following code to UnhandledExceptionModule.cs:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Globalization</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Web</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> WebMonitor <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> UnhandledExceptionModule<span style="color: #008000;">:</span> IHttpModule <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> _unhandledExceptionCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> _sourceName <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">object</span> _initLock <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">bool</span> _initialized <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> _mailServer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;mymailserver&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> _mailTo <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;abc@xyz.com&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Init<span style="color: #000000;">&#40;</span>HttpApplication app<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// Do this one time for each AppDomain.</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_initialized<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span>_initLock<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>_initialized<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> 
&nbsp;
                        _sourceName <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>CultureInfo.<span style="color: #0000FF;">InvariantCulture</span>, <span style="color: #666666;">&quot;ASP.NET {0}.{1}.{2}.0&quot;</span>,
							<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Environment</span>.<span style="color: #0000FF;">Version</span>.<span style="color: #0000FF;">Major</span>, 
							<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Environment</span>.<span style="color: #0000FF;">Version</span>.<span style="color: #0000FF;">Minor</span>,
							<span style="color: #000000;">System</span>.<span style="color: #0000FF;">Environment</span>.<span style="color: #0000FF;">Version</span>.<span style="color: #0000FF;">Build</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>EventLog.<span style="color: #0000FF;">SourceExists</span><span style="color: #000000;">&#40;</span>_sourceName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span>CultureInfo.<span style="color: #0000FF;">InvariantCulture</span>,
                                                              <span style="color: #666666;">&quot;There is no EventLog source named '{0}'.&quot;</span>, 
                                                              _sourceName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                        <span style="color: #000000;">&#125;</span>
&nbsp;
                        AppDomain.<span style="color: #0000FF;">CurrentDomain</span>.<span style="color: #0000FF;">UnhandledException</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> UnhandledExceptionEventHandler<span style="color: #000000;">&#40;</span>OnUnhandledException<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                        _initialized <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
						LogMessage<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;UnhandledExceptionModule started&quot;</span>, <span style="color: #666666;">&quot;&quot;</span>, EventLogEntryType.<span style="color: #0000FF;">Information</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">void</span> OnUnhandledException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> o, UnhandledExceptionEventArgs e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Let this occur one time for each AppDomain.</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Interlocked.<span style="color: #0000FF;">Exchange</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> _unhandledExceptionCount, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                return<span style="color: #008000;">;</span>
&nbsp;
            StringBuilder message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>UnhandledException logged by UnhandledExceptionModule.dll:<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>appId=&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #FF0000;">string</span> appId <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span> AppDomain.<span style="color: #0000FF;">CurrentDomain</span>.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;.appId&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>appId <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                message.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>appId<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            Exception currentException <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>currentException <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Exception<span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">ExceptionObject</span><span style="color: #008000;">;</span> currentException <span style="color: #008000;">!=</span> null<span style="color: #008000;">;</span> currentException <span style="color: #008000;">=</span> currentException.<span style="color: #0000FF;">InnerException</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                message.<span style="color: #0000FF;">AppendFormat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>type={0}<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>message={1}<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>stack=<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>{2}<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>,
                                     currentException.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FullName</span>, 
                                     currentException.<span style="color: #0000FF;">Message</span>,
                                     currentException.<span style="color: #0000FF;">StackTrace</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>           
&nbsp;
			LogMessage<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;UnhandledExceptionModule exception&quot;</span>, message.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, EventLogEntryType.<span style="color: #0000FF;">Error</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">void</span> LogMessage<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> subject, <span style="color: #FF0000;">string</span> message, EventLogEntryType severity<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">try</span>
			<span style="color: #000000;">&#123;</span>
				EventLog Log <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> EventLog<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
				Log.<span style="color: #0000FF;">Source</span> <span style="color: #008000;">=</span> _sourceName<span style="color: #008000;">;</span>
				Log.<span style="color: #0000FF;">WriteEntry</span><span style="color: #000000;">&#40;</span>subject <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> message, severity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0600FF;">catch</span>  <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
			<span style="color: #0600FF;">try</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">Mail</span>.<span style="color: #0000FF;">MailMessage</span> msg <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">Mail</span>.<span style="color: #0000FF;">MailMessage</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
				msg.<span style="color: #0000FF;">Body</span> <span style="color: #008000;">=</span> message<span style="color: #008000;">;</span>
				msg.<span style="color: #0000FF;">To</span> <span style="color: #008000;">=</span> _mailTo<span style="color: #008000;">;</span>
				msg.<span style="color: #0000FF;">Subject</span> <span style="color: #008000;">=</span> severity.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;: &quot;</span> <span style="color: #008000;">+</span> subject<span style="color: #008000;">;</span>
				msg.<span style="color: #0000FF;">From</span> <span style="color: #008000;">=</span> _mailTo<span style="color: #008000;">;</span>
&nbsp;
				<span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">Mail</span>.<span style="color: #0000FF;">SmtpMail</span>.<span style="color: #0000FF;">SmtpServer</span> <span style="color: #008000;">=</span> _mailServer<span style="color: #008000;">;</span>
				<span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">Mail</span>.<span style="color: #0000FF;">SmtpMail</span>.<span style="color: #0000FF;">Send</span><span style="color: #000000;">&#40;</span>msg<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Open a Visual Studio Command Prompt and run: <b>csc /t:library /r:system.web.dll,system.dll UnhandledExceptionModule.cs</b></p>
<p>Place the DLL in your bin folder</p>
<p>Add following to your web.config:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;httpModules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;WebMonitor.UnhandledExceptionModule, UnhandledExceptionModule&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;UnhandledExceptionModule&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/httpModules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here is sample code to trigger an unhandled exception</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
Timer t<span style="color: #008000;">;</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> btnTest_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, <span style="color: #000000;">System</span>.<span style="color: #0000FF;">EventArgs</span> e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Timer<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> TimerCallback<span style="color: #000000;">&#40;</span>t_Elapsed<span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">null</span>, <span style="color: #FF0000;">250</span>, Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> t_Elapsed<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> state<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;My unhandled exception&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/01/11/catching-unhandled-aspnet-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VSS Best Practices</title>
		<link>http://www.brianlow.com/index.php/2007/01/03/vss-best-practices/</link>
		<comments>http://www.brianlow.com/index.php/2007/01/03/vss-best-practices/#comments</comments>
		<pubDate>Wed, 03 Jan 2007 20:28:43 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[.Net]]></category>

		<guid isPermaLink="false">http://www.brianlow.com/index.php/2007/01/03/vss-best-practices/</guid>
		<description><![CDATA[Team Development with Visual Studio .NET and Visual SourceSafe
]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn2.microsoft.com/en-us/library/ms998239.aspx">Team Development with Visual Studio .NET and Visual SourceSafe</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlow.com/index.php/2007/01/03/vss-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
