/[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 327 - (hide annotations) (download)
Mon Aug 27 21:01:33 2012 UTC (7 years, 10 months ago) by william
File size: 6047 byte(s)

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 william 327 public static ILog ATSClientManagerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSClientManager"); } }
59 william 186 public static ILog TSControlPanelLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel"); } }
60     public static ILog DatabaseLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.DBSupport"); } }
61 william 147
62 william 186 public static ILog ATSAdminInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer"); } }
63     public static ILog TSControlPanelInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer"); } }
64 william 165
65 william 186 public static ILog WizardLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.Wizard"); } }
66     public static ILog DefaultLog { get { return FallbackLog; } }
67 william 165
68 william 186
69     private static ILog FallbackLog;
70    
71 william 36 #endregion
72 william 70
73 william 165 public static void UpdateLogPath()
74     {
75 william 186 Assembly t = Assembly.GetExecutingAssembly();
76     FileInfo fi = new FileInfo(t.Location);
77     string path = fi.Directory.FullName;
78    
79     Logging.UpdateLogPath(string.Format(@"{0}\logs",path));
80 william 165 }
81 william 150 public static void UpdateLogPath(string path) { Logging.SetLogPath(path); }
82    
83     private static void SetLogPath(string path)
84 william 149 {
85     var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders()
86     where appender is FileAppender
87     select appender;
88     fileAppenders.Cast<FileAppender>()
89     .ToList()
90     .ForEach(
91     fa =>
92     {
93     FileInfo fi = new FileInfo(fa.File);
94     string file = fi.Name;
95     fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName;
96     fa.ActivateOptions();
97     }
98     );
99     }
100 william 150
101 william 70 #region log4net help
102     public static List<string> GetMessagesFromThreadContextStack(string RawContextStack)
103     {
104     try
105     {
106     ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()];
107     if (tc == null) { return new List<string>(); }
108     else { return GetMessagesFromThreadContextStack(tc); }
109     }
110 william 71 catch { return new List<string>(); }
111 william 70 }
112     public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
113     {
114 william 71 Type t = typeof(ThreadContextStack);
115     //ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
116     //ThreadContextStack tmpRawTCS = null;
117     //tmpRawTCS = (ThreadContextStack)ci.Invoke(null);
118     List<string> stObjects = new List<string>();
119     ThreadContextStack context = (rawTCS as ThreadContextStack);
120     for (int i = 0; i < context.Count; i++)
121     {
122     MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance);
123     string message = mi.Invoke(context, null).ToString();
124     stObjects.Add(message);
125     context.Pop();
126     }
127     stObjects.Reverse();
128     rawTCS.Clear();
129     // push the popped Context message back onto the original ContextStack
130     foreach (string stObject in stObjects) { rawTCS.Push(stObject); }
131     return stObjects;
132 william 70 }
133     #endregion
134 william 36 }
135     }

  ViewVC Help
Powered by ViewVC 1.1.22