Reputation: 51
I'm trying to audit db actions using SqlDataProvider .
Global.asax.cs
Audit.Core.Configuration.DataProvider = new SqlDataProvider()
{
ConnectionString = "...",
Schema = "dbo",
TableName = "Event",
IdColumnName = "EventId",
JsonColumnName = "JsonData",
LastUpdatedDateColumnName = "LastUpdatedDate",
CustomColumns = new List<CustomColumn>()
{
new CustomColumn("EventType", ev => ev.EventType),
new CustomColumn("ModuleName", ev => "AuditTrail")
}
};
if I add custom column like following
new CustomColumn("StartDate", ev=>ev.StartDate),
new CustomColumn("EndDate", ev=>ev.EndDate),
new CustomColumn("Duration", ev=>ev.Duration),
new CustomColumn("Target", ev=>ev.Target),
new CustomColumn("Comments", ev=>ev.Comments),
new CustomColumn("Environment", ev=> ev.Environment),
it throws an exception "No mapping exists from object type Audit.Core.AuditEventEnvironment to a known managed provider native type"
How can I track the event here. Basically I want to insert jasonData field value into several fields.
Context Changes
public override int SaveChanges()
{
return _helper.SaveChanges(_auditContext, () => base.SaveChanges());
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return await _helper.SaveChangesAsync(_auditContext, () => base.SaveChangesAsync(cancellationToken));
}
Upvotes: 1
Views: 247
Reputation: 51
It was an issue in data types. The correct code should be
Audit.Core.Configuration.DataProvider = new SqlDataProvider()
{
ConnectionString = "...",
Schema = "dbo",
TableName = "Event",
IdColumnName = "EventId",
JsonColumnName = "JsonData",
LastUpdatedDateColumnName = "LastUpdatedDate",
CustomColumns = new List<CustomColumn>()
{
new CustomColumn("EventType", ev => ev.EventType),
new CustomColumn("ModuleName", ev => "AuditTrail"),
new CustomColumn("StartDate", ev=>Convert.ToDateTime(ev.StartDate)),
new CustomColumn("EndDate", ev=>Convert.ToDateTime(ev.EndDate)),
new CustomColumn("Duration", ev=>Convert.ToInt32(ev.Duration)),
}
};
We can add the following custom fields for some of the other details of the json data field.
new CustomColumn("UserName", ev=> ev.Environment.UserName.ToString()),
new CustomColumn("MachineName", ev=> ev.Environment.MachineName.ToString()),
new CustomColumn("DomainName", ev=> ev.Environment.DomainName.ToString()),
new CustomColumn("CallingMethod", ev=> ev.Environment.CallingMethodName.ToString()),
new CustomColumn("DatabaseName", ev=> ev.GetEntityFrameworkEvent().Database.ToString()),
Since the entries of the json content is little bit different in insert and update, I'm still trying to separate data by the action.
Upvotes: 1