在 Audit.Net 中,有没有办法使用多个输出提供程序? [英] In Audit.Net is there a way to use multiple output provider?

查看:21
本文介绍了在 Audit.Net 中,有没有办法使用多个输出提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置下面的配置,但我认为只有其中一个正在使用.

I tried setting up the configuration below however, I think only one of them is being used.

有没有办法将两者链接起来,或者有没有其他方法可以使用多个输出提供者?

Is there a way to chain the two or is there any other way to use multiple output provider?

            Audit.Core.Configuration.Setup()
                .UseElasticsearch(config => config
                    .ConnectionSettings(new Uri(elasticUri))
                    .Index("sample-index")
                    .Id(ev => Guid.NewGuid()));

            Audit.Core.Configuration.Setup()
                .UseUdp(config => config
                    .RemoteAddress("127.0.0.1")
                    .RemotePort(6060));

推荐答案

DataProvider 在应用程序中全局共享,因此您不能分配多个.

The DataProvider is globally shared across the application, so you can't assign more than one.

但您可以轻松实现自定义数据提供者,该数据提供者包装一些其他数据提供者并按顺序调用它们的 InsertEvent/ReplaceEvent 方法.例如:

But you can easily implement a custom data provider that wraps some other data providers and calls their InsertEvent/ReplaceEvent methods sequentially. For example:

public class MultiDataProvider : AuditDataProvider
{
    private AuditDataProvider[] _providers;
    public MultiDataProvider(AuditDataProvider[] providers)
    {
        _providers = providers;
    }
    public override object InsertEvent(AuditEvent auditEvent)
    {
        object eventId = null;
        foreach (var dp in _providers)
        {
            eventId = dp.InsertEvent(auditEvent);
        }
        return eventId;
    }
    public async override Task<object> InsertEventAsync(AuditEvent auditEvent)
    {
        object eventId = null;
        foreach (var dp in _providers)
        {
            eventId = await dp.InsertEventAsync(auditEvent);
        }
        return eventId;
    }
    public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
    {
        foreach (var dp in _providers)
        {
            dp.ReplaceEvent(eventId, auditEvent);
        }
    }
    public async override Task ReplaceEventAsync(object eventId, AuditEvent auditEvent)
    {
        foreach (var dp in _providers)
        {
            await dp.ReplaceEventAsync(eventId, auditEvent);
        }
    }

}

然后在您的启动代码中,您只需将 MultiDataProvider 配置为数据提供者,例如:

Then on your startup code, you just configure your MultiDataProvider as the data provider, for example:

Audit.Core.Configuration.DataProvider = new MultiDataProvider(
    new AuditDataProvider[]
    {
        new ElasticsearchDataProvider(_ => _
             .ConnectionSettings(new Uri(elasticUri))
                .Index("sample-index")
                .Id(ev => Guid.NewGuid())),
        new UdpDataProvider()
        {
            RemoteAddress = IPAddress.Parse("127.0.0.1"),
            RemotePort = 6060
        }
    }
);

这篇关于在 Audit.Net 中,有没有办法使用多个输出提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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