This is a migrated thread and some comments may be shown as answers.

Audit Trail

9 Answers 199 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Lafama
Top achievements
Rank 1
Lafama asked on 31 Aug 2010, 09:33 AM
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

9 Answers, 1 is accepted

Sort by
0
Jan Blessenohl
Telerik team
answered on 01 Sep 2010, 05:48 PM
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
0
Lafama
Top achievements
Rank 1
answered on 03 Sep 2010, 05:18 AM
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
0
Jan Blessenohl
Telerik team
answered on 06 Sep 2010, 01:59 PM
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
0
Lafama
Top achievements
Rank 1
answered on 06 Sep 2010, 06:43 PM
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 ??
0
Jan Blessenohl
Telerik team
answered on 07 Sep 2010, 08:19 AM
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
0
La
Top achievements
Rank 1
answered on 07 Sep 2010, 10:38 AM
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
0
Jan Blessenohl
Telerik team
answered on 07 Sep 2010, 11:24 AM
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
0
Muhammad
Top achievements
Rank 1
answered on 29 Aug 2012, 09:03 AM
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....
0
Ady
Telerik team
answered on 03 Sep 2012, 01:01 PM
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!
Tags
Development (API, general questions)
Asked by
Lafama
Top achievements
Rank 1
Answers by
Jan Blessenohl
Telerik team
Lafama
Top achievements
Rank 1
La
Top achievements
Rank 1
Muhammad
Top achievements
Rank 1
Ady
Telerik team
Share this question
or