Log.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SharpCifs.Util.DbsHelper
  8. {
  9. public class Log
  10. {
  11. /// <summary>
  12. /// コンソールへのログ出力を行うか否か
  13. /// </summary>
  14. public static bool IsActive { get; set; } = false;
  15. public static void Out(string message)
  16. {
  17. if (!Log.IsActive
  18. || string.IsNullOrEmpty(message))
  19. return;
  20. var msg = DateTime.Now.ToString("HH:mm:ss.fff")
  21. + ": [ThID: "
  22. + System.Environment.CurrentManagedThreadId.ToString().PadLeft(3)
  23. + " "
  24. + message;
  25. Debug.WriteLine(msg);
  26. Console.WriteLine(msg);
  27. }
  28. /// <summary>
  29. /// 例外のログ出力を行う。
  30. /// </summary>
  31. /// <param name="ex"></param>
  32. public static void Out(Exception ex)
  33. {
  34. if (!Log.IsActive
  35. || ex == null)
  36. return;
  37. Log.Out($"{ex}");
  38. var message = Log.GetHighlighted(Log.GetErrorString(ex));
  39. Log.Out(message);
  40. }
  41. /// <summary>
  42. /// Cast string-arrary to Highlighted message
  43. /// 文字列配列を強調メッセージ形式文字列に変換する。
  44. /// </summary>
  45. /// <param name="messages"></param>
  46. /// <returns></returns>
  47. private static string GetHighlighted(params System.String[] messages)
  48. {
  49. var time = DateTime.Now;
  50. var list = new List<string>();
  51. list.Add("");
  52. list.Add("");
  53. list.Add(time.ToString("HH:mm:ss.fff") + ":");
  54. list.Add("##################################################");
  55. list.Add("#");
  56. foreach (string message in messages)
  57. {
  58. var lines = message.Replace("\r\n", "\n").Replace("\r", "\n").Trim('\n').Split('\n');
  59. foreach (var line in lines)
  60. {
  61. list.Add($"# {line}");
  62. }
  63. }
  64. list.Add("#");
  65. list.Add("##################################################");
  66. list.Add("");
  67. list.Add("");
  68. return string.Join("\r\n", list);
  69. }
  70. /// <summary>
  71. /// Get Formatted Exception-Info string-array
  72. /// 例外情報を整形した文字列配列を返す。
  73. /// </summary>
  74. /// <param name="ex"></param>
  75. /// <returns></returns>
  76. private static string[] GetErrorString(Exception ex)
  77. {
  78. var list = new List<string>();
  79. if (ex.Message != null)
  80. {
  81. list.Add(ex.Message ?? "");
  82. list.Add("");
  83. }
  84. if (ex.StackTrace != null)
  85. {
  86. list.AddRange(ex.StackTrace.Split(new string[] { "場所", "at " },
  87. StringSplitOptions.None)
  88. .AsEnumerable()
  89. .Select(row => "\r\nat " + row));
  90. }
  91. if (ex.InnerException != null)
  92. {
  93. //InnerExceptionを再帰取得する。
  94. list.Add("");
  95. list.Add("Inner Exception");
  96. list.AddRange(Log.GetErrorString(ex.InnerException));
  97. }
  98. return list.ToArray();
  99. }
  100. }
  101. }