Replies: 14 comments 14 replies
-
I guess you are referring to Audit.EntityFramework.Core, if that's the case, you can use the low-level interceptor |
Beta Was this translation helpful? Give feedback.
-
Yes, my plan is too hookup this auditor with EF and SQL database, I just need to log everything including all select statements, inserts, deletes and updates. is there more an example on how to do it with this low level command interpreter? It does not really show enough for me to understand right now. Also can these all be saved in the same table that is generated? Looking to have more 1 audit table for everything. |
Beta Was this translation helpful? Give feedback.
-
Take into account that you cannot use the EntityFramework data provider to store the low-level interceptor events, since the EF data provider is exclusively for the high-level SaveChanges interception events. So if you want to store low-level events to a SQL table, you can use the Audit.NET.SqlServer data provider. For example, you can configure your DbContext to include both high-level and low-level as follows: // Inheriting from AuditDbContext will intercept SaveChanges for high-level events.
// Inherit from DbContext if you only want low-level events.
public class MyAuditedContext : AuditDbContext
{
public DbSet<User> Users { get; set; }
public DbSet<AuditEntity> Audits { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
// Add the low-level interceptor
optionsBuilder.AddInterceptors(new AuditCommandInterceptor());
optionsBuilder.UseSqlServer(Settings.SqlConnectionString);
}
}
}
// Sample audit entity class to store in the database
[AuditIgnore]
public class AuditEntity
{
[Key] public int Id { get; set; }
public DateTime DateTime { get; set; }
public string Event { get; set; }
} So you can configure the SQL data provider like this: Audit.Core.Configuration.Setup()
.UseSqlServer(cfg => cfg
.ConnectionString(Settings.SqlConnectionString)
.TableName("Audits")
.IdColumnName("Id")
.CustomColumn("DateTime", ev => DateTime.Now)
.JsonColumnName("Event")); |
Beta Was this translation helpful? Give feedback.
-
Okay I will try that but where is Settings coming from? Also where do you put the last chunk of code .Setup()? My project is split into many different projects where the ApplicationDBContext is on 1 project and the Entities and the setup of the context is in a seperate project looks something like this
Also your making a AuditTable that will be used to store all the aduits right? Is this including the high level ones? Would the json data just go into the Events? |
Beta Was this translation helpful? Give feedback.
-
Yes, the table will include all the events. Nothing stops you to try and see by yourself. Audit.Core.Configuration.Setup() should be called on your app startup. |
Beta Was this translation helpful? Give feedback.
-
I
Okay I think I got it to work in my project. It seems to be logging though I am not sure if it doing the delete (or maybe how my code is setup maybe it does not work). Some tables that are for drafts I clear the entire record and then reinsert the new record, I only see the insert not the clearing of the old record but maybe I missed it. Also I need to attach to all high and low level events a userId, I cannot set that in the startup at a customColumn, am I able to call something per request and add this information in? |
Beta Was this translation helpful? Give feedback.
-
I think the delete might be fine still trying to see how I could dynamically add the userId to each CRUD request |
Beta Was this translation helpful? Give feedback.
-
it depends, how do you get the current userId? |
Beta Was this translation helpful? Give feedback.
-
Right now it would be through a claim via the http context on each request. |
Beta Was this translation helpful? Give feedback.
-
If you use asp net core, then you can access the current httpcontext by injecting an public void Configure(IApplicationBuilder app, IHttpContextAccessor contextAccessor)
{
Audit.Core.Configuration.Setup()
.UseSqlServer(cfg => cfg
.ConnectionString("...")
.TableName("Audits")
.IdColumnName("Id")
.CustomColumn("User", _ => contextAccessor.HttpContext.User.Identity.Name)
.JsonColumnName("Event"));
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Also with the low level logging can we also log the actual results that come back and not just the query that was done? |
Beta Was this translation helpful? Give feedback.
-
I think the only option is to buffer the results in memory, but that will require some work and of course, it will impact performance so this should be an optional mechanism. If you want, you can create a new issue here and ask for support for including query results. |
Beta Was this translation helpful? Give feedback.
-
Hey with AuditIgnore for the EF provider does this work on low level commands as well (ie if I put AuditIgnore on a entity and do a select statement on it will it still log in the db?)? |
Beta Was this translation helpful? Give feedback.
-
I am not 100% sure yet as I am still trying to do some testing, but I am getting some weird results where I am getting an error saying a Data Reader is open in the same context and it is throwing an error. When I turn off the new flag that was released in the latest version to log the db results then I don't get this error anymore. It is happening only in specific situation where I seem to be inserting data into multiple tables. |
Beta Was this translation helpful? Give feedback.
-
Hi
I am wondering can this also log select statements for audit tracking?
Beta Was this translation helpful? Give feedback.
All reactions