Audit Trail

10 posts, 0 answers
  1. Lafama
    Lafama avatar
    12 posts
    Member since:
    Oct 2009

    Posted 31 Aug 2010 Link to this post

    Hi,
    What is the best way to implement an audit trail for my application.  I have tried the tracker class but i cannot manage to save any object type class field. could there be a better way to approach this..
    Please advice.

    Thanks
  2. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 01 Sep 2010 Link to this post

    Hi Lafama,
    What do you mean with 'tracker'?

    You can hook into the IObjectScope.Tracking events. Inside the event you can collect the data and store the old values in a new table or make a copy of your old object.

    Regards,
    Jan Blessenohl
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Lafama
    Lafama avatar
    12 posts
    Member since:
    Oct 2009

    Posted 03 Sep 2010 Link to this post

    hi Jan
    Thanks for the quick reply..nway i have already tried that but am unable to save my objs states in a the database. apparently the orm does not support the object datatype. Do you have an example {code of how i can achieve this}
    Example of one of my persistent class attributes

    class BussCompany
    {
    private int _id;
    private string _name;
    private IList<Bus> _buses;
    private User _createdBy;
    }
    if i try to save the changing state of each of the following attributes i get an error @ _buses and createdby fields. Please advice
    It is really urgent 

    Thanks
    Lafama
  5. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 06 Sep 2010 Link to this post

    Hi Lafama,
    If you want to store information about changed references or collections you have to add special code. I have just made the following very simple event handler:

    static void Tracking_Changed(object sender, ChangeEventArgs e)
    {
        var scope = Database.GetContext(e.PersistentObject) as IObjectScope;
        scope.Add(
            new AuditTrail()
            {
                PersistentObject = Database.OID.ToString(scope.GetObjectId(e.PersistentObject)),
                OldValue = e.OldValue.ToString(),
                NewValue = e.NewValue.ToString(),
                Changed = DateTime.Now,
            });
    }

    With a persistent class:

    class AuditTrail
    {
        public string PersistentObject;
        public string OldValue;
        public string NewValue;
        public DateTime Changed;
    }

    Where do you get errors and of which kind?

    Regards,
    Jan Blessenohl
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Lafama
    Lafama avatar
    12 posts
    Member since:
    Oct 2009

    Posted 06 Sep 2010 Link to this post

    hi,
    My main concern is  will this help me to track references or collections. 
    I have tried your code above nothing seems to work. Could you please give me a sample code on how i can track the references or collections within a given object. It is really important
    (Now 7++++ days and am still waiting for a clear answer )

    Seriously ??
  7. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 07 Sep 2010 Link to this post

    Hello Lafama,
    There is no explicit audit trail implementation as part of OpenAccess, we are not able to do a common implementation because the requirements are too different. Some projects just need an entry that a field has been changed and by whom, some need before and after values and some need complete copies of the data. It depends what you want to do with the information.

    OpenAccess gives you access to everything you need to do your own implementation. You are getting the collection with the content before your change and the collection content of the collection after your change. You have to decide and implement what you want to store, if you want to clone the old object, just do it inside the changed event. 

     Because I do not know what you need I cannot provide you with a complete example. Please give us more information and a first implementation from you also helps to understand your problems.


    Jan Blessenohl
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. La
    La avatar
    1 posts
    Member since:
    Sep 2010

    Posted 07 Sep 2010 Link to this post

    hi,
    Please give examples on
    1. tracking the Collections before and after change
    2. Storing the entire object in a database
    3.tracking the references before and after change events

    thanks
  9. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 07 Sep 2010 Link to this post

    Hello La,
    here is my example code:

    using System;
    using System.Collections.Generic;
    using Telerik.OpenAccess;
      
    namespace AuditTrail
    {
        class Program
        {
            static void Main(string[] args)
            {
                var db = Database.Get("DatabaseConnection1");
                var scope = db.GetObjectScope();
                scope.Tracking.Changed += Tracking_Changed;
                scope.Tracking.Changing += Tracking_Changing;
      
                A a = new A();
                a.Name = "orig";
                a.B = new B();
                a.Cs = new List<C>();
                a.Cs.Add(new C());
                a.Cs.Add(new C());
      
                scope.Transaction.Begin();
                scope.Add(a);
                scope.Transaction.Commit();
      
                scope.Transaction.Begin();
                a.Name = "changed";
                a.B = new B();
                a.Cs.RemoveAt(0);
                a.Cs.Add(new C());
                scope.Transaction.Commit();
            }
      
            static void Tracking_Changing(object sender, ChangeEventArgs e)
            {
                var scope = Database.GetContext(e.PersistentObject) as IObjectScope;
                if (e.PersistentObject is A)
                {
                    // make a clone of the old object and store it in a second table
                    if (e.WasDirty == false)
                        scope.Add(new AClone((A)e.PersistentObject));
                    // store old list content 
                    if (e.FieldName == "cs")
                        e.Tag = new List<C>(((A)e.PersistentObject).Cs);
                }
            }
      
            static void Tracking_Changed(object sender, ChangeEventArgs e)
            {
                var scope = Database.GetContext(e.PersistentObject) as IObjectScope;
                if (e.PersistentObject is A)
                {
                    if (e.FieldName == "cs")
                    {
                        var oldValue = e.Tag as List<C>;
                        var newValue = new List<C>(((A)e.PersistentObject).Cs);
                        // what do you want to do now with these lists?
                    }
                    else if (e.FieldName == "b")
                    {
                        var oldValue1 = e.OldValue as B ; 
                        var newValue1 = e.NewValue as B;
                        // what do you want to do now with these references?
                    }
                    //...
                }
            }
      
        }
      
        [Persistent]
        class A
        {
            private string name;
            private B b;
            private IList<C> cs;
      
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public B B
            {
                get
                {
                    return b;
                }
                set
                {
                    b = value;
                }
            }
            public IList<C> Cs
            {
                get
                {
                    return cs;
                }
                set
                {
                    cs = value;
                }
            }
        }
      
        [Persistent]
        class AClone
        {
            private string name;
            private B b;
            private IList<C> cs;
            private DateTime changed;
      
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            public B B
            {
                get
                {
                    return b;
                }
                set
                {
                    b = value;
                }
            }
            public IList<C> Cs
            {
                get
                {
                    return cs;
                }
                set
                {
                    cs = value;
                }
            }
            public DateTime Changed
            {
                get
                {
                    return changed;
                }
                set
                {
                    changed = value;
                }
            }
      
            public AClone()
            {
      
            }
      
            public AClone(A other)
            {
                Name = other.Name;
                B = other.B;
                Cs = new List<C>(other.Cs);
                Changed = DateTime.Now;
            }
        }
      
        [Persistent]
        class B
        {
        }
      
        [Persistent]
        class C
        {
        }
    }


    Sincerely yours,
    Jan Blessenohl
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Muhammad
    Muhammad avatar
    5 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    I am making web using Telerik Controls and now i need to have all track of database updation, insertion, deletion etc....
    how can i set the Audit Trail in Telerik....?
    please tell me its urgent....
  11. Ady
    Admin
    Ady avatar
    588 posts

    Posted 03 Sep 2012 Link to this post

    Hi Muhammad,

     Here is an audit example that demonstrates basic auditing with OpenAccess. Do have a look at it and get back in case you need further assistance.

    All the best,
    Ady
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
Back to Top
DevCraft banner