17 using System.Collections;
18 using System.Globalization;
19 using System.Runtime.CompilerServices;
27 public static class Log
29 private static string _lastTraceText =
"";
30 private static string _lastErrorText =
"";
31 private static bool _debuggingEnabled;
32 private static int _level = 1;
40 get {
return _logHandler; }
41 set { _logHandler = value; }
49 get {
return _debuggingEnabled; }
50 set { _debuggingEnabled = value; }
57 public static string FilePath {
get;
set; } =
"log.txt";
64 get {
return _level; }
65 set { _level = value; }
73 public static void Error(
string error,
bool overrideMessageFloodProtection =
false)
77 if (error == _lastErrorText && !overrideMessageFloodProtection)
return;
78 _logHandler.
Error(error);
79 _lastErrorText = error;
83 Console.WriteLine(
"Log.Error(): Error writing error: " + err.Message);
94 private static void Error(
string method, Exception exception,
string message =
null,
bool overrideMessageFloodProtection =
false)
96 message = method +
"(): " + (message ??
string.Empty) +
" " + exception;
97 Error(message, overrideMessageFloodProtection);
106 [MethodImpl(MethodImplOptions.NoInlining)]
107 public static void Error(Exception exception,
string message =
null,
bool overrideMessageFloodProtection =
false)
115 public static void Trace(
string traceText,
bool overrideMessageFloodProtection =
false)
119 if (traceText == _lastTraceText && !overrideMessageFloodProtection)
return;
120 _logHandler.
Trace(traceText);
121 _lastTraceText = traceText;
123 catch (Exception err)
125 Console.WriteLine(
"Log.Trace(): Error writing trace: " +err.Message);
132 public static void Trace(
string format, params
object[] args)
134 Trace(
string.Format(CultureInfo.InvariantCulture, format, args));
140 public static void Error(
string format, params
object[] args)
142 Error(
string.Format(CultureInfo.InvariantCulture, format, args));
150 public static void Debug(
string text,
int level = 1)
154 if (!_debuggingEnabled || level < _level)
return;
155 _logHandler.
Debug(text);
157 catch (Exception err)
159 Console.WriteLine(
"Log.Debug(): Error writing debug: " + err.Message);
169 public static string VarDump(
object obj,
int recursion = 0)
171 var result =
new StringBuilder();
177 var t = obj.GetType();
180 var properties = t.GetProperties();
182 foreach (var property
in properties)
187 var value =
property.GetValue(obj,
null);
191 var indent = String.Empty;
197 indent =
new StringBuilder(trail).Insert(0, spaces, recursion - 1).ToString();
203 var displayValue = value.ToString();
204 if (value is
string) displayValue = String.Concat(
'"', displayValue,
'"');
207 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, property.Name, displayValue);
211 if (!(value is ICollection))
216 result.Append(
VarDump(value, recursion + 1));
223 var elementCount = 0;
224 foreach (var element
in ((ICollection)value))
226 var elementName = $
"{property.Name}[{elementCount}]";
227 indent =
new StringBuilder(trail).Insert(0, spaces, recursion).ToString();
230 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, elementName, element.ToString());
233 result.Append(
VarDump(element, recursion + 2));
237 result.Append(
VarDump(value, recursion + 1));
244 result.AppendFormat(CultureInfo.InvariantCulture,
"{0}{1} = {2}\n", indent, property.Name,
"null");
255 return result.ToString();