Tips for Debugging Your Code in Visual Studio 2010: Listing 1.

You can use the Managed Extensibility Framework (MEF) to create a provider that returns an existing TraceSource, creates or accesses one as a MEF part, or creates one through traditional means. I don't like calling this a factory, because in most cases calls don't create a new item. You can use a similar pattern in many other scenarios.

   [TraceSourceProvider()]
   public class TraceSourceProvider : ITraceSourceProvider 
   {
      [ImportMany()]
      private IEnumerable<ITraceSource> sources = null;
      private IEnumerable<TraceSource> traceSources = null;

      public System.Diagnostics.TraceSource 
        GetTraceSource(string traceSourceName)
      {
         if (traceSources  == null)
         {
            traceSources = from x in sources 
                           select x as TraceSource;
         }
         var matches = from x in traceSources 
                       where x.Name == traceSourceName 
                       select x;
         if (matches.Count() > 0)
         {
            var ret = matches.FirstOrDefault() as TraceSource;
            return ret;
         }
         var traceSource = new TraceSource(traceSourceName);
         traceSources = 
           traceSources.Union (new TraceSource[] {traceSource});
         return traceSource;
      }