/[AnywhereTS-MSSQL]/trunk/AnywhereTS.Logging/Logging.cs
ViewVC logotype

Diff of /trunk/AnywhereTS.Logging/Logging.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 48 by william, Thu Jul 12 14:32:21 2012 UTC revision 165 by william, Mon Jul 16 10:19:21 2012 UTC
# Line 4  using System.Text; Line 4  using System.Text;
4  using log4net.Config;  using log4net.Config;
5  using log4net;  using log4net;
6  using System.IO;  using System.IO;
7    using log4net.Util;
8    using System.Reflection;
9    using System.Collections;
10    using System.Windows.Forms;
11    using log4net.Appender;
12    using System.Linq;
13    
14  namespace AnywhereTS  namespace AnywhereTS
15  {  {
16      public static class Logging      public static class Logging
17      {      {
18          static Logging() { Logging.Initialize(); }          static Logging() { Logging.Initialize(); }
19          private static void Initialize() { XmlConfigurator.Configure(new FileInfo("AnywhereTS.Logging.dll.config")); }          private static void Initialize()
20            {
21                string path = typeof(Logging).Assembly.Location.ToLower().Replace(@"\AnywhereTS.Logging.dll".ToLower(), "");
22                string config = string.Format(@"{0}\{1}", path, "AnywhereTS.Logging.dll.config");
23                //MessageBox.Show(config);
24                FileInfo fi = new FileInfo(config);
25                XmlConfigurator.Configure(fi);
26                Logging.SetLogPath(string.Format(@"{0}\logs", fi.FullName));
27            }
28    
29          #region Log access          #region Log access
30          public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin");          public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin");
31          public static readonly ILog TSControlPanelLog = LogManager.GetLogger("AnywhereTS.TSControlPanel");          public static readonly ILog TSControlPanelLog = LogManager.GetLogger("AnywhereTS.TSControlPanel");
32            public static readonly ILog DatabaseLog = LogManager.GetLogger("AnywhereTS.DBSupport");
33    
34            public static readonly ILog ATSAdminInstallerLog = LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer");
35            public static readonly ILog TSControlPanelInstallerLog = LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer");
36    
37            public static readonly ILog WizardLog = LogManager.GetLogger("AnywhereTS.Wizard");
38            public static readonly ILog DefaultLog = LogManager.GetLogger("AnywhereTS.DefaultLogger");
39    
40            #endregion
41    
42            public static void UpdateLogPath()
43            {
44                string path = typeof(Logging).Assembly.Location.ToLower().Replace(@"\AnywhereTS.Logging.dll".ToLower(), "");
45                Logging.UpdateLogPath(path);
46            }
47            public static void UpdateLogPath(string path) { Logging.SetLogPath(path); }
48    
49            private static void SetLogPath(string path)
50            {
51                var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders()
52                                    where appender is FileAppender
53                                    select appender;
54                fileAppenders.Cast<FileAppender>()
55                   .ToList()
56                   .ForEach(
57                       fa =>
58                       {
59                           FileInfo fi = new FileInfo(fa.File);
60                           string file = fi.Name;
61                           fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName;
62                           fa.ActivateOptions();
63                       }
64                   );
65            }
66    
67            #region log4net help
68            public static List<string> GetMessagesFromThreadContextStack(string RawContextStack)
69            {
70                try
71                {
72                    ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()];
73                    if (tc == null) { return new List<string>(); }
74                    else { return GetMessagesFromThreadContextStack(tc); }
75                }
76                catch { return new List<string>(); }
77            }
78            public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
79            {
80                Type t = typeof(ThreadContextStack);
81                //ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
82                //ThreadContextStack tmpRawTCS = null;
83                //tmpRawTCS = (ThreadContextStack)ci.Invoke(null);
84                List<string> stObjects = new List<string>();
85                ThreadContextStack context = (rawTCS as ThreadContextStack);
86                for (int i = 0; i < context.Count; i++)
87                {
88                    MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance);
89                    string message = mi.Invoke(context, null).ToString();
90                    stObjects.Add(message);
91                    context.Pop();
92                }
93                stObjects.Reverse();
94                rawTCS.Clear();
95                // push the popped Context message back onto the original ContextStack
96                foreach (string stObject in stObjects) { rawTCS.Push(stObject); }
97                return stObjects;
98            }
99          #endregion          #endregion
100      }      }
101  }  }

Legend:
Removed from v.48  
changed lines
  Added in v.165

  ViewVC Help
Powered by ViewVC 1.1.22