Sunday, December 1, 2013

Modifying NHibernate Connection String Value from MSBUILD file.

I am a big proponent of using Team City and MSBuild to create automated deployments in to Development, Testing and Staging environments.

I recently came across a problem with modifying a standard NHibernate connection string value using MSBuild and MSBuild Extension Pack, the MSBuild Extension Pack provides a collection of over 480 MSBuild Tasks, MSBuild Loggers and MSBuild Task Factories.

A typical NHibernate configuration setting looks like the following:


The section we want to change is

<property name="connection.connection_string">Data Source=FirstSample.sdf</property>
My first attempt was to use the XPath query of

/configuration/hibernate-configuration/session-factory/property[@name='connection.connection_string']
This was not matching the app.config XML which perplexed me at first until it dawned on me to use the namespace value of "urn:nhibernate-configuration-2.2".

The XMLFile class contains a namespace node


By setting the Namespaces value in the ItemGroup and then referencing it using @(Namespaces) in the Namespaces attribute of the XMLFile class I was able to get the desired result.

Saturday, September 7, 2013

Bootstrap 3 Upgrade

As a MVC developer I've been using Twitter Bootstrap for over a year to deliver responsive web designs. It gives me a massive head start on starting projects. By using Twitter Bootstrap I can reduce the Web Design cost for my clients to the point that we'll only engage a Web Designer for adding visual "icing to the cake" rather than building the site look and feel from the ground up. 

Recently Twitter Bootstrap v3.0.0 was released and if you've upgraded your project you'll noticed the layout no longer works. This is mainly in part to the new grid system. Instead of span1 you now have col-lg-1 and so on. The new (and 100% fluid) Bootstrap 3 grid now comes in 4 sizes. Tiny (for smartphones .col-xs-*), Small (for tablets .col-sm-*), Medium (for laptops .col-md-*) and Large (for laptops/desktops .col-lg-*). The 3 grid sizes enable you to control grid behavior on different devices (desktop, laptops, tablet, smartphone, etc..).

Now you can go through the official Bootstrap migration guide and manually make all the changes required or you can do as I did and use a tool. I've found that the Bootply Migration Tool to be an awesome starting point for the conversion. They'll be still a bit of manually editing but this tool will give you a good head start.

For instance copy and pasting the following HTML snippet into the tool:

Bootstrap 2 original mark up

Then hit "Convert to Bootstrap 3" and voilĂ .

Bootstrap 3 modified mark up

If you're a less addict like me you can Less mixins to migrate your grid. The ‘old’ span* will not be the same as col-* or col-lg-*. So replacing the class names in your templates will be give you better results. You will find that the col-lg-* class is in place of the former span* class. This will give you a possibility to create a mixin for span* with the same styles as col-lg-* in this case.

Sunday, May 12, 2013

Windows Service Logging and the Debug Process

I'm a big proponent of log4net for system logging. The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4 framework to the Microsoft .NET runtime. I especially like it's integration with NHibernate

This blog post is focusing in on logging in Windows Service. This is not a post about the setting up of log4net but about extending it's capabilities. There's plenty of posts on the interwebs about the actual configuration.

To give the reader some background, I am currently working on a project which requires integration points for the system. In short the system being built using Dynamics CRM needs to on a scheduled basis push and pull data from different systems. To accomplish this I've developed a Windows Service that allows for multiple jobs to be executed from it. I decided to use log4net for the logging interface. 

The issue I came across was during the debugging process. I generally debug a Windows Services on my local machine by using a Windows Form as a UI to start (or stop) the underlying service. By using the following code in the program.cs file:


Then I set whether to run the Windows Form UI by setting the command line argument for the start up project to /console as shown in the image below.

























Now when I press F5 the debug form is shown.



















The problem was I was always finding myself continuously running SQL queries during the debugging process to look at the log messages, not entirely productive. I figured there had to be a better approach. Through a bit of research I was able to find a decent approach to the problem. The solution was to use a custom AppenderSkeleton. The code is as shown below:


The is two key parts to the new CustomMemoryAppender class. Firstly, the overridden Append method. This method takes the logging event information and appends it to a StringBuilder variable called logBuffer. Secondly the public method ReadBuffer() this allows a external caller to access the current log message buffer information.

To hook this up in the Windows Form I need to override the Form OnLoad event and add some code:


The above code informs log4net to include the CustomMemoryAppender in the list of appenders the log4net with log to. It all tells log4net to log all logging events to the new appender.

Now when I press F5 I get the Debug form as well as all the logging messages. 

I've created a sample solution available at GitHub that uses a timer event to create log messages.

Tuesday, March 26, 2013

Delivering Chocolatey Goodness for Git Flow

I've been using Git Flow as a Git branching model for over 2 years on both personal and private projects. At my current job we are about to switch from SVN to Git, so we are going through a process of setting up the Developers machines for working with Git. 

One tool that can help the set of the machines is Chocolatey Nuget. Chocolatey is is a Machine Package Manager, somewhat like apt-get, but built with Windows in mind. It is set up on the same premise as Nuget both at their essence are package managers.

To set up Chocolatey open a command prompt and type 

This installs Chocolatey onto your machine.

Once this is done type the following commands at the command prompt. This installs Git, MsysGit, Posh Git and Git Flow to your machine.:

Now you have the tools to set up Git Flow on your machine. To complete the installation of Git Flow type the following into your command line:



If you get an error at the above step fix by copying the following files from the Git install path similar to C:\Users\USER_NAME\AppData\Local\GitHub\PortableGit_8810fd5c2c79c73adcc73fd0825f3b32fdb816e7.

NB: Replace PortableGit_8810fd5c2c79c73adcc73fd0825f3b32fdb816e7 with the name of your directory; you do not need the \bin at the end.

Git Flow Files












To C:\Program Files (x86)\Git\libexec\git-core (or similar)

Then you are ready to use git flow, read this article for more information about using git flow