LoggingContext.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 LoggingContext() { }
  26. public LoggingContext(object boundto)
  27. {
  28. Add("TypeName", boundto.GetType().Name);
  29. Add("Hash", boundto.GetHashCode());
  30. }
  31. public void Add(string key, long value)
  32. {
  33. Add(new LoggingContextField { fieldType = LoggingContextFieldType.Long, key = key, longValue = value });
  34. }
  35. public void Add(string key, bool value)
  36. {
  37. Add(new LoggingContextField { fieldType = LoggingContextFieldType.Bool, key = key, boolValue = value });
  38. }
  39. public void Add(string key, string value)
  40. {
  41. Add(new LoggingContextField { fieldType = LoggingContextFieldType.String, key = key, stringValue = value });
  42. }
  43. public void Add(string key, LoggingContext value)
  44. {
  45. Add(new LoggingContextField { fieldType = LoggingContextFieldType.AnotherContext, key = key, loggingContextValue = value });
  46. }
  47. private void Add(LoggingContextField field)
  48. {
  49. if (this.fields == null)
  50. this.fields = new List<LoggingContextField>();
  51. Remove(field.key);
  52. this.fields.Add(field);
  53. }
  54. public void Remove(string key)
  55. {
  56. this.fields.RemoveAll(field => field.key == key);
  57. }
  58. public LoggingContext Clone()
  59. {
  60. LoggingContext newContext = new LoggingContext();
  61. if (this.fields != null && this.fields.Count > 0)
  62. {
  63. newContext.fields = new List<LoggingContextField>(this.fields.Count);
  64. for (int i = 0; i < this.fields.Count; ++i)
  65. {
  66. var field = this.fields[i];
  67. switch (field.fieldType)
  68. {
  69. case LoggingContextFieldType.Long:
  70. case LoggingContextFieldType.Bool:
  71. case LoggingContextFieldType.String:
  72. newContext.fields.Add(field);
  73. break;
  74. case LoggingContextFieldType.AnotherContext:
  75. newContext.Add(field.key, field.loggingContextValue.Clone());
  76. break;
  77. }
  78. }
  79. }
  80. return newContext;
  81. }
  82. public void ToJson(System.Text.StringBuilder sb)
  83. {
  84. if (this.fields == null || this.fields.Count == 0)
  85. {
  86. sb.Append("null");
  87. return;
  88. }
  89. sb.Append("{");
  90. for (int i = 0; i < this.fields.Count; ++i)
  91. {
  92. var field = this.fields[i];
  93. if (field.fieldType != LoggingContextFieldType.AnotherContext)
  94. {
  95. if (i > 0)
  96. sb.Append(", ");
  97. sb.AppendFormat("\"{0}\": ", field.key);
  98. }
  99. switch (field.fieldType)
  100. {
  101. case LoggingContextFieldType.Long:
  102. sb.Append(field.longValue);
  103. break;
  104. case LoggingContextFieldType.Bool:
  105. sb.Append(field.boolValue ? "true" : "false");
  106. break;
  107. case LoggingContextFieldType.String:
  108. sb.AppendFormat("\"{0}\"", Escape(field.stringValue));
  109. break;
  110. }
  111. }
  112. sb.Append("}");
  113. for (int i = 0; i < this.fields.Count; ++i)
  114. {
  115. var field = this.fields[i];
  116. switch (field.fieldType)
  117. {
  118. case LoggingContextFieldType.AnotherContext:
  119. sb.Append(", ");
  120. field.loggingContextValue.ToJson(sb);
  121. break;
  122. }
  123. }
  124. }
  125. public static string Escape(string original)
  126. {
  127. return new StringBuilder(original)
  128. .Replace("\\", "\\\\")
  129. .Replace("\"", "\\\"")
  130. .Replace("/", "\\/")
  131. .Replace("\b", "\\b")
  132. .Replace("\f", "\\f")
  133. .Replace("\n", "\\n")
  134. .Replace("\r", "\\r")
  135. .Replace("\t", "\\t")
  136. .ToString();
  137. }
  138. }
  139. }