1

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.
}
2
  • 2
    why not just let .Log write to a memory stream and just dump this stream to a log file if you enter the exception block? Commented Jan 14, 2020 at 13:01
  • Yes implemented the same. Thank you! Commented Jan 17, 2020 at 8:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@MaheshBhosale You are welcome and I had already said there are various way to log the exception both from the application and in database side also.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.