© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
Kamil Pisz

Looking solution for get query from _dbContext.SaveChangesAsync() for save logs and easier debugging

Hello, i have a .net core 6.0 app that save entites in MSSQL database with EfCORE 6.0
so my function is something like

var newInvoice = new Invoice();
newInvoice.Name = "2022/12/12"
newInvoice.Value = 5m;
await _dbContext.Invoices.AddAsync(newInvoice);
await _SaveChangesAsync();

var newInvoiceProduct = new InvoiceProduct();
newInvoiceProduct.Name = "toy1"
newInvoiceProduct.Value = 5m;
await _dbContext.InvoicesProduct.AddAsync(newInvoiceProduct);
await _SaveChangesAsync();
var newInvoice = new Invoice();
newInvoice.Name = "2022/12/12"
newInvoice.Value = 5m;
await _dbContext.Invoices.AddAsync(newInvoice);
await _SaveChangesAsync();

var newInvoiceProduct = new InvoiceProduct();
newInvoiceProduct.Name = "toy1"
newInvoiceProduct.Value = 5m;
await _dbContext.InvoicesProduct.AddAsync(newInvoiceProduct);
await _SaveChangesAsync();


What i want to do:
every Add/Modify query that going to DB should create new Log in my Logging table
var newInvoice = new Invoice();
newInvoice.Name = "2022/12/12"
newInvoice.Value = 5m;
await _dbContext.Invoices.AddAsync(newInvoice);
await _SaveChangesAsync();

//Save logs what SQL send to database or save expcetion/error message
var newInvoiceLog = new InvoiceLog();
newInvoiceLog.InvoiceId = newInvoice.Id; // id of new created entity
newInvoiceLog.RawQuery = //<----- that i missing, single string that going to DB like Logging to debug console 
var newInvoice = new Invoice();
newInvoice.Name = "2022/12/12"
newInvoice.Value = 5m;
await _dbContext.Invoices.AddAsync(newInvoice);
await _SaveChangesAsync();

//Save logs what SQL send to database or save expcetion/error message
var newInvoiceLog = new InvoiceLog();
newInvoiceLog.InvoiceId = newInvoice.Id; // id of new created entity
newInvoiceLog.RawQuery = //<----- that i missing, single string that going to DB like Logging to debug console 
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

DbContext.SaveChangesAsync has no effect on the collection of changed objects
C#CC# / help
4y ago
❔ EF Core Query filters and DbContext lifetime
C#CC# / help
3y ago
Looking for a logging solution
C#CC# / help
15mo ago
Looking for a logging solution
C#CC# / help
17mo ago