在 C# 中实现对象的审计跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/328898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:23:55  来源:igfitidea点击:

Implementing Audit Trail for Objects in C#?

c#objectaudittrail

提问by abmv

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

我正在寻找有关如何在 C# 中为我的对象实现审计跟踪的想法,对于当前项目,基本上我需要:

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

1.存储给定对象的旧值和新值。2.记录新对象的创建。3.删除旧对象。

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.

有没有什么通用的方法可以做到这一点,比如使用 C# 泛型,这样我就不必为基础对象的事件编写代码,比如创建、删除等(ORM 对象)。问题是,如果有如果有人使用 .Anybody 有任何经验或他们遵循的任何方法,则注入审计跟踪的一种方法。以面向方面 (AOP) 方式执行此操作的任何方法。

Please share your ideas etc.

请分享您的想法等。

采纳答案by Satya

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.

我们已经实现了一个类似的解决方案,使用 AOP(aspectJ 实现)。可以捕获使用此特定点并执行特定操作。

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

这可以在我们喜欢时插入和关闭。

However, our implementation was in J2EE..

然而,我们的实现是在 J2EE..

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

如果您真的想在应用程序层中执行此操作,我会建议这样做。

Hope it helps..

希望能帮助到你..

回答by Harald Scheirich

In addition to some of the things mentioned in the above thread the Command Patternmight 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.

您可以围绕任何现有的对象结构包装命令,您只需将您的操作委托给命令层,而不是直接在对象上执行这些操作。

回答by JoshBerke

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.

您是否考虑过使用简单的通知模式?您可以让您的服务层引发诸如 NewObject、ChangedObject、DeletedObject 之类的事件,这些事件将由通用服务层侦听,然后可以获取对象并保存结果。

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

如果您想保存对象的状态,您可以利用 Xml 序列化。

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.

如果您使用 SQL Server 2008,则可以使用另一种方法,您可以实现新的审核功能,这将允许您审核对数据库记录的更改,您甚至可以在读取数据时跟踪(我认为)。

回答by Ray Booysen

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.

您可以实现与 INotifyPropertyChanged 类似的东西,但略有不同。我已经提取了大部分 INotifyPropertyChanged 并将其更改为泛型并存储新旧值。然后,您可以拥有某种可以侦听此内容的管理类,您可以使用 onSaving 和 onDeleting 来处理更改。

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; }
}

回答by thepirat000

Pretty old question, but for those wanting to audit C# objects, I would recommend the Audit.NETlibrary.

很老的问题,但对于那些想要审计 C# 对象的人,我会推荐Audit.NET库。

It has extensions to log to different storage systems (SQL, Azure, Mongo) and for auditing different systems (WCF, EF, MVC).

它具有用于登录不同存储系统(SQL、Azure、Mongo)和审计不同系统(WCF、EF、MVC)的扩展。

Note: I'm the owner

注:我是楼主