New World with Extension Methods
Introduction of Extension Methods in .NET 3.5 was one of the main new stuff in this version and the main reason was the necessity to have such a feature for LINQ. There have been many articles and tutorials about this new feature and the concept is easy to learn and implement.
However, I think that no one can believe the value of this new feature until he or she applies this feature to reduce a lot of existing code written in previous versions of .NET framework.
For me this happened today and I understood how great a feature can be!
Usually I write my own exception types derived from built-in .NET exception types in my projects for different purposes. Beside some other reasons (that can be ignored in some cases) the main reason to do this is the ability to have an integrated easy method to log the exception data.
There is no doubt that such a job can yield to lots of custom exception types in a project that are all derived from some base types. Even though, in a framework this can be the best way but it takes a lot of time to work on these custom exception types for smaller projects where built-in exception types can be used easily.
One solution can be using a third class that gets exceptions and logs them. It works very well but the result isn't as beautiful as the case when you use an extension method to extend built-in exception types to support logging methods!
Suppose that you want to add a single method to your built-in .NET exception types to log a few properties of the exception. You can simply add an extension method like Log to Exception type or any base exception type that you desire. End result can be something like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ExtensionMethods
{
public static class ExtendedException
{
public static void Log(this Exception ex)
{
string path = @"D:\Log\log.txt";
File.AppendAllText(path, string.Format
("Message: {0}\n" + "Type: {1}\n" + "-----------\n",
ex.Message, ex.GetType().ToString()), Encoding.UTF8);
}
}
}
Now if I run a simple code like the following code then the exception can be logged in a text file.
static void Main(string[] args)
{
Console.Title = "New World with Extension Methods";
Console.WriteLine("Enter your age");
int age = Convert.ToInt32(Console.ReadLine());
try
{
if (age < 0)
throw new ArgumentOutOfRangeException("age");
}
catch (Exception ex)
{
ex.Log();
Console.WriteLine("Exception Logged ...");
}
Console.ReadLine();
}
As you see this is a very simple, quick and powerful way to log exceptions in small and medium scale applications. The idea is completely based on the extension methods and results the simplest code that is probably ever written for exception logging in any application!
Today I applied this technique in a project and noticed the simplicity and ease of development that extension methods have brought to .NET applications!
[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.
1 Comment : 03.08.08
#1
Mohammad Azam
03.09.2008 @ 2:05 AM
Nicely done!