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 |
|
14 |
namespace AnywhereTS |
15 |
{ |
16 |
public static class Logging |
17 |
{ |
18 |
static Logging() { Logging.Initialize(); } |
19 |
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 |
30 |
public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin"); |
31 |
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 |
#endregion |
37 |
|
38 |
|
39 |
public static void UpdateLogPath(string path) { Logging.SetLogPath(path); } |
40 |
|
41 |
private static void SetLogPath(string path) |
42 |
{ |
43 |
var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders() |
44 |
where appender is FileAppender |
45 |
select appender; |
46 |
fileAppenders.Cast<FileAppender>() |
47 |
.ToList() |
48 |
.ForEach( |
49 |
fa => |
50 |
{ |
51 |
FileInfo fi = new FileInfo(fa.File); |
52 |
string file = fi.Name; |
53 |
fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName; |
54 |
fa.ActivateOptions(); |
55 |
} |
56 |
); |
57 |
} |
58 |
|
59 |
#region log4net help |
60 |
public static List<string> GetMessagesFromThreadContextStack(string RawContextStack) |
61 |
{ |
62 |
try |
63 |
{ |
64 |
ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()]; |
65 |
if (tc == null) { return new List<string>(); } |
66 |
else { return GetMessagesFromThreadContextStack(tc); } |
67 |
} |
68 |
catch { return new List<string>(); } |
69 |
} |
70 |
public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS) |
71 |
{ |
72 |
Type t = typeof(ThreadContextStack); |
73 |
//ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); |
74 |
//ThreadContextStack tmpRawTCS = null; |
75 |
//tmpRawTCS = (ThreadContextStack)ci.Invoke(null); |
76 |
List<string> stObjects = new List<string>(); |
77 |
ThreadContextStack context = (rawTCS as ThreadContextStack); |
78 |
for (int i = 0; i < context.Count; i++) |
79 |
{ |
80 |
MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance); |
81 |
string message = mi.Invoke(context, null).ToString(); |
82 |
stObjects.Add(message); |
83 |
context.Pop(); |
84 |
} |
85 |
stObjects.Reverse(); |
86 |
rawTCS.Clear(); |
87 |
// push the popped Context message back onto the original ContextStack |
88 |
foreach (string stObject in stObjects) { rawTCS.Push(stObject); } |
89 |
return stObjects; |
90 |
} |
91 |
#endregion |
92 |
} |
93 |
} |