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 |
|
23 |
string config = string.Format(@"{0}\{1}", path, "AnywhereTS.Logging.dll.config"); |
24 |
//MessageBox.Show(config); |
25 |
FileInfo fi = new FileInfo(config); |
26 |
|
27 |
if (!fi.Exists) |
28 |
{ |
29 |
MessageBox.Show(string.Format("Logging Config does not exist!{0}{1}", System.Environment.NewLine, fi.FullName)); |
30 |
} |
31 |
else |
32 |
{ |
33 |
XmlConfigurator.Configure(new FileInfo(config)); |
34 |
} |
35 |
} |
36 |
|
37 |
#region Log access |
38 |
public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin"); |
39 |
public static readonly ILog TSControlPanelLog = LogManager.GetLogger("AnywhereTS.TSControlPanel"); |
40 |
public static readonly ILog DatabaseLog = LogManager.GetLogger("AnywhereTS.DBSupport"); |
41 |
|
42 |
//public static readonly ILog ATSAdminInstallerLog = LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer"); |
43 |
//public static readonly ILog TSControlPanelInstallerLog = LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer"); |
44 |
#endregion |
45 |
|
46 |
|
47 |
public static void SetLogPath(string path) |
48 |
{ |
49 |
var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders() |
50 |
where appender is FileAppender |
51 |
select appender; |
52 |
fileAppenders.Cast<FileAppender>() |
53 |
.ToList() |
54 |
.ForEach( |
55 |
fa => |
56 |
{ |
57 |
FileInfo fi = new FileInfo(fa.File); |
58 |
string file = fi.Name; |
59 |
fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName; |
60 |
fa.ActivateOptions(); |
61 |
} |
62 |
); |
63 |
} |
64 |
#region log4net help |
65 |
public static List<string> GetMessagesFromThreadContextStack(string RawContextStack) |
66 |
{ |
67 |
try |
68 |
{ |
69 |
ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()]; |
70 |
if (tc == null) { return new List<string>(); } |
71 |
else { return GetMessagesFromThreadContextStack(tc); } |
72 |
} |
73 |
catch { return new List<string>(); } |
74 |
} |
75 |
public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS) |
76 |
{ |
77 |
Type t = typeof(ThreadContextStack); |
78 |
//ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null); |
79 |
//ThreadContextStack tmpRawTCS = null; |
80 |
//tmpRawTCS = (ThreadContextStack)ci.Invoke(null); |
81 |
List<string> stObjects = new List<string>(); |
82 |
ThreadContextStack context = (rawTCS as ThreadContextStack); |
83 |
for (int i = 0; i < context.Count; i++) |
84 |
{ |
85 |
MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance); |
86 |
string message = mi.Invoke(context, null).ToString(); |
87 |
stObjects.Add(message); |
88 |
context.Pop(); |
89 |
} |
90 |
stObjects.Reverse(); |
91 |
rawTCS.Clear(); |
92 |
// push the popped Context message back onto the original ContextStack |
93 |
foreach (string stObject in stObjects) { rawTCS.Push(stObject); } |
94 |
return stObjects; |
95 |
} |
96 |
#endregion |
97 |
} |
98 |
} |