ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/AnywhereTS.Logging/Logging.cs
Revision: 149
Committed: Sun Jul 15 11:21:36 2012 UTC (11 years, 2 months ago) by william
File size: 4043 byte(s)
Log Message:

File Contents

# User Rev Content
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 36
14     namespace AnywhereTS
15     {
16     public static class Logging
17     {
18     static Logging() { Logging.Initialize(); }
19 william 149 private static void Initialize()
20     {
21     string path = typeof(Logging).Assembly.Location.ToLower().Replace(@"\AnywhereTS.Logging.dll".ToLower(), "");
22 william 36
23 william 149 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 william 36 #region Log access
38     public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin");
39 william 48 public static readonly ILog TSControlPanelLog = LogManager.GetLogger("AnywhereTS.TSControlPanel");
40 william 86 public static readonly ILog DatabaseLog = LogManager.GetLogger("AnywhereTS.DBSupport");
41 william 147
42 william 149 //public static readonly ILog ATSAdminInstallerLog = LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer");
43     //public static readonly ILog TSControlPanelInstallerLog = LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer");
44 william 36 #endregion
45 william 70
46 william 149
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 william 70 #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 william 71 catch { return new List<string>(); }
74 william 70 }
75     public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
76     {
77 william 71 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 william 70 }
96     #endregion
97 william 36 }
98     }