在Audit.Net中,是否可以使用多个输出提供程序? [英] In Audit.Net is there a way to use multiple output provider?

查看:142
本文介绍了在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天全站免登陆