2014-02-06 2 views
7

우리는 Dapper 및 EF를 사용하고 있으며 Dapper는 문제가 발생했을 때 SQL 서버의 쿼리를 디버깅하는 데 매우 유용합니다. 대신 원시 SQL을 제출, 우리는이 우리의 DBA와 개발자가 매우 빠르게 reacy 소스를 찾을 수 있습니다 또한 SQL 주석과 같은 일부 컨텍스트 정보 (원점)을 추가 얇은 장식,Entity Framework 쿼리에 디버그 정보 가져 오기

/* Foo.Bar.GetOrders() */ SELECT * FROM Order WHERE orderId > 123 

같은 것을 만들어 DB 호출이 잘못되었거나 성능 히트가 발생하면 문제가 발생합니다 (하루에 수십만 DB 호출이 발생하므로 하나의 잘못된 쿼리로 인해 상당한 피해가 발생할 수 있습니다).

EF에서도이 작업을 수행하고 싶습니다. 그것은 SQL 주석이 될 필요는 없지만 호출과 함께 제출 된 메타 정보를 제공하기 위해서는 어떤 종류의 후크가 필요합니다. 이것이 가능한지 어떤 생각? 당신의 조언을

감사

필립

+0

(HTTP : // MSDN microsoft.com/en-us/magazine/dn532202.aspx). 나는 그걸 가지고 노는 것을 좋아하지 않았지만, 가까운 미래에 시간을 가질 수 있으며, 여기에 다시보고 할 것입니다. –

답변

4

밝혀이 EF 6. 매우 간단하게 모든 것을 필요한는 나를 정의에 제출 된 SQL을 확대 할 수 IDbCommandInterceptor의 구현 (SQL) 주석. 이 주석은 데이터베이스 로그에 나타나므로 DBA 측에서 디버깅/추적을 가능하게합니다. 위의 인터셉터가 운영 얻기 위하여

public class DebugCommentInterceptor : IDbCommandInterceptor 
{ 
    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) 
    { 
     command.CommandText = "/* TRACING INFORMATION GOES HERE */ " + command.CommandText; 
    } 

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) 
    { 
     command.CommandText = "/* TRACING INFORMATION GOES HERE */ " + command.CommandText; 
    } 

    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) 
    { 
    } 

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext) 
    { 
    } 

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) 
    { 
    } 

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext) 
    { 
    } 
} 

, 나는 단순히 정적 DbInterception 클래스로 등록 : 난 그냥 EF 6 더 나은 차단 capabilitie을 제공한다는 것을 발견

DbInterception.Add(new DebugCommentInterceptor()); 
관련 문제