Introduction
We can apply our own formatting for logging SQL by creating a class that inherits from this class and override some of the methods. The most common methods to override are:
- LogCommand: It called to log a command that is about to be executed. This method can be overridden to change how the command is logged to the "WriteAction" method.
- LogResult: It is called to log the result of the executing command. Override this method to change how the results are logged to the WriteAction method.
- LogParameter: It is called by the LogCommand method to log each parameter. It can be overridden to change the way that parameters are logged to the WriteAction method.
Example
Suppose I want to log the context name in one line and a single-line SQL query before each command is sent to the database. For that, I must write my own formatter class that is derived from the DatabaseLogFormatter class and need to override the two methods LogCommand and LogResult. Override LogCommand to format and write the SQL log and override LogResult with nobody.
The code is something like:
- using System;
- using System.Data.Common;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure.Interception;
- namespace SQLLogging.Model
- {
- public class MyFormatter : DatabaseLogFormatter
- {
- public MyFormatter(DbContext context, Action<string> writeAction)
- : base(context, writeAction)
- {
- }
- public override void LogCommand<TResult>(
- DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
- {
- Write(string.Format(
- "Context :'{0}'" + Environment.NewLine + "Executing command :'{1}'{2}",
- Context.GetType().Name,
- command.CommandText.Replace(Environment.NewLine, ""),
- Environment.NewLine));
- }
- public override void LogResult<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
- {
- }
- }
- }
- using System.Data.Entity;
- namespace SQLLogging.Model
- {
- public class MyDbConfiguration : DbConfiguration
- {
- public MyDbConfiguration()
- {
- SetDatabaseLogFormatter(
- (context, writeAction) => new MyFormatter(context, writeAction));
- }
- }
- }
- using System.IO;
- namespace SQLLogging.Model
- {
- public class FileWriter
- {
- public static void WriteSQL(string data)
- {
- string path = @"c:\SQLtrace.txt";
- File.AppendAllText(path, data);
- }
- }
- }
- namespace SQLLogging.Model
- {
- using System.Data.Entity;
- public partial class Model : DbContext
- {
- public Model()
- : base("name=EntityModel")
- {
- }
- public virtual DbSet<Employee> Employees { get; set; }
- public virtual DbSet<EmployeeDetail> EmployeeDetails { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- Database.Log = FileWriter.WriteSQL;
- }
- }
- }
- static void Main(string[] args)
- {
- using (SQLLogging.Model.Model context = new SQLLogging.Model.Model())
- {
- var data = context.Employees.Where(p => p.Code.StartsWith("p")).ToList();
- var data1 = (from e in context.Employees
- join d in context.EmployeeDetails on e.Id equals d.Id
- select new
- {
- Name = e.Name,
- Phone = d.PhoneNo
- }).ToList();
- }
- Console.ReadLine();
- }


Tom WilsonPosted Jul 13, 2018, 1:09 PM
How do I capture the closed and open connection statements? Also, it seems the paremeter values are not printing anymore with this code
Gowtham RajamanickamPosted May 28, 2015, 10:43 AM
this is great..