Testing code snippets for Visual Studio

October 30th, 2009

If you create many test classes and test methods you probably repeat some code many times.
Here are some code snippets that I use every day at work, feel free to modify them if you want.

Read the rest of this entry »

log4net – Exception renderer

June 12th, 2009

If you use log4net you have probably noticed that this logger does not write information from exception's "Data" property. Here is the renderer which does:

C#:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using log4net.ObjectRenderer;
  6.  
  7. namespace SomeNamespace
  8. {
  9.     public class ExceptionRenderer : IObjectRenderer
  10.     {
  11.         #region IObjectRenderer Members
  12.  
  13.         public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
  14.         {
  15.             if (rendererMap == null)
  16.             {
  17.                 throw new ArgumentNullException("rendererMap");
  18.             }
  19.  
  20.             if (obj == null)
  21.             {
  22.                 throw new ArgumentNullException("obj");
  23.             }
  24.  
  25.             if (writer == null)
  26.             {
  27.                 throw new ArgumentNullException("writer");
  28.             }
  29.  
  30.             if (obj is Exception)
  31.             {
  32.                 Exception ex = (Exception)obj;
  33.                 writer.WriteLine(ex.ToString());
  34.  
  35.                 if (ex.Data.Count> 0)
  36.                 {
  37.                     writer.WriteLine("Exception Data:");
  38.                 }
  39.                 foreach (string key in ex.Data.Keys)
  40.                 {
  41.                     writer.WriteLine("\t {0} = {1}", key, rendererMap.FindAndRender(ex.Data[key]));
  42.                 }
  43.             }
  44.             else
  45.             {
  46.                 throw new InvalidOperationException(string.Format("Object of type {0} cannot be rendered with ExceptionRenderer", obj.GetType()));
  47.             }
  48.         }
  49.  
  50.         #endregion
  51.     }
  52. }

and the configuration needed for this to work is:

XML:
  1. <log4net>
  2. ...
  3.     <renderer renderingClass="SomeNamespace.ExceptionRenderer" renderedClass="System.Exception" />
  4. ...
  5. </log4net>

Sample output for NullReferenceException with Data["configPath"] = "myconfig.config":

2009-06-12 22:22:04,038 INFO  [LoggerTest.Program] - Exception during init
System.NullReferenceException: Something was null
   at LoggerTest.Program.Main(String[] args) in G:\C#\Temp\LoggerTest\Program.cs:line 26
Exception Data:
         configPath = myconfig.config

Feel free to use and modify it.

Creating enum which sucks

April 22nd, 2009

We create many enumerations in our programs. But sometimes we can make a mistake that drives somebody else mad.

Let's suppose that the first programmer created an enum like this:

C#:
  1. enum Mood
  2. {
  3.     Unknown = -1,
  4.     Awesome,
  5.     Cool,
  6.     Bad,
  7.     Awful
  8. }

It does not look bad at the first look, yet another enum.

Another programmer created a list:

C#:
  1. List<mood> availableMoods = new List<mood>();
  2. availableMoods.Add(Mood.Awful);
  3. availableMoods.Add(Mood.Bad);

Then he would like to find there a first mood type which is a part of "good mood" group and pass the result to another layer of an application. He wrote the code:

C#:
  1. Mood found = availableMoods.Find(
  2.     delegate(Mood f)
  3.     {
  4.         return f == Mood.Awesome || f == Mood.Cool;
  5.     }
  6. );

Now he realized that it cannot work because Find method of a List(T) class returns default(T) when a list does not contain desired item. In this case he would get Mood.Awesome because it is the default value of the enum. I do not need to add that the Mood.Awesome is not even the part of the list.

We can solve this problem by several different approaches.

  1. Create different enum where the default element will be "Unknown = 0"
  2. Use FindIndex method of List(T) class so we will be able to handle the situation of not found element
  3. I believe that other approaches could work as well

The question to discuss - which programmer made a mistake? The first one who created "unfortunate" enum or the second one who used wrong structure / wrong searching method?

Designed by SirMike © All rights reserved

Valid XHTML 1.0! Valid CSS!

Powered by Rootnode