<?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>Ivan Zlatev &#187; HowTo</title>
	<atom:link href="http://ivanz.com/tag/howto/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanz.com</link>
	<description></description>
	<lastBuildDate>Mon, 16 Jan 2012 16:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java BDD with JBehave and Watij in Eclipse with JUnit</title>
		<link>http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/</link>
		<comments>http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/#comments</comments>
		<pubDate>Wed, 25 May 2011 17:29:42 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=762</guid>
		<description><![CDATA[<a href="http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/" title="Java BDD with JBehave and Watij in Eclipse with JUnit"></a>The purpose of this post is to explain how to write BDD acceptance tests in Java using the gherkin syntax, run those with JUnit and implement them using Web Browser automation. Before I begin let me assure you that I &#8230;<p class="read-more"><a href="http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/" title="Java BDD with JBehave and Watij in Eclipse with JUnit"></a><p>The purpose of this post is to explain how to write BDD acceptance tests in Java using the gherkin syntax, run those with JUnit and implement them using Web Browser automation.</p>
<p>Before I begin let me assure you that I haven&#8217;t left the .NET church for the Java one and while I really don&#8217;t enjoy writing in Java and using Eclipse sometimes a man has to do what a man has to do.</p>
<p>So yeah I spend an afternoon trying to figure out the state of BDD (Behavior Driven Development) tools for Java and it&#8217;s pretty poor so far. If you want to write gherkin syntax based behavioral specifications, which are executable inside Eclipse as JUnit tests you are stuck only with <a href="http://jbehave.org/">JBehave</a>. JBehave however is a monster with poor documentation, not so easily discoverable fluent API and little to none third party documentation content on forums and sites. BTW don&#8217;t get fooled like me &#8211; insetad of getting the core package you want to get the Web package because the latter has all the .jar dependencies bundled unlike the first one. Enough bashing, lets get down to business.</p>
<p>Let&#8217;s say we have the following &#8220;Search&#8221; story and a more specific scenario &#8211; &#8220;display an error dialog if I don&#8217;t fill in the search form&#8221;:</p>
<pre class="brush: plain; title: ; notranslate">Narrative: I should be able to search

Scenario: I should see an error if I try to search without filling in the search form fields.

When I open 'http://mysite.com'
When I click on the 'Search' button
Then I should see an error</pre>
<p>First thing you will notice is that JBehave doesn&#8217;t support the &#8220;And&#8221; keyword so we can&#8217;t wring &#8220;When I open &#8230; AND I click on the Seach button&#8221;.</p>
<p>Similar to other gherkin/cucumber based frameworks JBehave allows us to implement each step of the scenario as a method in a class, which optionally takes parameters mapped to words from the scenario. The above one can be stubbed like bellow. Also not that if properly configured you can make JBehave junit runner spit out the stubs for you to copy paste in your code (more about that later):</p>
<pre class="brush: java; title: ; notranslate">
public class Search
{
	@When(&quot;I open '$page'&quot;)
	public void whenIOpen(String page)
	{
		// TODO
	}

	@When(&quot;I click on the '$buttonText' button&quot;)
	public void whenIClickOnTheButton(String buttonText)
	{
		// TODO
	}

	@Then(&quot;I should see an error&quot;)
	public void thenIShouldSeeAnError(){
		// TODO
	}
}</pre>
<p>To implement the browser automation in Java there exist a Java variant of the Ruby watir library (and the .NET Watin library) called &#8230; surprise&#8230; watiJ. It&#8217;s available <a href="http://watij.com/">here</a>. Major pitfall with it though is that it actually embeds the IE COM component and mozilla&#8217;s ghecko engine instead launching the real IE/Firefox (like the above mentioned framework do) and it only supports IE6 and I saw some C++ runtime problems with the ghecko engine. Nevertheless to give you a feel of how the above tests can be implemented:</p>
<pre class="brush: java; title: ; notranslate">
public class Search extends StoryBase {

	@When(&quot;I open '$page'&quot;)
	public void whenIOpen(String page)
	{
		WebBrowser.open(page).pauseUntilReady();
	}

	@When(&quot;I click on the '$buttonText' button&quot;)
	public void whenIClickOnTheButton(String buttonText)
	{
		Tag button = WebBrowser.find.button().with.name(&quot;search&quot;);
		assertNotNull(button);
		button.click().pauseUntilReady();
	}

	@Then(&quot;I should see an error&quot;)
	public void thenIShouldSeeAnError(){
		Tag errorDialogTitleDiv = WebBrowser.find.span().with.className(&quot;dijitDialogTitle&quot;);
		assertNotNull(errorDialogTitleDiv);
		assertEquals(&quot;Error&quot;, errorDialogTitleDiv.get.innerHTML());
	}
}</pre>
<p>Now, how about running them? A few things before that:</p>
<ul>
<li>JBehave expects you to save stories as .story files in your source code tree</li>
<li>JBehave expects you to implement and configure problematically a <em>JUnitStory </em>for each story, so that it&#8217;s runnable through JUnit.</li>
<li>It will take your JUnitStory subclass implementation &#8211; for example &#8220;<em>HelloWorldStory extends JUnitStory&#8221; </em>and look for the plain text file <em>hello_world_story.story</em></li>
</ul>
<p>To save myself from having to configure each story  I present you my <em>StoryBase </em>base class, which:</p>
<ul>
<li>Initializes WatiJ and exposes it to subclasses</li>
<li>Configures JBehave to look for the .story files in the same directory as the class.</li>
<li>Configures JBehave/JUnit to look for the steps test methods of a story scenario in the subclass.</li>
<li>Configures JBehave to print out detailed information in the Console whilst running (such as the method stubs if any are missing)</li>
<li>Configures JBehave to spit out HTML report in <em>myproject/target/jbehave-report/</em></li>
</ul>
<p>Here it is:</p>
<pre class="brush: java; title: ; notranslate">
public abstract class StoryBase extends JUnitStory {
    protected final static WebSpec WebBrowser = new WebSpec().ie();

    @Override
    public Configuration configuration() {
        return new MostUsefulConfiguration()
            .useStoryLoader(new LoadFromClasspath(this.getClass().getClassLoader()))
    	    .useStoryReporterBuilder(
    	    		new StoryReporterBuilder()
    	    			.withDefaultFormats()
    	    			.withFormats(Format.HTML, Format.CONSOLE)
    	    			.withRelativeDirectory(&quot;jbehave-report&quot;)
			);
    }

    @Override
    public List candidateSteps() {
        return new InstanceStepsFactory(configuration(), this).createCandidateSteps();
    }
}
</pre>
<p>Then all that&#8217;s left is to make the Search class extend the StoryBase class and it is now runnable in JUnit inside and outside Eclipse.</p>
<pre class="brush: java; title: ; notranslate">public class Search extends StoryBase {
}</pre>
<p><a href="http://ivanz.com/wp-content/uploads/2011/05/Untitled.png" rel="shadowbox[sbpost-762];player=img;"><img class="aligncenter size-full wp-image-764" title="Project Tree" src="http://ivanz.com/wp-content/uploads/2011/05/Untitled.png" alt="" width="178" height="108" /></a><br />
<a href="http://ivanz.com/wp-content/uploads/2011/05/junit.png" rel="shadowbox[sbpost-762];player=img;"><img class="aligncenter size-full wp-image-763" title="junit" src="http://ivanz.com/wp-content/uploads/2011/05/junit.png" alt="" width="649" height="85" /></a></p>
<p>&nbsp;</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=762" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2011/05/25/java-bdd-with-jbehave-and-watij-in-eclipse-with-junit/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Client-Side XSLT Transformations with JavaScript</title>
		<link>http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/</link>
		<comments>http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/#comments</comments>
		<pubDate>Thu, 12 May 2011 20:10:35 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Dojo]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=758</guid>
		<description><![CDATA[<a href="http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/" title="Client-Side XSLT Transformations with JavaScript"></a>I bet you haven&#8217;t ever thought of doing XSLT transformations inside the Web Browser on the client-side, but anyway it&#8217;s absolutely possible (cross-browser) and it performs pretty well as well, which shouldn&#8217;t be a big surprise since the browser is &#8230;<p class="read-more"><a href="http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/" title="Client-Side XSLT Transformations with JavaScript"></a><p>I bet you haven&#8217;t ever thought of doing XSLT transformations inside the Web Browser on the client-side, but anyway it&#8217;s absolutely possible (cross-browser) and it performs pretty well as well, which shouldn&#8217;t be a big surprise since the browser is optimized to work with markup.</p>
<p>Real-life practical applications of this however I haven&#8217;t thought of yet, but possibly:</p>
<ol>
<li>Use XSLT to consume old XML-based web services and convert the XML into JSON on the client side on the fly. Come on, I know you want to! Why do it properly when you can do it in JavaScript?</li>
<li>Create a JavaScript-only  CMS that loads Word documents directly and uses XSLT to display HTML (.docx is zipped xml). Again, why do it properly when you can do it in JavaScript?</li>
<li>For fun and zero profit!</li>
</ol>
<p>Here is the XSLT transformer JavaScript class and sample. The class uses the Dojo JavaScript framework, but only for the browser checks and AJAX requests, so you can easily replace those with JQuery if you wish. Take it under the MIT/X11 license. There is also a <a href="http://ivanz.com/files/demos/js-xslt/demo.html">useless demo here</a>.</p>
<p><em>Sample</em>:</p>
<pre class="brush: jscript; title: ; notranslate">
// to transform
var xslTransform = new XslTransform(&quot;/path/to/sample.xsl&quot;);
var outputText = xslTransform.transform(&quot;/path/to/sample.xml&quot;);
</pre>
<p><em>XslTransform.js</em>:</p>
<pre class="brush: jscript; title: ; notranslate">
dojo.provide(&quot;XslTransform&quot;);

dojo.declare(&quot;XslTransform&quot;, [],
{
	_xslDoc : null,
	_xslPath : null,

	constructor : function(xslPath) {
		this._xslPath = xslPath;
	},

	transform : function(xmlPath) {
		if (this._xslDoc === null)
			this._xslDoc = this._loadXML(this._xslPath);

		var result = null;
		var xmlDoc = this._loadXML(xmlPath);

		if (dojo.isIE) {
			result = xmlDoc.transformNode(this._xslDoc);
		} else if(typeof XSLTProcessor !== undefined) {
			xsltProcessor = new XSLTProcessor();
	  		xsltProcessor.importStylesheet(this._xslDoc);

	  		var ownerDocument = document.implementation.createDocument(&quot;&quot;, &quot;&quot;, null);
	  		result = xsltProcessor.transformToFragment(xmlDoc, ownerDocument);
		} else {
			alert(&quot;Your browser doesn't support XSLT!&quot;);
		}

		return result;

	},

	createXMLDocument : function(xmlText) {
		var xmlDoc = null;

		if (dojo.isIE) {
			xmlDoc = new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
			xmlDoc.async = false;
			xmlDoc.loadXML(xmlText);
		} else if (window.DOMParser) {
			parser = new DOMParser();
			xmlDoc = parser.parseFromString(xmlText, &quot;text/xml&quot;);
		} else {
			alert(&quot;Your browser doesn't suppoprt XML parsing!&quot;);
		}

		return xmlDoc;
	},

	// Synchronously loads a remote xml file
	_loadXML : function(xmlPath) {
		var getResult = dojo.xhrGet({
			url : xmlPath,
			handleAs : &quot;xml&quot;,
			sync: true
		});

		var xml = null;
		// Returns immediately, because the GET is synchronous.
		var xmlData = getResult.then(function (response) {
			xml = response;
		});

		return xml;
	}
});
</pre>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=758" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2011/05/12/client-side-xslt-transformations-with-javascript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Logging JavaScript Errors with ELMAH in ASP.NET MVC</title>
		<link>http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/</link>
		<comments>http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/#comments</comments>
		<pubDate>Sun, 08 May 2011 19:27:14 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=753</guid>
		<description><![CDATA[<a href="http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/" title="Logging JavaScript Errors with ELMAH in ASP.NET MVC"></a>One final piece of information I wanted to add to my last blog post about setting up ELMAH for ASP.NET MVC is how to log Client Side/JavaScript errors with ELMAH. To do that we need to: 1. Because ELMAH works &#8230;<p class="read-more"><a href="http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/" title="Logging JavaScript Errors with ELMAH in ASP.NET MVC"></a><p>One final piece of information I wanted to add to <a title="ASP.NET MVC Magical Error Logging with ELMAH" href="http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/">my last blog post about setting up ELMAH for ASP.NET MVC</a> is how to log Client Side/JavaScript errors with <a href="http://code.google.com/p/elmah/">ELMAH</a>.</p>
<p>To do that we need to:</p>
<p>1. Because ELMAH works with exceptions we will need to define a <em>JavaScriptErrorException </em>to represent our client side errors in the logs:</p>
<pre class="brush: csharp; title: ; notranslate">using System;

namespace Triply.Extensions
{
	[Serializable]
	public class JavaScriptErrorException : Exception
	{
		public JavaScriptErrorException (string message) : base(message)
		{
		}
	}
}</pre>
<p>2. Define a Controller Action to be invoked by the client side that takes in an error message:</p>
<pre class="brush: csharp; title: ; notranslate">using System;
using System.Web.Mvc;
using Elmah;
using Triply.Extensions;

namespace Triply.Controllers
{
	public class HomeController : Controller
	{
		[HttpPost]
		public void LogJavaScriptError(string message)
		{
			ErrorSignal.FromCurrentContext().Raise(new JavaScriptErrorException(message));
		}
	}
}</pre>
<p>3. Use an AJAX post with a message to log the error</p>
<pre class="brush: jscript; title: ; notranslate">
function logError(details) {
    $.post(&quot;/Home/LogJavaScriptError&quot;, { message: details });
}
</pre>
<p>Done!</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=753" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2011/05/08/logging-javascript-errors-with-elmah/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC Magical Error Logging with ELMAH</title>
		<link>http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/</link>
		<comments>http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/#comments</comments>
		<pubDate>Sun, 08 May 2011 19:09:05 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=749</guid>
		<description><![CDATA[<a href="http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/" title="ASP.NET MVC Magical Error Logging with ELMAH"></a>If you aren&#8217;t using ELMAH (Error Logging Modules and Handlers) you should be - http://code.google.com/p/elmah/ It&#8217;s the best thing since sliced bread for logging ASP.NET errors with practically zero effort. It basically plugs into the ASP.NET pipeline and logs all unhandled exceptions thrown and &#8230;<p class="read-more"><a href="http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/" title="ASP.NET MVC Magical Error Logging with ELMAH"></a><p>If you aren&#8217;t using ELMAH (Error Logging Modules and Handlers) you should be - <a href="http://code.google.com/p/elmah/">http://code.google.com/p/elmah/</a> It&#8217;s the best thing since sliced bread for logging ASP.NET errors with practically zero effort. It basically plugs into the ASP.NET pipeline and logs all unhandled exceptions thrown and HTTP error codes together with all sorts of other useful information such as request url, stacktrace, current user username, cookies and more. You can say it makes a snapshot of the Yellow Screen of Death with extra information. It then gives you a simple web access to the recent errors logged (by default at http//mysite.com/elmah.axd), exposing a RSS feed as well. It&#8217;s fully configurable (error filters, multiple backend log storage systems support, etc) and also can be set up to send emails on error.</p>
<p>Some screenshots:</p>
<div class="wp-caption aligncenter" style="width: 310px"><a href="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-1.png" rel="shadowbox[sbpost-749];player=img;"><img title="elmah-log-1" src="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-1-300x43.png" alt="" width="300" height="43" /></a><p class="wp-caption-text">List of Errors using the Web Interface</p></div>
<div class="wp-caption aligncenter" style="width: 310px"><a href="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-2.png" rel="shadowbox[sbpost-749];player=img;"><img title="elmah-log-2" src="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-2-300x186.png" alt="" width="300" height="186" /></a><p class="wp-caption-text">Error Details in the web interface</p></div>
<p><a href="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-1.png" rel="shadowbox[sbpost-749];player=img;"></a></p>
<p>You can pull ELMAH through nuget via the &#8220;Elmah&#8221; package., which will download it and add a reference. The steps to get it all set up for ASP.NET MVC are as follows.</p>
<p>1. Register Elmah configuration sections in the <em>Web.config</em></p>
<pre class="brush: xml; title: ; notranslate">    &lt;sectionGroup name=&quot;elmah&quot;&gt;
      &lt;section name=&quot;security&quot; requirePermission=&quot;false&quot; type=&quot;Elmah.SecuritySectionHandler, Elmah&quot; /&gt;
      &lt;section name=&quot;errorLog&quot; requirePermission=&quot;false&quot; type=&quot;Elmah.ErrorLogSectionHandler, Elmah&quot; /&gt;
      &lt;section name=&quot;errorMail&quot; requirePermission=&quot;false&quot; type=&quot;Elmah.ErrorMailSectionHandler, Elmah&quot; /&gt;
      &lt;section name=&quot;errorFilter&quot; requirePermission=&quot;false&quot; type=&quot;Elmah.ErrorFilterSectionHandler, Elmah&quot; /&gt;
    &lt;/sectionGroup&gt;</pre>
<p>2. Add the root level ELMAH section in <em>Web.config</em> to tell it where to store the logs and how. Easiest to set up is to have a directory where it stores xml file for each error, <strong>but make sure the IIS process/AppPool has write access to that directory!</strong> Also I have added a filter to ignore all HTTP 404 errors. Note that this filter won&#8217;t work for ASP.NET MVC and I will come back to that later.</p>
<pre class="brush: xml; title: ; notranslate">&lt;elmah&gt;
    &lt;security allowRemoteAccess=&quot;1&quot; /&gt;
    &lt;errorLog type=&quot;Elmah.XmlFileErrorLog, Elmah&quot; logPath=&quot;~/App_Data/ElmahLogs&quot; /&gt;
    &lt;errorFilter&gt;
      &lt;test&gt;
        &lt;equal binding=&quot;HttpStatusCode&quot; value=&quot;404&quot; type=&quot;Int32&quot; /&gt;
      &lt;/test&gt;
    &lt;/errorFilter&gt;
  &lt;/elmah&gt;</pre>
<p>3. Register ELMAH with the ASP.NET pipeline <strong>both</strong> in the <strong>system.web </strong>section (when running locally) and the  <strong>system.webServer</strong> section when deployed to IIS. Just copy paste the below snippet in both sections. Notice how the default configuration says that ELMAH will be available at mysite.com/elmah.axd . You can change that if you want (in both places!)</p>
<pre class="brush: xml; title: ; notranslate">    &lt;httpModules&gt;
      &lt;add name=&quot;ErrorLog&quot; type=&quot;Elmah.ErrorLogModule, Elmah&quot; /&gt;
    &lt;/httpModules&gt;
    &lt;httpHandlers&gt;
      &lt;add verb=&quot;POST,GET,HEAD&quot; path=&quot;elmah.axd&quot; type=&quot;Elmah.ErrorLogPageFactory, Elmah&quot; /&gt;
    &lt;/httpHandlers&gt;</pre>
<p>You are done and elmah will be available at http://mysite.com/elmah.axd , but it won&#8217;t work quite right with ASP.NET MVC, because of the way errors are handled there, so there are those additional things that need to be set up:</p>
<p>4. Log exceptions before ASP.NET MVC swallows them if using customErrors or some other code handles and swallows the error. To do this I&#8217;ve implemented the below exception filter that logs all handled exceptions. It&#8217;s a good example also of how to programatically log errors with ELMAH:</p>
<pre class="brush: csharp; title: ; notranslate">using System;
using System.Web.Mvc;
using Elmah;

namespace Triply.Extensions
{
	public class ElmahHandledErrorLoggerFilter : IExceptionFilter
	{
		public void OnException (ExceptionContext context)
		{
			// Long only handled exceptions, because all other will be caught by ELMAH anyway.
			if (context.ExceptionHandled)
				ErrorSignal.FromCurrentContext().Raise(context.Exception);
		}
	}
}</pre>
<p>To register it add it in <em>Global.asax.cs</em>. The ordering is important &#8211; <strong>add it before the HandleErrorAttribute is registered</strong>.</p>
<pre class="brush: csharp; title: ; notranslate">public static void RegisterGlobalFilters (GlobalFilterCollection filters)
{
	filters.Add(new ElmahHandledErrorLoggerFilter());
	filters.Add(new HandleErrorAttribute());
}</pre>
<p>5. One final note regarding filtering out HTTP 404 errors. In ASP.NET MVC they are raised as exceptions so if you want to ignore them unfortunately you can&#8217;t do that declaratively in the Web.config. Instead you have to add the following elmah hooks to your <em>Global.asax.cs</em> to do it programatically:</p>
<pre class="brush: csharp; title: ; notranslate">		// ELMAH Filtering
		protected void ErrorLog_Filtering (object sender, ExceptionFilterEventArgs e)
		{
			FilterError404(e);
		}

		protected void ErrorMail_Filtering (object sender, ExceptionFilterEventArgs e)
		{
			FilterError404(e);
		}

		// Dismiss 404 errors for ELMAH
		private void FilterError404 (ExceptionFilterEventArgs e)
		{
			if (e.Exception.GetBaseException() is HttpException) {
				HttpException ex = (HttpException)e.Exception.GetBaseException();
				if (ex.GetHttpCode() == 404)
					e.Dismiss();
			}
		}</pre>
<p>That&#8217;s it. Now you get super cool logging (and remember &#8211; RSS feed!)</p>
<p><a href="http://ivanz.com/wp-content/uploads/2011/05/elmah-log-1.png" rel="shadowbox[sbpost-749];player=img;"><br />
</a></p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=749" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2011/05/08/asp-net-mvc-magical-error-logging-with-elmah/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Storing Date and Time in UTC and the definition &#8220;Today&#8221;</title>
		<link>http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/</link>
		<comments>http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 22:25:25 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=732</guid>
		<description><![CDATA[<a href="http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/" title="Storing Date and Time in UTC and the definition &quot;Today&quot;"></a>Storing date and times in UTC on the server side is all nice and dandy, however there are a few gotchas, one of which the definition of &#8220;Today&#8221; when you a dealing with time zones. Say we are dealing with &#8230;<p class="read-more"><a href="http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/" title="Storing Date and Time in UTC and the definition &quot;Today&quot;"></a><p>Storing date and times in UTC on the server side is all nice and dandy, however there are a few gotchas, one of which the definition of &#8220;Today&#8221; when you a dealing with time zones.</p>
<p>Say we are dealing with a system where a user can keep a history/log of all the food that he eats along with the date and time of when that happened. Current system times are:</p>
<blockquote><p>Current User Time (UTC+2): 4 April 01:45<br />
Current Server time (UTC): 3 April 23:45</p></blockquote>
<p>and we have two log entries:</p>
<blockquote><p>Log Entry User local time (UTC+2): 3 April 19:30<br />
Log Entry Server UTC time: 3 April 17:30</p></blockquote>
<blockquote><p>Log Entry User local time (UTC+2): 4 April 01:30<br />
Log Entry Server UTC time: 3 April 23:30</p></blockquote>
<p>If a user wants to see all the log entries for &#8220;Today&#8221; his perspective for today is 4 April, but if we use the current UTC date on the server (3 April) to do the query we are going to end up with two results, but only one of them (the second) is for the 4 April. This makes perfect sense on the server side, because from the server end perspective both of those log entries were logged on the 3rd, but that&#8217;s not the case with the user.</p>
<p>To solve this we need to calculate the start of user today day and end of user today day and use it as a range to query the log history.</p>
<p>The steps are more or less:</p>
<p>1. Get the current server time and convert it to user time.</p>
<p>2. &#8220;Rewind&#8221; that user time back to midnight (00:00)</p>
<p>3. Convert that to back UTC and you get the start of day for the user in server terms.</p>
<p>4. Add 23 hours, 59 minutes, 59 seconds to the above and you have the end of user day in server terms</p>
<p>5. Query for user_start_of_day &lt;= logentry.Date &lt;= user_end_of_day</p>
<p>A worked example for the above dates and times:</p>
<blockquote><p>Current Server time (UTC): 3 April 23:45</p></blockquote>
<p>1. Server time UtcNow is 3 April 23:45 and the user is in UTC+2, so the user local time is 4 April 01:45</p>
<p>2. This gives start of day for the user local time at 4 April 00:00</p>
<p>3. Which is 3 April 22:00 in server UTC time &#8211; the user_start_of_day</p>
<p>4. + 23:59:59 gives us 4 April 21:59:59 as the end of day</p>
<p>5. We query for log entries where the log UTC timestamp is between 3 April 22:00 and 4 April 21:59:59 , which will return only one result in the above case, which is correct.</p>
<p>Here is also a C# TimeZone class I have for each of my users in one of my toy projects:</p>
<pre class="brush: csharp; title: ; notranslate">public class TimeZone : Entity
{
    protected TimeZone ()
    {
    }

    public TimeZone (string name, TimeZoneInfo timeZone)
    {
        if (timeZone == null)
            throw new ArgumentNullException (&quot;timeZone&quot;, &quot;timeZone is null.&quot;);
        if (String.IsNullOrEmpty (name))
            throw new ArgumentException (&quot;name is null or empty.&quot;, &quot;name&quot;);

        Name = name;
        this.TimeZoneInfo = timeZone;
    }

    public virtual string Name { get; set; }
    public virtual TimeZoneInfo TimeZoneInfo { get; set; }

    public virtual DateTime StartOfToday {
        get {
            DateTime serverNow = DateTime.UtcNow;
            DateTime userNow = ToLocalTime(serverNow);
            return ToUniversalTime (userNow.Date);
        }
    }

    public virtual DateTime EndOfToday {
        get { return StartOfToday.Add (new TimeSpan (23, 59, 59)); }
    }

    public virtual DateTime ToLocalTime (DateTime utcTime)
    {
        return TimeZoneInfo.ConvertTimeFromUtc (utcTime, this.TimeZoneInfo);
    }

    public virtual DateTime ToUniversalTime (DateTime localTime)
    {
        if (localTime.Kind == DateTimeKind.Utc)
            return localTime;

        return TimeZoneInfo.ConvertTimeToUtc (localTime, this.TimeZoneInfo);
    }
}</pre>
<p>P.S: Some of you will spot a further implication of this and that is that we must always store the time as well as the date.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=732" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2011/04/04/storing-date-and-time-in-utc-and-the-definition-today/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to import a database schema into SQL Server Express with Visual Studio</title>
		<link>http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/</link>
		<comments>http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/#comments</comments>
		<pubDate>Wed, 22 Sep 2010 11:32:50 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=723</guid>
		<description><![CDATA[<a href="http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/" title="How to import a database schema into SQL Server Express with Visual Studio"></a>Today I needed to import my SQL Server schema into a SQL Server Express instance on a different machine. I only have Visual Studio on this machine and no SQL Server Management Studio installed, so it took me a bit &#8230;<p class="read-more"><a href="http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/" title="How to import a database schema into SQL Server Express with Visual Studio"></a><p>Today I needed to import my SQL Server schema into a SQL Server Express instance on a different machine. I only have Visual Studio on this machine and no SQL Server Management Studio installed, so it took me a bit to figure out how to import the schema, so here it goes.</p>
<ol>
<li>Add the SQL Server Express instance to the Server Explorer. It&#8217;s located at &#8220;.\SQLEXPRESS&#8221;.</li>
<li>Open the T-SQL database schema. It will say &#8220;not connected&#8221; at the end of the tab caption.</li>
<li>In the &#8220;Data&#8221; menu you will have to &#8220;Connect&#8221; and then Validate and Execute SQL.</li>
<li>Done.</li>
</ol>
<p><a href="http://ivanz.com/wp-content/uploads/2010/09/connect.png" rel="shadowbox[sbpost-723];player=img;"><img class="aligncenter size-full wp-image-724" title="connect" src="http://ivanz.com/wp-content/uploads/2010/09/connect.png" alt="" width="694" height="335" /></a></p>
<p>I bet the same can be done with <a href="http://msdn.microsoft.com/en-us/library/ms365247.aspx" target="_blank">SQL Server Management Studio Express</a> but I have yet to install it.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=723" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2010/09/22/how-to-import-a-database-schema-into-sql-server-express-with-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HowTo Disable the Unsigned Security Warnings on Windows Mobile</title>
		<link>http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/</link>
		<comments>http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 20:54:05 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://i-nz.net/?p=674</guid>
		<description><![CDATA[<a href="http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/" title="HowTo Disable the Unsigned Security Warnings on Windows Mobile"></a>The security warnings when deploying unsigned applications from Visual Studio to a real Windows Mobile device can get very annoying. Thankfully they can be disabled with a hack and here is how. Using the Remote Registry Editor supplied with Visual &#8230;<p class="read-more"><a href="http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/" title="HowTo Disable the Unsigned Security Warnings on Windows Mobile"></a><p>The security warnings when deploying unsigned applications from Visual Studio to a real Windows Mobile device can get very annoying. Thankfully they can be disabled with a hack and here is how.</p>
<p>Using the Remote Registry Editor supplied with Visual Studio and with the Windows Mobile SDK installed change the following key value from 0 to 1 (and vise-versa to reset the change)</p>
<pre><code>HKEY_LOCAL_MACHINE\Security\Policies\Policies\0000101a = 1</code></pre>
<p>That&#8217;s it.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=674" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2009/08/15/howto-disable-the-unsigned-security-warnings-on-windows-mobile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Network Connections at the Same Time on Windows</title>
		<link>http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/</link>
		<comments>http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 02:32:00 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://i-nz.net/?p=598</guid>
		<description><![CDATA[<a href="http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/" title="Multiple Network Connections at the Same Time on Windows"></a>In my scenario I have one Wireless connection for my Internet and one LAN connection to a small private network of my own with my NAS, PS3 and TV and I want to have them both at the same time. &#8230;<p class="read-more"><a href="http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/" title="Multiple Network Connections at the Same Time on Windows"></a><p>In my scenario I have one Wireless connection for my Internet and one LAN connection to a small private network of my own with my NAS, PS3 and TV and I want to have them both at the same time. It was a major pain to get this setup working on Windows. When I had both connections enabled my Internet wasn&#8217;t working because Windows was routing through the LAN connection even though the Wireless connection had a higher priority set in <em>Network Connections</em> &#8211;&gt; <em>Advanced</em> menu &#8211;&gt; <em>Advanced settings.</em></p>
<p>The solution is to go into the <em>Properties </em>of each connection (right click on it) &#8211;&gt; select <em>Internet Protocol (TCP/IP) </em>&#8211;&gt; click <em>Properties </em>-&gt; click <em>Advanced </em>&#8211;&gt; uncheck <em>Automatic metric </em>in the bottom and set a number between 1 and 9999 where the smaller the number the higher the connection priority. I have set my Wireless Internet connection to 1 and my LAN connection to 9999 and that works.</p>
<p>I hope this post will save someone else&#8217;s precious time in the future.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=598" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2009/07/08/multiple-network-connections-at-the-same-time-on-windows/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Resize a VirtualBox Virtual Disk HowTo</title>
		<link>http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/</link>
		<comments>http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 21:58:42 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false">http://i-nz.net/?p=498</guid>
		<description><![CDATA[<a href="http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/" title="Resize a VirtualBox Virtual Disk HowTo"></a>It is not possible to increase the maximum virtual size of a VirtualBox VDI HDD once it is created. The only way I found to do it is to create a new larger VirtualBox HDD and use GParted to copy &#8230;<p class="read-more"><a href="http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/" title="Resize a VirtualBox Virtual Disk HowTo"></a><p>It is not possible to increase the maximum virtual size of a VirtualBox VDI HDD once it is created. The only way I found to do it is to create a new larger VirtualBox HDD and use GParted to copy the old one to the new one. The steps to do that are:</p>
<p>1. Create a new VirtualBox HDD with the desired size and add it as a secondary HDD (Primary Slave) in the Virtual Machine HDD settings.</p>
<p>2. Download the GParted LiveCD and boot from it inside the Guest &#8211; <a href="http://gparted.sourceforge.net/">http://gparted.sourceforge.net/</a></p>
<p>3. Right click on the partitions of the original HDD in GParted and click &#8220;<em>Copy</em>&#8220;. Select the second HDD and &#8220;Paste&#8221;.  This will ask to create a &#8220;<em>msdos</em>&#8221; partition table first so click <em>&#8220;OK&#8221; </em>and &#8220;<em>Paste</em>&#8221; again. Adjust partition sizes as you wish and hit &#8220;<em>Apply</em>&#8220;.</p>
<p>4. Mark the new partition as bootable in right click -&gt; &#8220;<em>Manage Flags</em>&#8221; -&gt; tick &#8220;<em>boot</em>&#8220;. Once done shutdown the VM and release the gparted Live CD.</p>
<p>5. Because the HDD characteristics have changed you might not be able to boot at this point. Insert the Windows/Linux OS installation CD/DVD and run in repair mode. For Windows this would be the recovery console and the &#8220;<em>fixboot c:</em>&#8221; or fixmbr commands. On Linux just reinstall the boot loader.</p>
<p>That&#8217;s it. It&#8217;s not very convenient doing this but at least it works.</p>
<p>P.S: If you get an error &#8220;FATAL: INT18 Boot Failure&#8221; you have forgotten to mark the new partition as bootable.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=498" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2009/04/30/resize-a-virtualbox-virtual-disk-howto/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Git automatic smart ChangeLog merging</title>
		<link>http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/</link>
		<comments>http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 21:29:36 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://i-nz.net/?p=323</guid>
		<description><![CDATA[<a href="http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/" title="Git automatic smart ChangeLog merging"></a>This might be old news for some, but there is a merge driver for Git that can automatically perform smart merges of ChangeLogs. I&#8217;ve used it for months now and it has always worked flawlessly. 1) Install git-merge-changelog which is &#8230;<p class="read-more"><a href="http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/" title="Git automatic smart ChangeLog merging"></a><p>This might be old news for some, but there is a merge driver for Git that can automatically perform smart merges of ChangeLogs. I&#8217;ve used it for months now and it has always worked flawlessly.</p>
<p>1) Install <em>git-merge-changelog</em> which is part of <a href="http://www.gnu.org/software/gnulib/"><em>gnulib</em></a> . For openSUSE you can use my repository at <a href="http://download.opensuse.org/repositories/home:/i-nZ/">http://download.opensuse.org/repositories/home:/i-nZ/</a></p>
<p>2) Make Git aware of it by adding the following to your <em>~/.gitconfig</em></p>
<pre>[merge "merge-changelog"]
name = GNU-style ChangeLog merge driver
driver = /usr/bin/git-merge-changelog %O %A %B</pre>
<p>3) For each repository where you want to use this driver make Git aware of it by adding to <em>$GIT_REPO/.git/info/attributes</em> the following (create the file if it doesn&#8217;t exist):</p>
<pre>ChangeLog    merge=merge-changelog</pre>
<p>That&#8217;s it! Enjoy.</p>
 <img src="http://ivanz.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=323" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2009/03/19/git-automatic-smart-changelog-merging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

