LogSplicingComponentSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. namespace ET
  10. {
  11. public class LogSplicingComponentAwakeSystem : AwakeSystem<LogSplicingComponent>
  12. {
  13. public override void Awake(LogSplicingComponent self)
  14. {
  15. }
  16. }
  17. public class LogSplicingComponentDestroySystem : DestroySystem<LogSplicingComponent>
  18. {
  19. public override void Destroy(LogSplicingComponent self)
  20. {
  21. }
  22. }
  23. public static class LogSplicingComponentSystem
  24. {
  25. /// <summary>
  26. /// 拼接日志数据
  27. /// </summary>
  28. /// <param name="baseObj"></param>
  29. /// <returns></returns>
  30. public static string LogObjectToStr(this LogSplicingComponent self, ILogBase baseObj)
  31. {
  32. return JsonHelper.ToJson(baseObj);
  33. }
  34. // 注释不删除
  35. // /// <summary>
  36. // /// 拼接日志数据
  37. // /// </summary>
  38. // /// <param name="baseObj"></param>
  39. // /// <returns></returns>
  40. // public static string LogObjectToStr(this LogSplicingComponent self, ILogBase baseObj)
  41. // {
  42. // string logData = string.Empty;
  43. // Type baseObjType = baseObj.GetType();
  44. // //表名
  45. // var loggerTypeName = baseObjType.GetCustomAttribute<LogNameAttribute>().Name;
  46. //
  47. // //检索表示在指定类型上定义的所有字段的集合
  48. // IEnumerable<FieldInfo> fieldInfos = baseObjType.GetRuntimeFields();
  49. //
  50. // fieldInfos = fieldInfos.OrderBy(a => a.Name).Where(a => a.GetCustomAttribute<LogColumnAttribute>() != null).OrderBy(a => a.Name);
  51. //
  52. // self.keyFieldInfos.Clear();
  53. // foreach (var item in fieldInfos)
  54. // {
  55. // LogColumnAttribute logAtt = item.GetCustomAttribute<LogColumnAttribute>();
  56. // string fieldName = self.GetOrDefualt(logAtt.name, item.Name);
  57. // if (self.keyFieldInfos.ContainsKey(fieldName))
  58. // {
  59. // self.keyFieldInfos[fieldName] = item;
  60. // }
  61. // else
  62. // {
  63. // self.keyFieldInfos.Add(fieldName, item);
  64. // }
  65. // }
  66. //
  67. // var keyFieldInfoss = self.keyFieldInfos.OrderBy(a => a.Key);
  68. //
  69. // if (keyFieldInfoss != null)
  70. // {
  71. // int index = 0;
  72. // foreach (var item in keyFieldInfoss) //全部统一使用字段名进行 升序 排序--结构亦如此
  73. // {
  74. // var fieldValue = item.Value.GetValue(baseObj);
  75. //
  76. // if (fieldValue != null)
  77. // {
  78. // fieldValue = Regex.Replace(fieldValue.ToString(), @"[\n`|$@]", "",
  79. // RegexOptions.IgnoreCase);
  80. // }
  81. //
  82. // index++;
  83. // //最后一个不拼接"|"
  84. // if (index == fieldInfos.Count())
  85. // {
  86. // logData += fieldValue;
  87. // }
  88. // else
  89. // {
  90. // logData += fieldValue + "|";
  91. // }
  92. // }
  93. // }
  94. //
  95. // logData = InsertSpaceBeforeUpperCase(loggerTypeName.ToString()) + "|" + logData;
  96. // return logData;
  97. // }
  98. // private static string GetOrDefualt(this LogSplicingComponent self, string value,
  99. // string defualt)
  100. // {
  101. // if (string.IsNullOrEmpty(value))
  102. // {
  103. // return defualt;
  104. // }
  105. //
  106. // return value;
  107. // }
  108. //
  109. // private static string InsertSpaceBeforeUpperCase(string str)
  110. // {
  111. // var sb = new StringBuilder();
  112. //
  113. // char previousChar = char.MinValue; // Unicode '\0'
  114. //
  115. // foreach (char c in str)
  116. // {
  117. // if (char.IsUpper(c))
  118. // {
  119. // // If not the first character and previous character is not a space, insert a space before uppercase
  120. //
  121. // if (sb.Length != 0 && previousChar != ' ')
  122. // {
  123. // sb.Append('_');
  124. // }
  125. // }
  126. //
  127. // sb.Append(c);
  128. //
  129. // previousChar = c;
  130. // }
  131. //
  132. // return sb.ToString();
  133. // }
  134. }
  135. }