.NET Health Monitoring with Elmah

Elmah (Error Logging Modules and Handlers for ASP.NET) is a Google library that you can find at its dedicated web site: Elmah - Error Logging Modules and Handlers for ASP.NET.

This library is used for Health Monitoring and allows to trace unhandled exceptions, logging them and, optionally, viewing them in a dedicated page.


This library is that it is implemented with a custom HttpModule, as described in the article Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components.

In this way, the installation doesn't require any code recompilation, as it is enough to add copy DLLs to the bin folder and add some web.config entries.


Finally, the last article ELMAH (Error Logging Modules and Handlers) with SQL Server Compact explains how to install and configure ELMAH with the Local SQL.

Deploy DLL and admin rights

Sometimes, when working with corporate machines, it can happen that you have security issues that make your SharePoint development harder.

One of these problems is that even if you are machine administrator, or you have started Visual Studio as administrator, you still have problems with deploies, or you have DLL locked in the GAC and you are not able to update/delete them.

The solution to this problem is to remove the admin approval from the local policies:

  • run the Local Security Policy MMC snap-in (from the start menu, run, type Local);
  • go to Security Settings | Local Policies | Security Options
  • disable "User Account Control: Run all administrators in Admin Approval"
  • reboot the machine

I've seen this trick in the blog post Access Denied trying to uninstall assemblies from GAC.

async and await keywords in C# 5

The new version of the C# language, the version 5, introduces native support to async programming.

Async programming has always been available since the first version of .NET and C#, but now the development is really simplified, because the compiler is taking care of all the burden to deal with it.

To understand this semplification, and explain the support that the compiler is giving to the developer, we can summarize in the following points:

  • when you need to execute a lenghty operation, use the await keyword;
  • mark methods with await keywords with the async keyword;
  • logically, what the compiler will do is:
    1. generate the first part of the method up to the async call;
    2. the async call will be done passing as callback another "hidden" method,
    3. that will be defined with the remaining of the body content, and will be able to consume the output of the async call;
  • pratically, this construction makes use of the Task<T> class, where T is the type of the output of the async call;
  • one final note: in a async method, multiple await are possible.

As reference, there are the following articles:

Lazy class in .NET 4

The concept of lazy is simple: sometimes creating a new instance of a class could be time-consuming, and you don't always need the full initialization of the class, but only a few members of it. So here we have the conpect of lazy construction: create or object, but complete the initialization only when all the fields are needed.

In previous versions of the .NET framework you had to implement this pattern by hand, but the .NET 4 introduces the Lazy class. The concept to build a lazy instances are simple:

  • define a private property containing the real value of the property you want to save (and don't initialize it), let's suppose you call it MyPropertyPrivate;
  • define your internal Lazy<T> property (let's say: call it MyPropertyLazy);
  • initialize the previous MyPropertyLazy as new Lazy<T>(() => { give a value to MyPropertyPrivate });
  • define a public property of type T, let's say called MyProperty, with a get accessor returning MyPropertyLazy.Value.

So in the end what happens is:

  1. a user access the public property MyProperty;
  2. MyProperty returns its value from MyPropertyLazy.Value;
  3. once "touched", MyProperty.Value evaluates the lambda expression,
  4. that finally executes and gives a value to MyPropertyPrivate.

Parallel programming in .NET 4 and custom TaskScheduler

.NET 4 introduces the concept of Parallel Programming. To simplify drammatically, its main concepts are:

  • Task: higher abstraction of Thread; conceptually, a thread is a way of accomplishing a task. Above all, you can cancel a Task (and its implementation should cancel the inner thread);
  • PLINQ: evolution of LINQ to allow parallel execution of LINQ queries.

You can get an introduction to the news of .NET parallel programming in the following two articles:

In my case I needed to create a custom TaskScheduler to handle priorities (like in Threads) for a custom queue management system. So, I got help from the following two articles:

ActiveDirectory query examples

If you need to query the ActiveDirectory, you have two ways:

C#: how associate string description to Enum types

If you need to associate string description to Enum types, you can add an attribute to each enum member, via the System.ComponentModel.DescriptionAttribute attribute.
So for example:
public enum MyEnum
[Description("desc A")]
[Description("desc B")]
Then, you can retrieve each member description with the following extension method:
public static string GetDescriptionFromAttribute(this Enum myInstance)
FieldInfo fi = myInstance.GetType().GetField(myInstance.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
return attributes[0].Description;
return myInstance.ToString();

Querying StringCollections with LINQ

If you need to query StringCollections, or other non-generic collection classes, with LINQ, you have to use the Cast method, so, for example:

StringCollection myStringColl = new StringCollection("AAA", "BBB", "CCC");

var query = from x in myStringColl.Cast<string>()...