kenegozi.com

<form id='kenegozi' action='post'></form>

   
2009 Oct 29

Better late than never – IDCC video - Beautiful Teams and Code Leader by Roy Osherove

tagged as: miscellanea | idcc

It took me a while to get to it, but now you can watch the great talk by Roy Osherove on team leadership, from IDCC conference last month:

The quality is not the best, to say the least. I've been using a cheep Canon digicamera, and it's the first time I'm uploading *any* video, so please concentrate on the amazing quality of the content, not on the crappy visual

2009 Oct 16

Castle Windsor and the LoggingFacility

tagged as: tools | castle

So you want to be able to do some logging from your code.

 

log4net for example, is a very common logging framework for .NET, and using it is pretty straight forward, and the net is full of log4net intros.

 

Usage example:

namespace My.Application
{
	public class UsingLog4netDirectly
	{
		private static log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.FullName);
		...
		...
		...
	}
}

 

However, there are some caveats with using it directly.

  1. You get a hard dependency on log4net’s dll
  2. You need to manually setup the logger instance with logger name for each class – that’s both annoying and error prone
  3. And has bad impact on testing.

Windsor, once again, can help a lot with making it much easier. e.g:

namespace My.Application
{
	public class UsingLogIntegration
	{
		readonly ILogger logger;
		public UsingLogIntegration(ILogger logger)
		{
			this.logger = logger;
		}
		...
		...
		...
	}
}

 

Windsor will take care of injecting the correct logger instance, with the name set correctly (using the class name, not needing the wacky MethodBase… stuff)

 

So, what do you set this up?

 

Required assemblies:

Assuming you already use Windsor, you need

  1. Castle.Facilities.Logging  (the logging facility)
  2. Castle.Services.Logging.Log4netIntegration (or any other built-in or custom made Integration)
  3. log4net.dll  (or any other actual logger implementation)

You actually do not need to reference these assemblies, only make sure they are on the application’s bin folder.  If you use the programmatic configuration (like I do), you’d also need the first (the facility) referenced from your code.

 

Registration:
container.AddFacility("LoggingFacility", new LoggingFacility(LoggerImplementation.Log4net));

 

and …  that’s it !

 

You still need to setup log4net’s configuration, and tell the application where it is:

log4net.Config.XmlConfigurator.Configure();

 

Get a different logger for tests

Easy. Instead of configuring the facility, you simply configure another implementation.

container.Register(Component.For().Instance(new ConsoleLogger()));

If you'd explore the Castle.Core.Logging namespace you'd find few, very useful built-in implementations, such as a NullLogger (which you get by calling NullLogger.Instance), ConsoleLogger, StreamLogger (write to a file or memory stream), TraceLogger (writes to the diagnostic trace output) and WebLogger (writes to the HttpContext’s Trace, visible at trace.axd).

2009 Oct 1

Linq Cast extension method and InvalidCastException

tagged as: c#

For times when one wants the average of a collection of numbers, Linq’s Average() extension method is an easy win.

The problem is that it won’t work on a collection of byte elements.

The initial attempt would be:

var numbers = new byte[] {1, 2, 3, 4};
var avg = numbers.Cast<int>().Average();
Console.WriteLine(avg);

 

However this will fail with

Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
   at System.Linq.Enumerable.d__aa`1.MoveNext()
   at System.Linq.Enumerable.Average(IEnumerable`1 source)

 

The workaround is to cast using the Select operator, and manually cast to int:

var numbers = new byte[] {1, 2, 3, 4};
var avg = numbers.Select(b=>(int)b).Average();
Console.WriteLine(avg);

 

 

The author of the best explanation that will be posted as a comment to this post will get a huge prize (in other words – might get a mention in a follow up post …)

 

 

 

 

This was a short reminder-post. I do have some more interesting things awaiting in the pipe, and I promise to re-attend my blog now that the pressure of the new baby and of setting up the IDCC conference is out of the way. Good things comes to those who wait blah blah blah.

Subscribe

Statistics

387
834

The Lounge

Related Jobs

Related Books

search page | Blog's home | About me