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)中。

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

相关内容

热门资讯

wepoke模拟器!wepok... wepoke模拟器!wepoke有科技吗,wepoke软件收费是真的,扑克教程(有挂教程);致您一封...
微扑克ai机器人!wepoke... 微扑克ai机器人!wepoke辅助透视教程,德州aa poker有外挂,软件教程(有挂辅助挂)1、构...
德州微扑克辅助!wpk微扑克真... 德州微扑克辅助!wpk微扑克真的有挂吗,德州软件工具,德州论坛(有挂辅助挂),您好,德州微扑克辅助这...
wepok软件透明挂!德扑统计... wepok软件透明挂!德扑统计软件,德州辅助神器wpk,2025新版总结(有挂透明)1、wepok软...
智星德州菠萝有挂吗!微扑克有规... 智星德州菠萝有挂吗!微扑克有规律吗,德州ai智能系统,透明挂教程(有挂技巧)您好,智星德州菠萝有挂吗...
wepower辅助器!德州之星... wepower辅助器!德州之星app辅助器怎么用,wpk透视辅助哪里下载,规律教程(有挂黑科技)是一...
wepokeai代打!微扑克系... wepokeai代打!微扑克系统的发牌速度有多快,红龙扑克是真是假,可靠技巧(有挂透明)1、许多玩家...
aapoker猫腻!德州ai机... aapoker猫腻!德州ai机器人免费测试,微扑克有计算器,技巧教程(有挂教学),您好,德州ai机器...
wepoke辅助有挂!aapo... wepoke辅助有挂!aapoker辅助是真的吗,wpk透视辅助封号,第三方教程(有挂教学);小薇(...
微扑克辅助机器人!aapoke... 微扑克辅助机器人!aapoker是正规的吗,(wEpoKe)原生真的是有挂(详细辅助玩家教你)1、完...