Logging DML operations in ASP NET MVC project

0 votes
There are several ways to log dml activities like as insert, update, and delete in an asp.net mvc project. I'm not sure if using aspects in code, triggers, or CDC in the database server is more useful, or if there are other, more logical options.
Jun 11, 2022 in C# by krishna
• 2,820 points
1,190 views

1 answer to this question.

0 votes

There are several options.

You might register an application interceptor if you're using Entity Framework as your ORM (see IDbCommandInterceptor). For further information, see Logging and intercepting database activities...

Setting the Database is another way that is very useful for logging ALL database operations.

Assign the following property to your DbContext implementation:

public class YourContext : DbContext
{   
    public YourContext(ILogger<YourContext> logger)
    {
        Database.Log = sql => logger.LogDebug(sql);
    }
}

I found this approach to be quite useful for debugging. I would have a file appender/target/sink (depending on what logging framework you're using e.g. NLog/Log4net/Serilog) specifically for my DbContext implementation, so that I could easily see the SQL of the database operations that are being executed without any noise from other application logs.

The other approach that I've used quite a bit is to use SQL Server Profiler, but this assumes a) you're using SQL Server, and b) you're able to connect to the database server.

answered Jun 14, 2022 by jyoti
• 1,240 points

Related Questions In C#

0 votes
1 answer

How to send email in ASP.NET C#

You can try this using Hotmail like ...READ MORE

answered Jun 14, 2022 in C# by krishna
• 2,820 points
943 views
0 votes
0 answers

How to create dependency injection for ASP.NET MVC 5

It's simple to create Dependency Injection using ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
514 views
0 votes
1 answer

A good and complete tutorial about reflection in .NET?

You can use this https://www.ecma-international.org/publications-and-standards/standards/ecma-335/ to understand ...READ MORE

answered Jun 6, 2022 in C# by pranav
• 2,590 points
529 views
0 votes
1 answer

Is a Library a project or a C# source code file in Visual Studio?

A library project is a collection of ...READ MORE

answered Jun 7, 2022 in C# by rajiv
• 1,620 points
474 views
0 votes
1 answer

Embedding Power Bi Report Promise is not defined powerbi.js

IE does not yet support Promise. You ...READ MORE

answered Oct 30, 2018 in Power BI by Shubham
• 13,490 points
2,059 views
0 votes
0 answers

The page cannot be displayed because an internal server error has occurred on server

When  I've deployed the website I installed  ...READ MORE

May 23, 2022 in Others by Kichu
• 19,040 points
1,106 views
0 votes
1 answer

Examples of dynamic polymorphism in c#

Check the example of polymorphism below. We ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
855 views
0 votes
1 answer

Reading a key from the Web.Config using ConfigurationManager

Instead of any other thing, use the ...READ MORE

answered Jun 11, 2022 in C# by pranav
• 2,590 points
5,003 views
0 votes
1 answer

Awesome ASP.NET and C# tutorials for beginners

Go here https://dotnet.microsoft.com/en-us/learn/aspnet They have some nice tutorials ...READ MORE

answered Jun 17, 2022 in C# by jyoti
• 1,240 points
886 views
0 votes
1 answer

Beginners book for .NET and C#

It depends on how proficient you are ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
437 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP