12 May 2012

Creating a Centralized NuGet Packages Folder

I would imagine most people by now will have discovered and use the NuGet Visual Studio extension (http://www.nuget.org/). It is a great way of adding and maintaining 3rd party code libraries in your projects.

One problem I find though is that every time I create a new project or solution I end up installing the same old NuGet packages. By default NuGet downloads the source code for these packages into a new "packages" folder in your project/solution root directory. This means I have the same source code downloaded multiple times, in multiple locations.

This can easily be fixed as you can configure NuGet to change where it stores packages for your projects and solutions. Simply create a file called "nuget.config" and drop it in your project/solution root directory. In order to set where NuGet stores your project packages add the following settings element to the file.

(C:\NuGetPackages is a bad example; you would probably want to use a relative path)

I find it really useful to have all of my projects and solutions share a single NuGet package store as it saves having duplicate copies of packages littered around. It also helps if you don’t intend to keep the packages in source control as you can move your NuGet package store out of your source control folder structure. Don’t worry about different projects needing different versions of the same package. NuGet can maintain multiple versions of the same package so you can still upgrade certain projects and not others.

N.B. I find using a centralized NuGet package store works really well with projects that also enable NuGet Package Restore (http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages). It is quite easy to tweak to reflect the change in package store directory.

16 January 2012

Style Selected Text with CSS3

Loving some of the new things you can now do with CSS3. Recently discovered that you can add styling to text selected by the user. Begs the question where you could make use of this or in fact whether a user is ever likely to notice you have tweaked the styling for their benefit but hey...

Style Selected Text

Simply drag the cursor over the styled text and the background and color properties change.

This is now supported in the latest versions of all the major browsers: Internet Explorer, Opera, Firefox and Chrome.

26 September 2011

Using Log4Net in a Shared Hosting Environment

Another day and another shared web hosting issue. This time it's with Log4Net and its AdoNetAppender. The AdoNetAppender is used by Log4Net to append logging events to a table within a database. This is my preferred method for logging any live site errors within my web applications. However the latest release of Log4Net (1.2.10) will not allow the use of the AdoNetAppender within a medium trust environment. Fortunately after a lot of trawling through forums and blogs I finally stumbled onto a fix...

To demonstrate the fix let's run through the basic steps for adding Log4Net to a web project with an AdoNetAppender.

Download the latest release of Log4Net (http://logging.apache.org/log4net/download.html) and add a reference to the downloaded Log4Net release dll to your web project.

Add the relevant configuration settings to your web projects Web.config file for Log4Net and include an AdoNetAppender.

Inform the Log4Net library that it should be looking in the Web.config for its configuration settings by adding the following code to your Global.asax.

Finally somewhere in your application code try and log something.

In order to test this set up under shared hosting conditions you should also add the following config setting to your Web.config file to run the web application with medium trust privileges.

Now fire up the web application and you will see the following Log4Net error whenever it tries to log anything via the AdoNetAppender.

Visual Studio Output Window
So you can't use Log4Net with an AdoNetAppender in a shared hosting environment... well actually you can. This problem has actually been fixed in the latest Subversion branch of the Log4Net code but has yet to make it into a release. In order to get the latest branch code use an SVN client, such as TortoiseSVN (http://tortoisesvn.net/), to download the latest code from the Log4Net source repository: http://svn.apache.org/repos/asf/logging/log4net/trunk

Now replace the referenced Log4Net dll in your web project with the dll produced by building the latest branch code.

Re run your web application and hey presto, no Log4Net error. You should start to see rows appearing in your database log table.

Log Table Select Query