Removed/wrapped Log methods in Globals
authorThomas
Wed, 07 Sep 2016 15:34:16 +0200
changeset 12470d02588e644c
parent 1246 98a10b3984e6
child 1248 d6aa0fcdbaa3
Removed/wrapped Log methods in Globals
Globals.cs
     1.1 --- a/Globals.cs	Wed Sep 07 13:39:32 2016 +0200
     1.2 +++ b/Globals.cs	Wed Sep 07 15:34:16 2016 +0200
     1.3 @@ -306,8 +306,7 @@
     1.4          /// <param name="text">The text to add to the log.</param>
     1.5          public static void Log(string text)
     1.6          {
     1.7 -            Globals.WriteToLogFile(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |#| " + text);
     1.8 -            return;
     1.9 +            pEp.Log.Info(text);
    1.10          }
    1.11  
    1.12          /// <summary>
    1.13 @@ -317,12 +316,7 @@
    1.14          /// <param name="text">The verbose text to add to the lob.</param>
    1.15          public static void LogVerbose(string text)
    1.16          {
    1.17 -            if ((Globals.ThisAddIn != null) &&
    1.18 -                (Globals.ThisAddIn.Settings.IsVerboseLoggingEnabled))
    1.19 -            {
    1.20 -                Globals.WriteToLogFile(System.DateTime.Now.ToString("HH:mm:ss.fff") + " | " + text);
    1.21 -            }
    1.22 -            return;
    1.23 +            pEp.Log.Verbose(text);
    1.24          }
    1.25  
    1.26          /// <summary>
    1.27 @@ -408,129 +402,13 @@
    1.28          }
    1.29  
    1.30          /// <summary>
    1.31 -        /// Writes the given text to the log file.
    1.32 -        /// </summary>
    1.33 -        /// <param name="text">The text to write to the log file.</param>
    1.34 -        private static void WriteToLogFile(string text)
    1.35 -        {
    1.36 -            try
    1.37 -            {
    1.38 -                if (text != null)
    1.39 -                {
    1.40 -                    lock (mutexLogFile)
    1.41 -                    {
    1.42 -                        if (Globals.logWriter == null)
    1.43 -                        {
    1.44 -                            // If file exists, set line counter to file line count
    1.45 -                            if (File.Exists(Globals.GetLogFilePath()))
    1.46 -                            {
    1.47 -                                try
    1.48 -                                {
    1.49 -                                    var lines = File.ReadAllLines(Globals.GetLogFilePath());
    1.50 -                                    logLineCount = lines.Length;
    1.51 -                                }
    1.52 -                                catch { }
    1.53 -                            }
    1.54 -
    1.55 -                            // Open the file
    1.56 -                            Globals.logWriter = File.AppendText(Globals.GetLogFilePath());
    1.57 -                        }
    1.58 -
    1.59 -                        Globals.logWriter.WriteLine(text);
    1.60 -
    1.61 -                        Globals.logLineCount++;
    1.62 -
    1.63 -                        // If the maximum line count was reached, clean up the log file by removing the first lines.
    1.64 -                        if (Globals.logLineCount >= Globals.LOG_MAX_LINES)
    1.65 -                        {
    1.66 -                            Globals.SanitizeLog();
    1.67 -                        }
    1.68 -                    }
    1.69 -                }
    1.70 -            }
    1.71 -            catch { }
    1.72 -
    1.73 -            return;
    1.74 -        }
    1.75 -
    1.76 -        /// <summary>
    1.77 -        /// FIFO function to remove the first lines of the log file when a defined maximum 
    1.78 -        /// of lines has been reached. 
    1.79 -        /// </summary>
    1.80 -        private static void SanitizeLog()
    1.81 -        {
    1.82 -            try
    1.83 -            {
    1.84 -                lock (mutexLogFile)
    1.85 -                {
    1.86 -                    Globals.CloseLogFile();
    1.87 -
    1.88 -                    var lines = File.ReadAllLines(Globals.GetLogFilePath());
    1.89 -
    1.90 -                    Globals.ClearLogFile();
    1.91 -
    1.92 -                    int startIndex = (lines.Length - (Globals.LOG_MAX_LINES - Globals.LOG_LINES_TO_REPLACE));
    1.93 -                    if (startIndex < 0)
    1.94 -                    {
    1.95 -                        startIndex = 0;
    1.96 -                    }
    1.97 -
    1.98 -                    using (StreamWriter streamWriter = File.AppendText(Globals.GetLogFilePath()))
    1.99 -                    {
   1.100 -                        for (int i = startIndex; i < lines.Length; i++)
   1.101 -                        {
   1.102 -                            if (string.IsNullOrEmpty(lines[i]) == false)
   1.103 -                            {
   1.104 -                                streamWriter.WriteLine(lines[i]);
   1.105 -                            }
   1.106 -                        }
   1.107 -                    }
   1.108 -
   1.109 -                    Globals.logLineCount = (Globals.LOG_MAX_LINES - Globals.LOG_LINES_TO_REPLACE);
   1.110 -                }
   1.111 -            }
   1.112 -            catch { }
   1.113 -
   1.114 -            return;
   1.115 -        }
   1.116 -
   1.117 -        /// <summary>
   1.118          /// Reads all lines of text from the log file.
   1.119          /// This will never return null.
   1.120          /// </summary>
   1.121          /// <returns>The lines of text in the log file.</returns>
   1.122          public static string ReadLogFile()
   1.123          {
   1.124 -            string log = "";
   1.125 -            string path;
   1.126 -            StreamReader logReader;
   1.127 -
   1.128 -            try
   1.129 -            {
   1.130 -                Globals.CloseLogFile();
   1.131 -
   1.132 -                lock (mutexLogFile)
   1.133 -                {
   1.134 -                    path = Globals.GetLogFilePath();
   1.135 -                    if (File.Exists(path))
   1.136 -                    {
   1.137 -                        logReader = new StreamReader(path);
   1.138 -                        log = logReader.ReadToEnd();
   1.139 -
   1.140 -                        logReader.Close();
   1.141 -                        logReader.Dispose();
   1.142 -                    }
   1.143 -                }
   1.144 -
   1.145 -                // Never allow null
   1.146 -                if (log == null)
   1.147 -                {
   1.148 -                    log = "";
   1.149 -                }
   1.150 -            }
   1.151 -            catch { }
   1.152 -
   1.153 -            return (log);
   1.154 +            return pEp.Log.Read();
   1.155          }
   1.156  
   1.157          /// <summary>
   1.158 @@ -538,22 +416,7 @@
   1.159          /// </summary>
   1.160          public static void CloseLogFile()
   1.161          {
   1.162 -            try
   1.163 -            {
   1.164 -                lock (mutexLogFile)
   1.165 -                {
   1.166 -                    if (Globals.logWriter != null)
   1.167 -                    {
   1.168 -                        Globals.logWriter.Flush();
   1.169 -                        Globals.logWriter.Close();
   1.170 -                        Globals.logWriter.Dispose();
   1.171 -                        Globals.logWriter = null;
   1.172 -                    }
   1.173 -                }
   1.174 -            }
   1.175 -            catch { }
   1.176 -
   1.177 -            return;
   1.178 +            pEp.Log.Close();
   1.179          }
   1.180  
   1.181          /// <summary>
   1.182 @@ -561,24 +424,7 @@
   1.183          /// </summary>
   1.184          public static void ClearLogFile()
   1.185          {
   1.186 -            string path;
   1.187 -
   1.188 -            try
   1.189 -            {
   1.190 -                Globals.CloseLogFile();
   1.191 -
   1.192 -                lock (mutexLogFile)
   1.193 -                {
   1.194 -                    path = Globals.GetLogFilePath();
   1.195 -                    if (File.Exists(path))
   1.196 -                    {
   1.197 -                        File.Delete(path);
   1.198 -                    }
   1.199 -                }
   1.200 -            }
   1.201 -            catch { }
   1.202 -
   1.203 -            return;
   1.204 +            pEp.Log.Clear();
   1.205          }
   1.206  
   1.207          /// <summary>