实体框架核心的审计跟踪 [英] Audit trail with Entity Framework Core

查看:112
本文介绍了实体框架核心的审计跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在SQL Server数据库上使用Entity Framework核心的ASP.NET core 2.0。



我必须跟踪并审核用户在服务器上所做的所有工作。数据。我的目标是要有一个自动机制来记录所有发生的事情。



例如,如果我有动物表,我想要一个并行表 Audit_animals查找有关数据的所有信息,操作类型(添加,删除,编辑)以及执行此操作的用户。



我这一次已经在Django + MySQL中进行过,但现在的环境有所不同。我发现了

解决方案

阅读文档


事件输出



要配置输出持久性机制,请参见配置数据提供程序部分。


然后,在配置:


如果您未指定数据提供程序,则将使用默认的 FileDataProvider 事件写为.json文件 进入当前工作目录。 (强调我的意思)


长而简短,请按照文档配置您要使用的数据提供程序。


I have an ASP.NET core 2.0 using Entity Framework core on a SQL Server db.

I have to trace and audit all the stuff made by the users on the data. My goal is to have an automatic mechanism writing all what is happening.

For example, if I have the table Animals, I want a parallele table "Audit_animals" where you can find all the info about the data, the operation type (add, delete, edit) and the user who made this.

I already made this time ago in Django + MySQL, but now the environment is different. I found this and it seems interesting, but I'd like to know if there are better ways and which is the best approach to do this in EF Core.

UPDATE

I'm trying this and something happens, but I have some problems.

I added this:

  1. services.AddMvc().AddJsonOptions(options => {
    
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            }); 
    

  2. public Mydb_Context(DbContextOptions<isMultiPayOnLine_Context> options) : base(options)
    {
        Audit.EntityFramework.Configuration.Setup()
            .ForContext<Mydb_Context>(config => config
                .IncludeEntityObjects()
                .AuditEventType("Mydb_Context:Mydb"))
            .UseOptOut()
    }
    

  3. public MyRepository(Mydb_Context context)
    {
        _context = context;
        _context.AddAuditCustomField("UserName", "pippo");
    
    }
    

I also created a table to insert the audits (only one to test this tool), but the only thing I got is what you see in the image. A list of json files with the data I created.... why??

解决方案

Read the documentation:

Event Output

To configure the output persistence mechanism please see Configuration and Data Providers sections.

Then, in the documentation on Configuration:

If you don't specify a Data Provider, a default FileDataProvider will be used to write the events as .json files into the current working directory. (emphasis mine)

Long and short, follow the documentation to configure the data provider you'd like to use.

这篇关于实体框架核心的审计跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆