Reputation: 57
I am using Entity Framework 6.1.3. I want to log SQL queries with parameters after only if an exception occurred in the catch block. Is there any way to log queries in this way?
try
{
base.SaveChangesAsync();
}
catch(Exception ex)
{
// I want to log SQL queries here.
}
Upvotes: 1
Views: 541
Reputation: 57
Logged the log into the memory stream and if the exception has occurred the log is added to the file.
Upvotes: 0
Reputation: 5643
You can use any of the below logging libraries as shown below described here.
Log4net (Logging in text file + logging in SQL database)
Nlog (Logging in text file + logging in SQL database)
Serilog (Logging in text file + logging in SQL database)
Elmah (logging in SQL database)
Alternatively you can create a procedure to track and insert the log/issue in the database manually as below.
try
{
//Your code here.
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
Label1.Text = "Some Technical Error occurred,Please visit after some time";
}
You can get the details from here.
Upvotes: 1