Audit.Net as a dedicated API? #501
-
Hi there @thepirat000 - I have 4 .net Core applications that currently are 100% microservice driven - they do not have direct access to our back end database. I've created an "Audit API" that would take an audit event as a parameter (seen below), Unfortunately so far I have not been able to derive how I can make this work without basically copy/pasting all of the classes from the Audit.Net source code to parse the audit event natively like the library does. I am able to call the Audit API from my application using a custom provider and the insert event, it's just the receiving end I can't figure out. Controller (Audit API):
Any suggestions here? I have Audit.Net working perfectly if I use the SQL provider in the actual application, but I really don't want to have to do that. I want the Audit.Net SQL provider in the Audit API and just to be able to call the endpoint with the Audit Event param. Thanks for any advice. |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 14 replies
-
What's the problem on the receiving part? |
Beta Was this translation helpful? Give feedback.
-
@thepirat000 thanks for the reply. So the API endpoint receives the audit event object, but I do not see anything in your documentation about a function that simply takes an audit event as a parameter, so I do not know what to do with the object to then get it into the SQL data provider. |
Beta Was this translation helpful? Give feedback.
-
I guess you can instantiate a SqlDataProvider from your controller. public class MyController : ControllerBase
{
private AuditDataProvider _dataProvider;
public MyController(AuditDataProvider dataProvider)
{
_dataProvider = dataProvider;
}
[HttpPost]
[Route("AddAuditEvent")]
public ActionResult<AuditEvent> AddAuditEvent([FromBody] AuditEvent entity)
{
_dataProvider.InsertEvent(entity);
// ...
}
} And configure the dependency on your startup, for example: services.AddSingleton<AuditDataProvider>(new Audit.SqlServer.Providers.SqlDataProvider(config => config
.ConnectionString("data source=localhost;initial catalog=Audit;integrated security=true;")
.TableName("Event")
.IdColumnName("EventId")
.JsonColumnName("JsonData"))); |
Beta Was this translation helpful? Give feedback.
-
@thepirat000 so just a little more advice here. On startup of the application (not the API), I'm registering the custom data provider that will call the API. Audit.Core.Configuration.DataProvider = new AuditEventProvider(); Here is the Custom Provider:
|
Beta Was this translation helpful? Give feedback.
-
Just to be super clear, here's what I have for the custom provider now
So obviously with that, the "setup" in startup doesn't work due to no default constructor. Thanks for any help you can give when you have a chance. |
Beta Was this translation helpful? Give feedback.
-
I think one option is to resolve the dependencies in the Data Provider Factory. For example, on your Startup Configure method: public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddTransient<ITokenService, TokenService>();
services.AddTransient<AuditDataProvider, AuditEventProvider>();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Factory method to create the DataProvider to use on each audit scope
Audit.Core.Configuration.Setup().UseFactory(() => app.ApplicationServices.GetService<AuditDataProvider>());
// ...
} |
Beta Was this translation helpful? Give feedback.
-
Here is the AuditAPI app - I'm not sure it'll work since the database will be unavailable, but for context, I'll add a screenshot of the debugging information separately. |
Beta Was this translation helpful? Give feedback.
-
Here's a screenshot of the debugger at the time the error occurs. You can see the incoming entity has all the correct fields populated. |
Beta Was this translation helpful? Give feedback.
-
I have to wonder if the AuditEvent object that is sent into the endpoint is not formatted quite right Here is the call to the endpoint:
Is it something to do with doing a JsonContent.Create() on the already Json formatted AuditEvent? Some sort of "Double Json-ing"? So the key/values don't line up right for your library to parse them? |
Beta Was this translation helpful? Give feedback.
-
Looks like your SQL table is expecting an EventType column, but you are not configuring the SqlDataProvider to insert that column. services.AddSingleton<AuditDataProvider>(new Audit.SqlServer.Providers.SqlDataProvider(config => config
.ConnectionString(Environment.GetEnvironmentVariable("DBCREDENTIAL"))
.TableName("Event")
.IdColumnName("EventId")
.CustomColumn("EventType", ev => ev.EventType) // <--- Add the missing custom column
.JsonColumnName("JsonData"))); |
Beta Was this translation helpful? Give feedback.
Looks like your SQL table is expecting an EventType column, but you are not configuring the SqlDataProvider to insert that column.