Advertisement
Guest User

AuditLog Example

a guest
Jun 16th, 2012
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.98 KB | None | 0 0
  1. // AuditLog.cs
  2. // ------------------------------------------------------------------
  3. //
  4.  
  5. using System;
  6. using System.IO;
  7. using System.Text;
  8. using System.Collections.Generic;
  9. using System.Xml;
  10. using System.Xml.Serialization;
  11.  
  12.  
  13. namespace Cheeso.Examples
  14. {
  15.     public class Customer
  16.     {
  17.         public string Name    { get; set; }
  18.         public string City    { get; set; }
  19.         public String Country { get; set; }
  20.     }
  21.  
  22.     public enum Flavor
  23.     {
  24.         Edit,
  25.         Delete
  26.         // more...
  27.     }
  28.  
  29.     public class AuditRecord<T>
  30.     {
  31.         public AuditRecord() { Stamp = DateTime.Now; }
  32.  
  33.         [XmlAttribute("action")]
  34.         public Flavor Action  { get; set;}
  35.  
  36.         [XmlAttribute("stamp")]
  37.         public DateTime Stamp   { get; set;}
  38.  
  39.         public T Before;
  40.         public T After;
  41.     }
  42.  
  43.     public class AuditLog<T>
  44.     {
  45.         [XmlIgnore]
  46.         public string StoragePath   { get; set; }
  47.  
  48.         public int UserID   { get; set; }
  49.         public string LastSaved   { get; set;}
  50.         private List<AuditRecord<T>> _records;
  51.  
  52.         [XmlArrayItem("Entry")]
  53.         public List<AuditRecord<T>> Records
  54.         {
  55.             get
  56.             {
  57.                 if (_records==null)
  58.                     _records = new List<AuditRecord<T>>();
  59.                 return _records;
  60.             }
  61.  
  62.             set
  63.             {
  64.                 _records = value;
  65.             }
  66.         }
  67.  
  68.         public void RecordEdit(T before, T after)
  69.         {
  70.             // a convenience method
  71.             var r = new AuditRecord<T>
  72.                 {
  73.                     Before = before,
  74.                     After = after,
  75.                     Action = Flavor.Edit
  76.                 };
  77.             Records.Add(r);
  78.         }
  79.  
  80.         public void RecordDelete(T before)
  81.         {
  82.             // a convenience method
  83.             var r = new AuditRecord<T>
  84.                 {
  85.                     Before = before,
  86.                     Action = Flavor.Delete
  87.                 };
  88.             Records.Add(r);
  89.         }
  90.  
  91.  
  92.         TextWriter GetWriter(bool wantSave)
  93.         {
  94.             if (wantSave)
  95.             {
  96.                 var fs = new FileStream(StoragePath, FileMode.Create);
  97.                 return new StreamWriter(fs, new UTF8Encoding());
  98.             }
  99.             return Console.Out;
  100.         }
  101.  
  102.         public void Show()
  103.         {
  104.             SaveShow(false);
  105.         }
  106.  
  107.         public void Save()
  108.         {
  109.             SaveShow(true);
  110.         }
  111.  
  112.         protected internal void SaveShow(bool wantSave)
  113.         {
  114.             var writerSettings = new XmlWriterSettings
  115.             {
  116.                 OmitXmlDeclaration = true,
  117.                 Indent = true
  118.             };
  119.  
  120.             using (XmlWriter xmlWriter =
  121.                    XmlWriter.Create(GetWriter(wantSave), writerSettings))
  122.             {
  123.                 var ns = new XmlSerializerNamespaces();
  124.                 ns.Add("", "");
  125.                 var ser = new XmlSerializer(this.GetType());
  126.                 this.LastSaved = System.DateTime.Now.ToString("yyyy MMM dd HH:mm:ss");
  127.                 ser.Serialize(xmlWriter, this, ns);
  128.             }
  129.         }
  130.     }
  131.  
  132.  
  133.     public class Program
  134.     {
  135.         public static void Main(string[] args)
  136.         {
  137.             try
  138.             {
  139.                 var log = new AuditLog<Customer>();
  140.                 var oldCustomer = new Customer
  141.                 {
  142.                     Name = "Sheldon", City = "Ipswich", Country = "UK"
  143.                 };
  144.                 var newCustomer = new Customer
  145.                 {
  146.                     Name = "Sheldon", City = "London", Country = "UK"
  147.                 };
  148.  
  149.                 log.RecordEdit(oldCustomer, newCustomer);
  150.                 log.RecordDelete(newCustomer);
  151.  
  152.                 log.Show();
  153.             }
  154.             catch (System.Exception exc1)
  155.             {
  156.                 Console.WriteLine("Exception: {0}", exc1.ToString());
  157.             }
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement