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