Advertisement
fortsoft

ErrorLog

Dec 12th, 2022 (edited)
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | Source Code | 0 0
  1. /**
  2.  * This is open-source software licensed under the terms of the MIT License.
  3.  *
  4.  * Copyright (c) 2020-2023 Petr Červinka - FortSoft <[email protected]>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  **
  24.  * Version 1.1.0.0
  25.  */
  26.  
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. using System.IO;
  31. using System.Text;
  32. using System.Windows.Forms;
  33.  
  34. namespace FortSoft.Tools {
  35.  
  36.     /// <summary>
  37.     /// Simple error logging class.
  38.     /// </summary>
  39.     public static class ErrorLog {
  40.  
  41.         /// <summary>
  42.         /// A method to split the stack trace into an array of strings.
  43.         /// </summary>
  44.         /// <param name="stackTrace">Input string.</param>
  45.         /// <returns>Array of strings.</returns>
  46.         private static string[] SplitStackTrace(string stackTrace) {
  47.             List<string> lines = new List<string>();
  48.             using (StringReader stringReader = new StringReader(stackTrace)) {
  49.                 for (string line; (line = stringReader.ReadLine()) != null;) {
  50.                     lines.Add(line);
  51.                 }
  52.             }
  53.             return lines.ToArray();
  54.         }
  55.  
  56.         /// <summary>
  57.         /// A method to log an exception.
  58.         /// </summary>
  59.         /// <param name="exception">An instance of the Exception class.</param>
  60.         public static void WriteLine(Exception exception) {
  61.             try {
  62.                 string filePath = Path.Combine(Application.LocalUserAppDataPath, Constants.ErrorLogFileName);
  63.                 using (StreamWriter streamWriter = File.AppendText(filePath)) {
  64.                     StringBuilder stringBuilder = new StringBuilder()
  65.                         .Append(DateTime.Now.ToString(Constants.ErrorLogTimeFormat))
  66.                         .Append(Constants.VerticalTab)
  67.                         .Append(exception.TargetSite.Name)
  68.                         .Append(Constants.VerticalTab)
  69.                         .Append(exception.GetType().FullName)
  70.                         .Append(Constants.VerticalTab)
  71.                         .Append(exception.Message);
  72.                     string[] stackTrace = SplitStackTrace(exception.StackTrace);
  73.                     if (stackTrace.Length > 0) {
  74.                         stringBuilder.Append(Constants.VerticalTab);
  75.                         stringBuilder.Append(stackTrace[stackTrace.Length - 1]);
  76.                     }
  77.                     streamWriter.WriteLine(stringBuilder.ToString());
  78.                 }
  79.             } catch (Exception e) {
  80.                 Debug.WriteLine(e);
  81.             }
  82.         }
  83.  
  84.         /// <summary>
  85.         /// A method to log an error message.
  86.         /// </summary>
  87.         /// <param name="errorMessage">Error message.</param>
  88.         public static void WriteLine(string errorMessage) {
  89.             try {
  90.                 string filePath = Path.Combine(Application.LocalUserAppDataPath, Constants.ErrorLogFileName);
  91.                 using (StreamWriter streamWriter = File.AppendText(filePath)) {
  92.                     StringBuilder stringBuilder = new StringBuilder()
  93.                         .Append(DateTime.Now.ToString(Constants.ErrorLogTimeFormat))
  94.                         .Append(Constants.VerticalTab)
  95.                         .Append(Constants.ErrorLogErrorMessage)
  96.                         .Append(Constants.Colon)
  97.                         .Append(Constants.VerticalTab);
  98.                     if (errorMessage == null) {
  99.                         stringBuilder.Append(Constants.ErrorLogNull);
  100.                     } else if (errorMessage.Equals(string.Empty)) {
  101.                         stringBuilder.Append(Constants.ErrorLogEmptyString);
  102.                     } else if (string.IsNullOrWhiteSpace(errorMessage)) {
  103.                         stringBuilder.Append(Constants.ErrorLogWhiteSpace);
  104.                     } else {
  105.                         stringBuilder.Append(errorMessage);
  106.                     }
  107.                     streamWriter.WriteLine(stringBuilder.ToString());
  108.                 }
  109.             } catch (Exception e) {
  110.                 Debug.WriteLine(e);
  111.             }
  112.         }
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement