Reputation: 73
Is it possible to establish some mechanism so that in case the insertion of the audit fails in the default dataprovider, oracle for example, another data provider is used, for example in a file? Thanks for the help
Upvotes: 2
Views: 469
Reputation: 13114
There is not a data provider with automatic fallback capabilities, but you can implement a custom data provider.
Say you want the Sql data provider by default, and another data provider as fallback. Inherit from the SQL data provider (SqlDataProvider
) and fallback to another DataProvider
when a SqlException
is thrown:
public class FallbackSqlDataProvider : SqlDataProvider
{
public AuditDataProvider FallbackProvider { get; set; }
public override object InsertEvent(AuditEvent auditEvent)
{
try
{
return base.InsertEvent(auditEvent);
}
catch (SqlException)
{
return FallbackProvider?.InsertEvent(auditEvent);
}
}
public override async Task<object> InsertEventAsync(AuditEvent auditEvent)
{
try
{
return await base.InsertEventAsync(auditEvent);
}
catch (SqlException)
{
return await FallbackProvider?.InsertEventAsync(auditEvent);
}
}
public override void ReplaceEvent(object eventId, AuditEvent auditEvent)
{
try
{
base.ReplaceEvent(eventId, auditEvent);
}
catch (SqlException)
{
FallbackProvider?.ReplaceEvent(eventId, auditEvent);
}
}
public override async Task ReplaceEventAsync(object eventId, AuditEvent auditEvent)
{
try
{
await base.ReplaceEventAsync(eventId, auditEvent);
}
catch (SqlException)
{
await FallbackProvider?.ReplaceEventAsync(eventId, auditEvent);
}
}
}
Then you can set the fallback data provider on FallbackProvider
property, for example like this:
var dp = new FallbackSqlDataProvider()
{
ConnectionString = "cnnstring",
TableName = "table",
IdColumnName = "id",
JsonColumnName = "data",
FallbackProvider = new Log4netDataProvider()
{
Logger = LogManager.GetLogger(typeof(Log4netDataProvider)),
LogMessageBuilder = (ev, id) => ev.ToJson()
}
};
Audit.Core.Configuration.Setup()
.UseCustomProvider(dp);
Also check this related issue.
Upvotes: 2