Mahesh Bhosale
Mahesh Bhosale

Reputation: 57

Entity Framework 6.1.3 Log SQL queries only if exception occurred On DBContext.SaveChanges()

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

Answers (2)

Mahesh Bhosale
Mahesh Bhosale

Reputation: 57

Logged the log into the memory stream and if the exception has occurred the log is added to the file.

Upvotes: 0

Suraj Kumar
Suraj Kumar

Reputation: 5643

You can use any of the below logging libraries as shown below described here.

  1. Log4net (Logging in text file + logging in SQL database)

  2. Nlog (Logging in text file + logging in SQL database)

  3. Serilog (Logging in text file + logging in SQL database)

  4. 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

Related Questions