Added log4net for client profile
[pithos-ms-client] / trunk / Pithos.Client.WPF / AppBootstrapper.cs
1 using System.Windows;
2 using System.Windows.Navigation;
3 using Caliburn.Micro;
4 using Caliburn.Micro.Logging;
5 using Pithos.Client.WPF.Caliburn.Micro.Logging;
6 using Pithos.Core;
7 using Pithos.Network;
8 using log4net.Appender;
9 using log4net.Config;
10 using log4net.Filter;
11 using log4net.Layout;
12
13 namespace Pithos.Client.WPF
14 {
15         using System;
16         using System.Collections.Generic;
17         using System.ComponentModel.Composition;
18         using System.ComponentModel.Composition.Hosting;
19         using System.ComponentModel.Composition.Primitives;
20         using System.Linq;
21         using Caliburn.Micro;
22
23         public class AppBootstrapper : Bootstrapper<IShell>
24         {
25                 CompositionContainer container;
26
27             public AppBootstrapper()
28             {
29                 ConfigureLogging();
30
31             LogManager.GetLog = type => new log4netLogger(type);
32             }
33
34         private static void ConfigureLogging()
35         {
36             var patternLayout = new PatternLayout();
37             patternLayout.ConversionPattern = "%logger (%property{myContext}) [%level]- %message%newline";
38             patternLayout.ActivateOptions();
39             var appender = new TraceAppender { Layout = patternLayout };
40             appender.AddFilter(new LevelRangeFilter{LevelMin=log4net.Core.Level.Info,LevelMax=log4net.Core.Level.Fatal});
41             appender.ActivateOptions();
42             
43             BasicConfigurator.Configure(appender);            
44         }
45
46                 /// <summary>
47                 /// By default, we are configured to use MEF
48                 /// </summary>
49                 protected override void Configure() {
50                     var catalog = new AggregateCatalog(
51                         AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
52                         );
53
54             Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient) };
55             foreach (var type in types)
56             {
57                 catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly));
58             }
59
60                       
61
62
63                         container = new CompositionContainer(catalog);
64
65                         var batch = new CompositionBatch();
66
67                         batch.AddExportedValue<IWindowManager>(new WindowManager());
68                         batch.AddExportedValue<IEventAggregator>(new EventAggregator());
69                         batch.AddExportedValue(container);
70                     batch.AddExportedValue(catalog);
71
72
73
74
75
76
77                         container.Compose(batch);
78                 }
79
80                 protected override object GetInstance(Type serviceType, string key)
81                 {
82                         string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
83                         var exports = container.GetExportedValues<object>(contract);
84
85                         if (exports.Count() > 0)
86                                 return exports.First();
87
88                         throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
89                 }
90
91                 protected override IEnumerable<object> GetAllInstances(Type serviceType)
92                 {
93                         return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
94                 }
95
96                 protected override void BuildUp(object instance)
97                 {
98                         container.SatisfyImportsOnce(instance);
99                 }
100         }
101 }