时间:2021-07-01 10:21:17 帮助过:2人阅读
using System;
using System.Data.Common;
using System.Diagnostics;
using Clutch.Diagnostics.EntityFramework;
/// <summary>
/// Implementation of IDbTracingListener Class is used for tracing all SQL Queries to the entity framework database
/// </summary>
public class DbTracingListener : IDbTracingListener
{
public void CommandExecuted(DbConnection connection, DbCommand command, object result, TimeSpan duration)
{
Debug.WriteLine(command.CommandText);
Debug.WriteLine(string.Format("Executed in: {0}", duration));
}
public void CommandExecuting(DbConnection connection, DbCommand command)
{
}
public void CommandFailed(DbConnection connection, DbCommand command, Exception exception, TimeSpan duration)
{
}
public void CommandFinished(DbConnection connection, DbCommand command, object result, TimeSpan duration)
{
}
}
在方法内部通过 context.Command.CommandText 可以获得你的ef的sql命令的内容。
然后在程序的入口启用SQL日志输出实现:
// Enable Tracing queries
DbTracing.Enable();
// Adding the listener (implementation of IDbTracingListener)
DbTracing.AddListener(new DbTracingListener());
关于EF输出sql的执行日志
标签: