/[AnywhereTS-MSSQL]/trunk/AnywhereTS.Logging/Logging.cs
ViewVC logotype

Diff of /trunk/AnywhereTS.Logging/Logging.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/AnywhereTS.Logging/Logging.cs	2012/07/12 11:35:07	36
+++ trunk/AnywhereTS.Logging/Logging.cs	2012/07/16 15:00:20	186
@@ -4,16 +4,131 @@
 using log4net.Config;
 using log4net;
 using System.IO;
+using log4net.Util;
+using System.Reflection;
+using System.Collections;
+using System.Windows.Forms;
+using log4net.Appender;
+using System.Linq;
+using log4net.Repository.Hierarchy;
+using log4net.Core;
 
 namespace AnywhereTS
 {
     public static class Logging
     {
+        private static bool configured = false;
         static Logging() { Logging.Initialize(); }
-        private static void Initialize() { XmlConfigurator.Configure(new FileInfo("AnywhereTS.Logging.dll.config")); }
-
+        private static void Initialize()
+        {
+            
+            Assembly t = Assembly.GetExecutingAssembly();
+            FileInfo fi = new FileInfo(t.Location);
+            string path = fi.Directory.FullName;
+            Initialize_FallbackLog(path);
+            FileInfo f_config = new FileInfo(string.Format(@"{0}\{1}", path, "AnywhereTS.Logging.dll.config"));
+            if (f_config.Exists)
+            {
+                XmlConfigurator.Configure(f_config);
+                Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
+                configured = true;
+            }
+            else
+            {
+                configured = false;               
+            }            
+        }
+        private static void Initialize_FallbackLog(string path)
+        {
+            Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
+            FileAppender appender = new FileAppender();
+            appender.AppendToFile = true;
+            appender.File = string.Format(@"{0}\logs\AnywhereTS.log", path);
+            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger [%property{NDC}] - %message%newline");
+            appender.Layout = layout;
+            appender.Name = "Default";
+            appender.ActivateOptions();
+            hierarchy.Root.AddAppender(appender);
+            hierarchy.Root.Level = Level.All;
+            hierarchy.Configured = true;
+            FallbackLog = LogManager.GetLogger("Default");
+        }
         #region Log access
-        public static readonly ILog ATSAdminLog = LogManager.GetLogger("AnywhereTS.ATSAmdin");
+        public static ILog ATSAdminLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSAmdin"); } }
+        public static ILog TSControlPanelLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel"); } }
+        public static ILog DatabaseLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.DBSupport"); } }
+
+        public static ILog ATSAdminInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.ATSAmdin.Installer"); } }
+        public static ILog TSControlPanelInstallerLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.TSControlPanel.Installer"); } }
+
+        public static ILog WizardLog { get { if (!configured) return FallbackLog; return LogManager.GetLogger("AnywhereTS.Wizard"); } }
+        public static ILog DefaultLog { get { return FallbackLog; } }
+
+
+        private static ILog FallbackLog;
+
+        #endregion
+
+        public static void UpdateLogPath() 
+        {
+            Assembly t = Assembly.GetExecutingAssembly();
+            FileInfo fi = new FileInfo(t.Location);
+            string path = fi.Directory.FullName;
+
+            Logging.UpdateLogPath(string.Format(@"{0}\logs",path));
+        }
+        public static void UpdateLogPath(string path) { Logging.SetLogPath(path); }
+
+        private static void SetLogPath(string path)
+        {
+            var fileAppenders = from appender in log4net.LogManager.GetRepository().GetAppenders()
+                                where appender is FileAppender
+                                select appender;
+            fileAppenders.Cast<FileAppender>()
+               .ToList()
+               .ForEach(
+                   fa =>
+                   {
+                       FileInfo fi = new FileInfo(fa.File);
+                       string file = fi.Name;
+                       fa.File = new FileInfo(string.Format(@"{0}\{1}", path, file)).FullName;
+                       fa.ActivateOptions();
+                   }
+               );
+        }
+
+        #region log4net help
+        public static List<string> GetMessagesFromThreadContextStack(string RawContextStack)
+        {
+            try
+            {
+                ThreadContextStack tc = ThreadContext.Stacks[RawContextStack.ToUpper()];
+                if (tc == null) { return new List<string>(); }
+                else { return GetMessagesFromThreadContextStack(tc); }
+            }
+            catch { return new List<string>(); }
+        }
+        public static List<string> GetMessagesFromThreadContextStack(ThreadContextStack rawTCS)
+        {
+            Type t = typeof(ThreadContextStack);
+            //ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
+            //ThreadContextStack tmpRawTCS = null;
+            //tmpRawTCS = (ThreadContextStack)ci.Invoke(null);
+            List<string> stObjects = new List<string>();
+            ThreadContextStack context = (rawTCS as ThreadContextStack);
+            for (int i = 0; i < context.Count; i++)
+            {
+                MethodInfo mi = t.GetMethod("GetFullMessage", BindingFlags.NonPublic | BindingFlags.Instance);
+                string message = mi.Invoke(context, null).ToString();
+                stObjects.Add(message);
+                context.Pop();
+            }
+            stObjects.Reverse();
+            rawTCS.Clear();
+            // push the popped Context message back onto the original ContextStack
+            foreach (string stObject in stObjects) { rawTCS.Push(stObject); }
+            return stObjects;
+        }
         #endregion
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22