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

Annotation of /trunk/AnywhereTS.Logging/Logging.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (hide annotations) (download)
Mon Jul 16 15:00:20 2012 UTC (8 years, 9 months ago) by william
File size: 5890 byte(s)
+ major fix to logging (all logs write to their respective logs and also get written into a single log)

1 william 36 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using log4net.Config;
5     using log4net;
6     using System.IO;
7 william 70 using log4net.Util;
8     using System.Reflection;
9     using System.Collections;
10 william 149 using System.Windows.Forms;
11     using log4net.Appender;
12     using System.Linq;
13 william 186 using log4net.Repository.Hierarchy;
14     using log4net.Core;
15 william 36
16     namespace AnywhereTS
17     {
18     public static class Logging
19     {
20 william 186 private static bool configured = false;
21 william 36 static Logging() { Logging.Initialize(); }
22 william 149 private static void Initialize()
23     {
24 william 186
25     Assembly t = Assembly.GetExecutingAssembly();
26     FileInfo fi = new FileInfo(t.Location);
27     string path = fi.Directory.FullName;
28     Initialize_FallbackLog(path);
29     FileInfo f_config = new FileInfo(string.Format(@"{0}\{1}", path, "AnywhereTS.Logging.dll.config"));
30     if (f_config.Exists)
31     {
32     XmlConfigurator.Configure(f_config);
33     Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
34     configured = true;
35     }
36     else
37     {
38     configured = false;
39     }
40 william 149 }
41 william 186 private static void Initialize_FallbackLog(string path)
42     {
43     Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
44     FileAppender appender = new FileAppender();
45     appender.AppendToFile = true;
46     appender.File = string.Format(@"{0}\logs\AnywhereTS.log", path);
47     log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline");
48     appender.Layout = layout;
49     appender.Name = "Default";
50     appender.ActivateOptions();
51     hierarchy.Root.AddAppender(appender);
52     hierarchy.Root.Level = Level.All;
53     hierarchy.Configured = true;
54     FallbackLog = LogManager.GetLogger("Default");
55     }
56 william 36 #region Log access
57 william 186 public static ILog ATSAdminLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSAmdin"); } }
58     public static ILog TSControlPanelLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel"); } }
59     public static ILog DatabaseLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.DBSupport"); } }
60 william 147
61 william 186 public static ILog ATSAdminInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer"); } }
62     public static ILog TSControlPanelInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer"); } }
63 william 165
64 william 186 public static ILog WizardLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.Wizard"); } }
65     public static ILog DefaultLog { get { return FallbackLog; } }
66 william 165
67 william 186
68     private static ILog FallbackLog;
69    
70 william 36 #endregion
71 william 70
72 william 165 public static void UpdateLogPath()
73     {
74 william 186 Assembly t = Assembly.GetExecutingAssembly();
75     FileInfo fi = new FileInfo(t.Location);
76     string path = fi.Directory.FullName;
77    
78     Logging.UpdateLogPath(string.Format(@"{0}\logs",path));
79 william 165 }
80 william 150 public static void UpdateLogPath(string path) { Logging.SetLogPath(path); }
81    
82     private static void SetLogPath(string path)
83 william 149 {
84     var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders()
85     where appender is FileAppender
86     select appender;
87     fileAppenders.Cast<FileAppender>()
88     .ToList()
89     .ForEach(
90     fa =>
91     {
92     FileInfo fi = new FileInfo(fa.File);
93     string file = fi.Name;
94     fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName;
95     fa.ActivateOptions();
96     }
97     );
98     }
99 william 150
100 william 70 #region log4net help
101     public static List<string> GetMessagesFromThreadContextStack(string RawContextStack)
102     {
103     try
104     {
105     ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()];
106     if (tc == null) { return new List<string>(); }
107     else { return GetMessagesFromThreadContextStack(tc); }
108     }
109 william 71 catch { return new List<string>(); }
110 william 70 }
111     public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
112     {
113 william 71 Type t = typeof(ThreadContextStack);
114     //ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
115     //ThreadContextStack tmpRawTCS = null;
116     //tmpRawTCS = (ThreadContextStack)ci.Invoke(null);
117     List<string> stObjects = new List<string>();
118     ThreadContextStack context = (rawTCS as ThreadContextStack);
119     for (int i = 0; i < context.Count; i++)
120     {
121     MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance);
122     string message = mi.Invoke(context, null).ToString();
123     stObjects.Add(message);
124     context.Pop();
125     }
126     stObjects.Reverse();
127     rawTCS.Clear();
128     // push the popped Context message back onto the original ContextStack
129     foreach (string stObject in stObjects) { rawTCS.Push(stObject); }
130     return stObjects;
131 william 70 }
132     #endregion
133 william 36 }
134     }

  ViewVC Help
Powered by ViewVC 1.1.22