/Logging/logg.aspx.cs

http://github.com/teamaton/toolbelt · C# · 61 lines · 50 code · 10 blank · 1 comment · 8 complexity · a29ea4de887641dfa82a2e7ced592f1a MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using System.Web.UI;
  6. using log4net;
  7. namespace Logging
  8. {
  9. public class Logg : Page
  10. {
  11. private static readonly ILog Log = LogManager.GetLogger(typeof(Logg));
  12. private static readonly ILog Log2 = LogManager.GetLogger("Logg");
  13. protected void Page_Load(object sender, EventArgs e)
  14. {
  15. if (Log.IsInfoEnabled)
  16. Log.InfoFormat("Logging away at " + DateTime.Now.ToLongTimeString());
  17. if (Log2.IsDebugEnabled)
  18. Log2.DebugFormat("Logging away at " + DateTime.Now.ToLongTimeString());
  19. }
  20. public string ToDebugString(object obj, int maxdepth, int depth=0)
  21. {
  22. if (obj == null)
  23. return "null";
  24. if (obj is IConvertible)
  25. return obj.ToString();
  26. if (depth >= maxdepth)
  27. return "...";
  28. var sb = new StringBuilder();
  29. if (depth > 0)
  30. sb.AppendLine();
  31. foreach (var propertyInfo in obj.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance))
  32. {
  33. sb.Append(new string(' ', 2*depth)).Append(propertyInfo.Name).Append(": ");
  34. try
  35. {
  36. var value = propertyInfo.GetValue(obj, new object[0]);
  37. sb.AppendLine(ToDebugString(value, maxdepth, depth + 1));
  38. }
  39. catch (Exception ex)
  40. {
  41. sb.AppendLine(string.Format("[{0}]", ex.Message));
  42. }
  43. }
  44. // remove newline from end of string
  45. var newLine = Environment.NewLine;
  46. if (sb.Length >= newLine.Length)
  47. sb.Replace(newLine, "", sb.Length - newLine.Length, newLine.Length);
  48. return sb.ToString();
  49. }
  50. }
  51. }