LoggingContext.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace BestHTTP.Logger
  5. {
  6. public sealed class LoggingContext
  7. {
  8. private enum LoggingContextFieldType
  9. {
  10. Long,
  11. Bool,
  12. String,
  13. AnotherContext
  14. }
  15. private struct LoggingContextField
  16. {
  17. public string key;
  18. public long longValue;
  19. public bool boolValue;
  20. public string stringValue;
  21. public LoggingContext loggingContextValue;
  22. public LoggingContextFieldType fieldType;
  23. }
  24. private List<LoggingContextField> fields = null;
  25. private static System.Random random = new System.Random();
  26. private LoggingContext() { }
  27. public LoggingContext(object boundto)
  28. {
  29. var name = boundto.GetType().Name;
  30. Add("TypeName", name);
  31. long hash = 0;
  32. lock (random)
  33. hash = ((long)boundto.GetHashCode() << 32 | (long)name.GetHashCode()) ^ ((long)this.GetHashCode() << 16) | (long)(random.Next(int.MaxValue) << 32) | (long)random.Next(int.MaxValue);
  34. Add("Hash", hash);
  35. }
  36. public void Add(string key, long value)
  37. {
  38. Add(new LoggingContextField { fieldType = LoggingContextFieldType.Long, key = key, longValue = value });
  39. }
  40. public void Add(string key, bool value)
  41. {
  42. Add(new LoggingContextField { fieldType = LoggingContextFieldType.Bool, key = key, boolValue = value });
  43. }
  44. public void Add(string key, string value)
  45. {
  46. Add(new LoggingContextField { fieldType = LoggingContextFieldType.String, key = key, stringValue = value });
  47. }
  48. public void Add(string key, LoggingContext value)
  49. {
  50. Add(new LoggingContextField { fieldType = LoggingContextFieldType.AnotherContext, key = key, loggingContextValue = value });
  51. }
  52. private void Add(LoggingContextField field)
  53. {
  54. if (this.fields == null)
  55. this.fields = new List<LoggingContextField>();
  56. Remove(field.key);
  57. this.fields.Add(field);
  58. }
  59. public void Remove(string key)
  60. {
  61. this.fields.RemoveAll(field => field.key == key);
  62. }
  63. public LoggingContext Clone()
  64. {
  65. LoggingContext newContext = new LoggingContext();
  66. if (this.fields != null && this.fields.Count > 0)
  67. {
  68. newContext.fields = new List<LoggingContextField>(this.fields.Count);
  69. for (int i = 0; i < this.fields.Count; ++i)
  70. {
  71. var field = this.fields[i];
  72. switch (field.fieldType)
  73. {
  74. case LoggingContextFieldType.Long:
  75. case LoggingContextFieldType.Bool:
  76. case LoggingContextFieldType.String:
  77. newContext.fields.Add(field);
  78. break;
  79. case LoggingContextFieldType.AnotherContext:
  80. newContext.Add(field.key, field.loggingContextValue.Clone());
  81. break;
  82. }
  83. }
  84. }
  85. return newContext;
  86. }
  87. public void ToJson(System.Text.StringBuilder sb)
  88. {
  89. if (this.fields == null || this.fields.Count == 0)
  90. {
  91. sb.Append("null");
  92. return;
  93. }
  94. sb.Append("{");
  95. for (int i = 0; i < this.fields.Count; ++i)
  96. {
  97. var field = this.fields[i];
  98. if (field.fieldType != LoggingContextFieldType.AnotherContext)
  99. {
  100. if (i > 0)
  101. sb.Append(", ");
  102. sb.AppendFormat("\"{0}\": ", field.key);
  103. }
  104. switch (field.fieldType)
  105. {
  106. case LoggingContextFieldType.Long:
  107. sb.Append(field.longValue);
  108. break;
  109. case LoggingContextFieldType.Bool:
  110. sb.Append(field.boolValue ? "true" : "false");
  111. break;
  112. case LoggingContextFieldType.String:
  113. sb.AppendFormat("\"{0}\"", Escape(field.stringValue));
  114. break;
  115. }
  116. }
  117. sb.Append("}");
  118. for (int i = 0; i < this.fields.Count; ++i)
  119. {
  120. var field = this.fields[i];
  121. switch (field.fieldType)
  122. {
  123. case LoggingContextFieldType.AnotherContext:
  124. sb.Append(", ");
  125. field.loggingContextValue.ToJson(sb);
  126. break;
  127. }
  128. }
  129. }
  130. public static string Escape(string original)
  131. {
  132. return PlatformSupport.Text.StringBuilderPool.ReleaseAndGrab(PlatformSupport.Text.StringBuilderPool.Get(1)
  133. .Append(original)
  134. .Replace("\\", "\\\\")
  135. .Replace("\"", "\\\"")
  136. .Replace("/", "\\/")
  137. .Replace("\b", "\\b")
  138. .Replace("\f", "\\f")
  139. .Replace("\n", "\\n")
  140. .Replace("\r", "\\r")
  141. .Replace("\t", "\\t"));
  142. }
  143. }
  144. }