Log.cs
author Dean
Fri, 09 Sep 2016 10:28:22 +0200
changeset 1258 6d0b700bb463
parent 1249 00cc05656f71
child 1405 cd14f9d6cb61
permissions -rw-r--r--
Make log constants private.
Thomas@1246
     1
´╗┐using System;
Thomas@1246
     2
using System.IO;
Thomas@1246
     3
Thomas@1246
     4
namespace pEp
Thomas@1246
     5
{
Thomas@1246
     6
    /// <summary>
Thomas@1246
     7
    /// Class to handle all the logging of pEp for Outlook.
Thomas@1246
     8
    /// </summary>
Thomas@1246
     9
    internal class Log
Thomas@1246
    10
    {
Dean@1258
    11
        private const string LOG_FILE_NAME        = "log.txt";
Dean@1258
    12
        private const int    LOG_LINES_TO_REPLACE = 50;        // The number of lines to replace when the log file gets cleaned up
Dean@1258
    13
        private const int    LOG_MAX_LINES        = 1000;      // The maximum lines a log file can reach before the first lines get replaced (FIFO)
Thomas@1246
    14
Dean@1249
    15
        private static int          logLineCount = 0;
Dean@1249
    16
        private static StreamWriter logWriter    = null;
Dean@1249
    17
        private static object       mutexLogFile = new object();
Thomas@1246
    18
Thomas@1246
    19
        /**************************************************************
Thomas@1246
    20
         * 
Thomas@1246
    21
         * Methods
Thomas@1246
    22
         * 
Thomas@1246
    23
         *************************************************************/
Thomas@1246
    24
Thomas@1246
    25
        /// <summary>
Thomas@1246
    26
        /// Gets the path of the log file.
Thomas@1246
    27
        /// </summary>
Thomas@1246
    28
        /// <returns>The path of the log file.</returns>
Thomas@1246
    29
        private static string GetLogFilePath()
Thomas@1246
    30
        {
Thomas@1246
    31
            return (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", LOG_FILE_NAME));
Thomas@1246
    32
        }
Thomas@1246
    33
Thomas@1246
    34
        /// <summary>
Thomas@1246
    35
        /// Adds the given info text to the pEp for Outlook log.
Thomas@1246
    36
        /// </summary>
Thomas@1246
    37
        /// <param name="text">The text to add to the log.</param>
Thomas@1246
    38
        public static void Info(string text)
Thomas@1246
    39
        {
Thomas@1246
    40
            if (text != null)
Thomas@1246
    41
            {
Dean@1249
    42
                Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " | " + text);
Thomas@1246
    43
            }
Thomas@1246
    44
            return;
Thomas@1246
    45
        }
Thomas@1246
    46
Thomas@1246
    47
        /// <summary>
Thomas@1246
    48
        /// Adds the given error text to the pEp for Outlook log.
Thomas@1246
    49
        /// </summary>
Thomas@1246
    50
        /// <param name="text">The error text to add to the log.</param>
Thomas@1246
    51
        public static void Error(string text)
Thomas@1246
    52
        {
Thomas@1246
    53
            if (text != null)
Thomas@1246
    54
            {
Dean@1249
    55
                Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |E| " + text);
Thomas@1246
    56
            }
Thomas@1246
    57
            return;
Thomas@1246
    58
        }
Thomas@1246
    59
Thomas@1246
    60
        /// <summary>
Thomas@1246
    61
        /// Adds the given warning text to the pEp for Outlook log.
Thomas@1246
    62
        /// </summary>
Thomas@1246
    63
        /// <param name="text">The warning text to add to the log.</param>
Thomas@1246
    64
        public static void Warning(string text)
Thomas@1246
    65
        {
Thomas@1246
    66
            if (text != null)
Thomas@1246
    67
            {
Dean@1249
    68
                Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |W| " + text);
Thomas@1246
    69
            }
Thomas@1246
    70
            return;
Thomas@1246
    71
        }
Thomas@1246
    72
Thomas@1246
    73
        /// <summary>
Thomas@1246
    74
        /// Adds the given verbose text to the pEp for Outlook log only
Thomas@1246
    75
        /// when verbose logging is enabled.
Thomas@1246
    76
        /// </summary>
Thomas@1246
    77
        /// <param name="text">The verbose text to add to the log.</param>
Thomas@1246
    78
        public static void Verbose(string text)
Thomas@1246
    79
        {
Thomas@1246
    80
            if ((text != null) &&
Thomas@1246
    81
                (Globals.ThisAddIn != null) &&
Thomas@1246
    82
                (Globals.ThisAddIn.Settings.IsVerboseLoggingEnabled))
Thomas@1246
    83
            {
Dean@1249
    84
                Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |V| " + text);
Thomas@1246
    85
            }
Thomas@1246
    86
            return;
Thomas@1246
    87
        }
Thomas@1246
    88
Thomas@1246
    89
        /// <summary>
Thomas@1246
    90
        /// Writes the given text to the log file.
Thomas@1246
    91
        /// </summary>
Thomas@1246
    92
        /// <param name="text">The text to write to the log file.</param>
Thomas@1246
    93
        private static void Write(string text)
Thomas@1246
    94
        {
Thomas@1246
    95
            try
Thomas@1246
    96
            {
Thomas@1246
    97
                if (text != null)
Thomas@1246
    98
                {
Thomas@1246
    99
                    lock (mutexLogFile)
Thomas@1246
   100
                    {
Thomas@1246
   101
                        if (Log.logWriter == null)
Thomas@1246
   102
                        {
Thomas@1246
   103
                            // If file exists, set line counter to file line count
Thomas@1246
   104
                            if (File.Exists(Log.GetLogFilePath()))
Thomas@1246
   105
                            {
Thomas@1246
   106
                                try
Thomas@1246
   107
                                {
Thomas@1246
   108
                                    var lines = File.ReadAllLines(Log.GetLogFilePath());
Thomas@1246
   109
                                    logLineCount = lines.Length;
Thomas@1246
   110
                                }
Thomas@1246
   111
                                catch { }
Thomas@1246
   112
                            }
Thomas@1246
   113
Thomas@1246
   114
                            // Open the file
Thomas@1246
   115
                            Log.logWriter = File.AppendText(Log.GetLogFilePath());
Thomas@1246
   116
                        }
Thomas@1246
   117
Thomas@1246
   118
                        Log.logWriter.WriteLine(text);
Thomas@1246
   119
Thomas@1246
   120
                        Log.logLineCount++;
Thomas@1246
   121
Thomas@1246
   122
                        // If the maximum line count was reached, clean up the log file by removing the first lines.
Thomas@1246
   123
                        if (Log.logLineCount >= Log.LOG_MAX_LINES)
Thomas@1246
   124
                        {
Thomas@1246
   125
                            Log.Sanitize();
Thomas@1246
   126
                        }
Thomas@1246
   127
                    }
Thomas@1246
   128
                }
Thomas@1246
   129
            }
Thomas@1246
   130
            catch { }
Thomas@1246
   131
Thomas@1246
   132
            return;
Thomas@1246
   133
        }
Thomas@1246
   134
Thomas@1246
   135
        /// <summary>
Thomas@1246
   136
        /// FIFO function to remove the first lines of the log file when a defined maximum 
Thomas@1246
   137
        /// of lines has been reached. 
Thomas@1246
   138
        /// </summary>
Thomas@1246
   139
        private static void Sanitize()
Thomas@1246
   140
        {
Thomas@1246
   141
            try
Thomas@1246
   142
            {
Thomas@1246
   143
                lock (mutexLogFile)
Thomas@1246
   144
                {
Thomas@1246
   145
                    Log.Close();
Thomas@1246
   146
Thomas@1246
   147
                    var lines = File.ReadAllLines(Log.GetLogFilePath());
Thomas@1246
   148
Thomas@1246
   149
                    Log.Clear();
Thomas@1246
   150
Thomas@1246
   151
                    int startIndex = (lines.Length - (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE));
Thomas@1246
   152
                    if (startIndex < 0)
Thomas@1246
   153
                    {
Thomas@1246
   154
                        startIndex = 0;
Thomas@1246
   155
                    }
Thomas@1246
   156
Thomas@1246
   157
                    using (StreamWriter streamWriter = File.AppendText(Log.GetLogFilePath()))
Thomas@1246
   158
                    {
Thomas@1246
   159
                        for (int i = startIndex; i < lines.Length; i++)
Thomas@1246
   160
                        {
Thomas@1246
   161
                            if (string.IsNullOrEmpty(lines[i]) == false)
Thomas@1246
   162
                            {
Thomas@1246
   163
                                streamWriter.WriteLine(lines[i]);
Thomas@1246
   164
                            }
Thomas@1246
   165
                        }
Thomas@1246
   166
                    }
Thomas@1246
   167
Thomas@1246
   168
                    Log.logLineCount = (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE);
Thomas@1246
   169
                }
Thomas@1246
   170
            }
Thomas@1246
   171
            catch { }
Thomas@1246
   172
Thomas@1246
   173
            return;
Thomas@1246
   174
        }
Thomas@1246
   175
Thomas@1246
   176
        /// <summary>
Thomas@1246
   177
        /// Reads all lines of text from the log file.
Thomas@1246
   178
        /// This will never return null.
Thomas@1246
   179
        /// </summary>
Thomas@1246
   180
        /// <returns>The lines of text in the log file.</returns>
Thomas@1246
   181
        public static string Read()
Thomas@1246
   182
        {
Thomas@1246
   183
            string log = "";
Thomas@1246
   184
            string path;
Thomas@1246
   185
            StreamReader logReader;
Thomas@1246
   186
Thomas@1246
   187
            try
Thomas@1246
   188
            {
Thomas@1246
   189
                Log.Close();
Thomas@1246
   190
Thomas@1246
   191
                lock (mutexLogFile)
Thomas@1246
   192
                {
Thomas@1246
   193
                    path = Log.GetLogFilePath();
Thomas@1246
   194
                    if (File.Exists(path))
Thomas@1246
   195
                    {
Thomas@1246
   196
                        logReader = new StreamReader(path);
Thomas@1246
   197
                        log = logReader.ReadToEnd();
Thomas@1246
   198
Thomas@1246
   199
                        logReader.Close();
Thomas@1246
   200
                        logReader.Dispose();
Thomas@1246
   201
                    }
Thomas@1246
   202
                }
Thomas@1246
   203
Thomas@1246
   204
                // Never allow null
Thomas@1246
   205
                if (log == null)
Thomas@1246
   206
                {
Thomas@1246
   207
                    log = "";
Thomas@1246
   208
                }
Thomas@1246
   209
            }
Thomas@1246
   210
            catch { }
Thomas@1246
   211
Thomas@1246
   212
            return (log);
Thomas@1246
   213
        }
Thomas@1246
   214
Thomas@1246
   215
        /// <summary>
Thomas@1246
   216
        /// Closes and releases any open log file writer.
Thomas@1246
   217
        /// </summary>
Thomas@1246
   218
        public static void Close()
Thomas@1246
   219
        {
Thomas@1246
   220
            try
Thomas@1246
   221
            {
Thomas@1246
   222
                lock (mutexLogFile)
Thomas@1246
   223
                {
Thomas@1246
   224
                    if (Log.logWriter != null)
Thomas@1246
   225
                    {
Thomas@1246
   226
                        Log.logWriter.Flush();
Thomas@1246
   227
                        Log.logWriter.Close();
Thomas@1246
   228
                        Log.logWriter.Dispose();
Thomas@1246
   229
                        Log.logWriter = null;
Thomas@1246
   230
                    }
Thomas@1246
   231
                }
Thomas@1246
   232
            }
Thomas@1246
   233
            catch { }
Thomas@1246
   234
Thomas@1246
   235
            return;
Thomas@1246
   236
        }
Thomas@1246
   237
Thomas@1246
   238
        /// <summary>
Thomas@1246
   239
        /// Clears all logged text by deleting the file.
Thomas@1246
   240
        /// </summary>
Thomas@1246
   241
        public static void Clear()
Thomas@1246
   242
        {
Thomas@1246
   243
            string path;
Thomas@1246
   244
Thomas@1246
   245
            try
Thomas@1246
   246
            {
Thomas@1246
   247
                Log.Close();
Thomas@1246
   248
Thomas@1246
   249
                lock (mutexLogFile)
Thomas@1246
   250
                {
Thomas@1246
   251
                    path = Log.GetLogFilePath();
Thomas@1246
   252
                    if (File.Exists(path))
Thomas@1246
   253
                    {
Thomas@1246
   254
                        File.Delete(path);
Thomas@1246
   255
                    }
Thomas@1246
   256
                }
Thomas@1246
   257
            }
Thomas@1246
   258
            catch { }
Thomas@1246
   259
Thomas@1246
   260
            return;
Thomas@1246
   261
        }
Thomas@1246
   262
    }
Thomas@1246
   263
}