Messages tagged with c#

Compile errors while migrating from Unity 1.2 to version 2.0

While migrating project from Unity version 1.2 to version 2.0 I came across several compiler errors like below:

c.Resolve<ILogger>();

with following error message:

The non-generic method 'Microsoft.Practices.Unity.UnityContainer.Resolve(System.Type, string, params Microsoft.Practices.Unity.ResolverOverride[])' cannot be used with type arguments

It happened because in the code we were specifying types with a namespace like below:

Microsoft.Practices.Unity.UnityContainer c = new Microsoft.Practices.Unity.UnityContainer();

and there was no using directive aliasing the Microsoft.Practices.Unity namespace. Because the new version of Unity defines those generic methods (and many more) as extension methods, the following using statement is required:

using Microsoft.Practices.Unity;

Fortunately, this time the migration was rather easy and after updating using section in several classes we were good to go.

You can read more about Extension Methods at following address http://msdn.microsoft.com/en-us/library/bb383977.aspx

You can read more about Unity Container methods at following address http://msdn.microsoft.com/en-us/library/microsoft.practices.unity.unitycontainer_methods(PandP.20).aspx

You can read more about Unity Container methods at following address http://msdn.microsoft.com/en-us/library/ff660929(PandP.20).aspx

How to log exceptions

If you log exceptions by saving it’s message:

logger.Error("Operation failed." + ex.Message);

or it’s string representation:

logger.Error("Operation failed." + ex.ToString());

you do it wrong! It makes extremely hard if not impossible to find out what went wrong in the application. To be able to successfully debug application from log files you have to log at least: exception type, message, stack trace and, recursively, inner exceptions. For more specific exceptions you also want to log their properties. And then there is an AggregateException for which you need to log all aggregated exceptions.