Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Audit Trail for Objects in C#?

I'm looking for ideas on how to implement audit trails for my objects in C#, for the current project,basically I need to:

1.Store the old values and new values of a given object. 2.Record creation of new objects. 3.Deletion of old object.

Is there any generic way of doing this,like using C# Generics,so that I don't have to write code for events of the base object like on creation,on deletion etc.(ORM objects).The thing is that if there was a way to inject audit trail if one is using a .Anybody have any experiences or any methods they follow.Any way to do this in a Aspect-oriented (AOP) mannner.

Please share your ideas etc.

like image 296
abmv Avatar asked Nov 30 '08 14:11

abmv


People also ask

What elements need to be included in an audit trail?

An audit trail should include sufficient information to establish what events occurred and who (or what) caused them. In general, an event record should specify when the event occurred, the user ID associated with the event, the program or command used to initiate the event, and the result.

What can be used to set up an audit trail for a table?

When you create the audit trail, use the -file flag to create an audit trail file in the current directory. Note: You must have first specified at least one table. In the following example, auditdb extracts a record of the changes to the employee table from the journal for the demodb database.

What is audit trail in CSV?

An audit trail is a progression of records of computer data about a working framework, an application, or client exercises. Computer frameworks may have a few audit trails each gave to a specific sort of action.


4 Answers

You could implement something similiar to INotifyPropertyChanged with a slight difference. I've extracted most of INotifyPropertyChanged and changed it to be generic and store new and old values. You can then have some sort of a management class that can listen to this and onSaving and onDeleting you can deal with the changes.

public interface INotifyProperyChanged<T>
{
   event PropertyChangedEventHandler<T> PropertyChanged;
}

    public delegate void PropertyChangedEventHandler<T>(object sender,   
PropertyChangedEventArgs<T> e);

public class PropertyChangedEventArgs<T> : EventArgs
{
    private readonly string propertyName;

    public PropertyChangedEventArgs(string propertyName)
    {
        this.propertyName = propertyName
    }

    public virtual string PropertyName { get { return propertyName; } }

    public T OldValue { get; set; }
    public T NewValue { get; set; }
}
like image 158
Ray Booysen Avatar answered Oct 03 '22 19:10

Ray Booysen


Question is pretty similar to How do you implement audit trail for your objects (Programming)?

We've implemented a similar solution, using AOP (aspectJ implementation). Using this particular points can be captured and specific operations can be performed.

This can be plugged in and plugged off when we like.

However, our implementation was in J2EE..

If you really want to do it in the app layer, i would suggest this.

Hope it helps..

like image 38
Satya Avatar answered Oct 03 '22 19:10

Satya


In addition to some of the things mentioned in the above thread the Command Pattern might be of help, if you wrap all the state changes on your object in a command then the command can be responsible for keeping the audit trail, while the object does not have to worry about auditing itself. Of course there is added overhead to creating and disposing commands.

You can wrap commands around any existing object structure, you just delegate your actions to the command layer as opposed to doing them on the objects directly.

like image 28
Harald Scheirich Avatar answered Oct 03 '22 18:10

Harald Scheirich


Have you considered using a simple notification pattern? You could have your service layer raise events such as NewObject, ChangedObject, DeletedObject that, will be listened to by a generic service layer which can then take the object and save the results.

If you want to save the state of the object you could leverage Xml Serialization.

Another approach is available if your using SQL Server 2008 you can implement the new Auditing features which will let you audit changes to database records you can even track (I think) when the data is read.

like image 25
JoshBerke Avatar answered Oct 03 '22 19:10

JoshBerke