Audit.Net无法追踪新值。
创始人
2024-09-22 06:30:50
0

在使用Audit.Net进行审计追踪时,如果无法追踪新值,有几种可能的解决方法。以下是其中一种解决方法的代码示例:

using Audit.Core;
using Audit.EntityFramework;
using System;

namespace AuditDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 设置全局配置
            Audit.Core.Configuration.Setup()
                // 使用EntityFramework进行审计追踪
                .UseEntityFramework(config => config
                    // 审计追踪的上下文
                    .AuditTypeMapper(type => typeof(AuditLog))
                    // 忽略审计追踪的表
                    .IgnoreMatchedProperties(true)
                    // 检测新值
                    .IncludeEntityObjects(false)
                    // 忽略空值
                    .ExcludeEmptyValues(true)
                    // 审计追踪的数据标记
                    .AuditEntityAction((ev, entry, entity) =>
                    {
                        var auditLog = entity as AuditLog;
                        if (auditLog != null)
                        {
                            auditLog.AuditDate = DateTime.Now;
                            auditLog.AuditAction = entry.Action;
                            auditLog.TableName = entry.Table.Name;
                        }
                    })
                );

            // 添加Entity Framework Core上下文
            Audit.EntityFramework.Configuration.Setup()
                .ForContext(config => config
                    // 忽略审计追踪的表
                    .IgnoreMatchedProperties(true)
                    // 检测新值
                    .IncludeEntityObjects(false)
                    // 忽略空值
                    .ExcludeEmptyValues(true)
                    // 审计追踪的数据标记
                    .AuditEntityAction((ev, entry, entity) =>
                    {
                        var auditLog = entity as AuditLog;
                        if (auditLog != null)
                        {
                            auditLog.AuditDate = DateTime.Now;
                            auditLog.AuditAction = entry.Action;
                            auditLog.TableName = entry.Table.Name;
                        }
                    })
                );

            // 创建Entity Framework Core上下文
            using (var context = new AuditLogDbContext())
            {
                // 进行数据库操作
                var customer = new Customer { Id = 1, Name = "John Doe" };
                context.Customers.Add(customer);
                context.SaveChanges();
            }

            Console.WriteLine("Audit tracking completed.");
            Console.ReadLine();
        }
    }

    // Entity Framework Core上下文
    public class AuditLogDbContext : AuditDbContext
    {
        public DbSet Customers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("your_connection_string");
        }
    }

    // 审计追踪的实体
    public class AuditLog : IAuditEntity
    {
        public int Id { get; set; }
        public DateTime AuditDate { get; set; }
        public string AuditAction { get; set; }
        public string TableName { get; set; }
    }

    // 示例实体
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

在上述代码示例中,我们使用了Audit.Core和Audit.EntityFramework库来进行审计追踪。在全局配置中,我们设置了一些选项来控制审计追踪的行为,包括忽略审计追踪的表、检测新值和忽略空值。在审计追踪的数据标记中,我们将相关的审计信息填充到审计追踪的实体中。

我们还创建了一个Entity Framework Core上下文(AuditLogDbContext),并使用Audit.EntityFramework库的配置方法来设置审计追踪的行为。

最后,在进行数据库操作时,我们可以使用上述创建的Entity Framework Core上下文来进行操作。审计追踪的数据将被自动记录到审计追踪的实体(AuditLog)中。

请注意,上述示例中的连接字符串需要根据实际情况进行修改。

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...