Sometimes I use a console logger, for simple apps.and it’s more important to see the data than the text for a programmer.
I have written a simple Console encoding app that spits color encoded console lines to the console.
I may some day write a log4net logger that does this, but for now:
public static string EncodeConsole(string message
, params object[] args)
{
string content = message;
string pattern = @"{(.*?)}";
MatchCollection mc = Regex.Matches(content, pattern
,RegexOptions.Compiled); //compiled is faster
#region make better Exception that string.format
if (mc.Count > args.Length)
{
string format1 = "you supplied {0} params, but require {1} in the template";
///erro message
string err = string.Format(format1, args.Length, mc.Count);
throw new FormatException( err );
}
#endregion
string encoded = message;
foreach (Match item in mc)
encoded = encoded.Replace(item.Value, ",#,").Replace(",,", ",");
var arr = encoded.Split(',');
string out_ = "";
int matchnum = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i][0] == '#')
{
Console.ForegroundColor = ConsoleColor.White;
Console.BackgroundColor = ConsoleColor.Blue;
var oldmatch = mc[matchnum].Value;
var g = oldmatch.Split(':');
//will always be 0 for data
string match = "{0}";
if (g.Length > 1)
match = "{0:" + g[1];
var data = string.Format(match, args[matchnum]);
arr[i] = data;
matchnum++;
}
else
Console.ResetColor();
Console.Write(arr[i]);
out_ += arr[i];
}
Console.WriteLine("");
return out_;
}