Catch wrongly formatted log strings
authorThomas
Mon, 27 Aug 2018 11:10:40 +0200
changeset 23507103795126a0
parent 2349 a65f67dcb335
child 2351 1a6bc4294582
Catch wrongly formatted log strings
Log.cs
     1.1 --- a/Log.cs	Mon Aug 27 10:45:29 2018 +0200
     1.2 +++ b/Log.cs	Mon Aug 27 11:10:40 2018 +0200
     1.3 @@ -39,13 +39,14 @@
     1.4          /// Adds the given info text to the pEp for Outlook log.
     1.5          /// </summary>
     1.6          /// <param name="text">The text to add to the log.</param>
     1.7 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
     1.8          public static void Info(string text, params object[] args)
     1.9          {
    1.10              if (text != null)
    1.11              {
    1.12                  try
    1.13                  {
    1.14 -                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " | " + string.Format(text, args));
    1.15 +                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " | " + Log.Format(text, args));
    1.16                  }
    1.17                  catch { }
    1.18              }
    1.19 @@ -55,13 +56,14 @@
    1.20          /// Adds the given error text to the pEp for Outlook log.
    1.21          /// </summary>
    1.22          /// <param name="text">The error text to add to the log.</param>
    1.23 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
    1.24          public static void Error(string text, params object[] args)
    1.25          {
    1.26              if (text != null)
    1.27              {
    1.28                  try
    1.29                  {
    1.30 -                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |E| " + string.Format(text, args));
    1.31 +                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |E| " + Log.Format(text, args));
    1.32                  }
    1.33                  catch { }
    1.34              }
    1.35 @@ -72,6 +74,7 @@
    1.36          /// when sensitive data logging is enabled.
    1.37          /// </summary>
    1.38          /// <param name="text">The sensitive text to add to the log.</param>
    1.39 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
    1.40          public static void SensitiveData(string text, params object[] args)
    1.41          {
    1.42              if ((text != null) &&
    1.43 @@ -80,7 +83,7 @@
    1.44              {
    1.45                  try
    1.46                  {
    1.47 -                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |S| " + string.Format(text, args));
    1.48 +                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |S| " + Log.Format(text, args));
    1.49                  }
    1.50                  catch { }
    1.51              }
    1.52 @@ -90,13 +93,14 @@
    1.53          /// Adds the given warning text to the pEp for Outlook log.
    1.54          /// </summary>
    1.55          /// <param name="text">The warning text to add to the log.</param>
    1.56 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
    1.57          public static void Warning(string text, params object[] args)
    1.58          {
    1.59              if (text != null)
    1.60              {
    1.61                  try
    1.62                  {
    1.63 -                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |W| " + string.Format(text, args));
    1.64 +                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |W| " + Log.Format(text, args));
    1.65                  }
    1.66                  catch { }
    1.67              }
    1.68 @@ -107,6 +111,7 @@
    1.69          /// when verbose logging is enabled.
    1.70          /// </summary>
    1.71          /// <param name="text">The verbose text to add to the log.</param>
    1.72 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
    1.73          public static void Verbose(string text, params object[] args)
    1.74          {
    1.75              if ((text != null) &&
    1.76 @@ -114,7 +119,7 @@
    1.77              {
    1.78                  try
    1.79                  {
    1.80 -                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |V| " + string.Format(text, args));
    1.81 +                    Log.Write(DateTime.Now.ToString("HH:mm:ss.fff") + " |V| " + Log.Format(text, args));
    1.82                  }
    1.83                  catch { }
    1.84              }
    1.85 @@ -209,6 +214,30 @@
    1.86          }
    1.87  
    1.88          /// <summary>
    1.89 +        /// Formats the input text and returns some information about
    1.90 +        /// potential exceptions that occur during formatting.
    1.91 +        /// </summary>
    1.92 +        /// <param name="text">A composite format string</param>
    1.93 +        /// <param name="args">An object array that contains zero or more objects to format.</param>
    1.94 +        /// <returns>A copy of format in which the format items have been replaced by the string representation 
    1.95 +        /// of the corresponding objects in args.</returns>
    1.96 +        public static string Format(string text, params object[] args)
    1.97 +        {
    1.98 +            string formattedText = null;
    1.99 +
   1.100 +            try
   1.101 +            {
   1.102 +                formattedText = string.Format(text, args);
   1.103 +            }
   1.104 +            catch (Exception ex)
   1.105 +            {
   1.106 +                formattedText = "Error formatting error message. Text: " + text + ". Exception: " + ex.ToString();
   1.107 +            }
   1.108 +
   1.109 +            return formattedText;
   1.110 +        }
   1.111 +
   1.112 +        /// <summary>
   1.113          /// Closes and releases any open log file writer.
   1.114          /// </summary>
   1.115          public static void Close()