ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/AnywhereTS.Logging/Logging.cs
Revision: 186
Committed: Mon Jul 16 15:00:20 2012 UTC (10 years, 10 months ago) by william
File size: 5890 byte(s)
Log Message:
+ major fix to logging (all logs write to their respective logs and also get written into a single log)

File Contents

# Content
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using log4net.Config;
5 using log4net;
6 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 using log4net.Repository.Hierarchy;
14 using log4net.Core;
15
16 namespace AnywhereTS
17 {
18 public static class Logging
19 {
20 private static bool configured = false;
21 static Logging() { Logging.Initialize(); }
22 private static void Initialize()
23 {
24
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 }
41 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 #region Log access
57 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
61 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
64 public static ILog WizardLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.Wizard"); } }
65 public static ILog DefaultLog { get { return FallbackLog; } }
66
67
68 private static ILog FallbackLog;
69
70 #endregion
71
72 public static void UpdateLogPath()
73 {
74 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 }
80 public static void UpdateLogPath(string path) { Logging.SetLogPath(path); }
81
82 private static void SetLogPath(string path)
83 {
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
100 #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 catch { return new List<string>(); }
110 }
111 public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
112 {
113 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 }
132 #endregion
133 }
134 }