Logging Database Operations To File In Entity Framework 6

Introduction
 
Entity Framework Logging features were introduced in Entity Framework 6.This feature gives us the opportunity to log every executing query or command sent to the database. This is the best or easiest way to track what's happening with the database. Before this, we had used the SQL Profiler to see what EF request is running and which query has executed and the result. So, now we have this feature integrated with EF6. The important use of this feature is to track of unexpected error/results and to find the performance issues by looking at the logged report, it can track by time, query result and more. We can also see how the Entity Framework translates the Linq query to SQL query.It logs all the activity performed by EF using context.database.Log. Here, you can attach any method of any class, which accepts one string parameter and returns void.
 
Logging 
 
The DbContext.Database.Log help us to record or track the pieces of information about database operation. Here we will record all information to file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.IO;  
  7. namespace EFLogging {  
  8.     class Program {  
  9.         private static CSharpCornerEntities context = new CSharpCornerEntities();  
  10.         static void Main(string[] args) {  
  11.             context.Database.Log = logInfo => FileLogger.Log(logInfo);  
  12.             InsertEmployeeRecord();  
  13.             UpdateEmployeeRecord(10024);  
  14.             context.Database.Log = Console.Write;  
  15.         }  
  16.         private static void UpdateEmployeeRecord(long employeeID) {  
  17.             //Linq  
  18.             var empData = (from employee in context.Employees where employee.ID == employeeID select employee).FirstOrDefault();  
  19.             //Lambda  
  20.             //var empData = context.Employees.Where(a => a.ID.Equals(employeeID)).ToList();  
  21.             empData.Location = "UK";  
  22.             context.Entry(empData).State = System.Data.Entity.EntityState.Modified;  
  23.             context.SaveChanges();  
  24.         }  
  25.         private static void InsertEmployeeRecord() {  
  26.             context.Employees.Add(new Employee {  
  27.                 Designation = "Software Engineer ", Location = "Chennai", Name = "DummyRecord"  
  28.             });  
  29.             context.SaveChanges();  
  30.         }  
  31.     }  
  32.     public class FileLogger {  
  33.         public static void Log(string logInfo) {  
  34.             File.AppendAllText(@ "C:\Users\SMKG\Desktop\Logger.txt", logInfo);  
  35.         }  
  36.     }  
  37. }  
Look at the above code, I mentioned The DbContext.Database.Log property can be set to a delegate for any method that takes a string. Here,I created FileLogger class and Log method to record the respective database information using DbContext.Database.Log property. All SQL generated by the current context will be logged to that FileLogger class using Log method. 
 
Once I executed the code above, you will see extra log information in stored file, now let us open the respective file
 
 
 
I hope it's helpful.