Advertisement
uraharadono

Object logger C#

Jun 27th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. public static class ObjectLogger
  2.     {
  3.         public static void LogObjectContent(this object model)
  4.         {
  5.             try
  6.             {
  7.                 SaveObjectToFile(model.GetLogFor());
  8.             }
  9.             catch (Exception e)
  10.             {
  11.                 Console.WriteLine(e);
  12.                 throw;
  13.             }
  14.         }
  15.  
  16.         private static string GetLogFor(this object target)
  17.         {
  18.             var properties =
  19.                 from property in target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
  20.                 select new
  21.                 {
  22.                     Name = property.Name,
  23.                     Value = property.GetValue(target, null)
  24.                 };
  25.  
  26.             var builder = new StringBuilder();
  27.  
  28.             foreach (var property in properties)
  29.             {
  30.                 builder
  31.                     .Append(property.Name)
  32.                     .Append(" = ")
  33.                     .Append(property.Value)
  34.                     .AppendLine();
  35.             }
  36.  
  37.             return builder.ToString();
  38.         }
  39.  
  40.         private static void SaveObjectToFile(string content)
  41.         {
  42.             string path = HostingEnvironment.MapPath(Path.Combine("~/RequestLogs/" + GetFileName()).Trim());
  43.  
  44.             if (!File.Exists(path))
  45.             {
  46.                 // Create a file to write to.
  47.                 using (StreamWriter sw = File.CreateText(path))
  48.                 {
  49.                     sw.WriteLine(content);
  50.                 }
  51.             }
  52.         }
  53.  
  54.         private static string GetFileName()
  55.         {
  56.             var dateNow = DateTime.Now;
  57.             return (dateNow.Day + "꞉"
  58.                     + dateNow.Month + "꞉"
  59.                     + dateNow.Year + "꞉"
  60.                    + "__" //to separate date and hours
  61.                    + DateTime.Now.Hour + "꞉"
  62.                    + DateTime.Now.Minute + "꞉"
  63.                    + DateTime.Now.Second + "꞉"
  64.                    + DateTime.Now.Millisecond
  65.                    + ".txt")
  66.                    .ToSafeFileName();
  67.         }
  68.  
  69.         private static string ToSafeFileName(this string s)
  70.         {
  71.             return s
  72.                 .Replace("\\", "")
  73.                 .Replace("/", "")
  74.                 .Replace("\"", "")
  75.                 .Replace("*", "")
  76.                 .Replace(":", "")
  77.                 .Replace("?", "")
  78.                 .Replace("<", "")
  79.                 .Replace(">", "")
  80.                 .Replace("|", "");
  81.         }
  82.  
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement