Tool:Visual Studio 2013 Ultimate Update 2 OS:Windows 8.1
.NET Framework : 4.5.x
Entity Framework : 6.1.x版以上
ASP.NET MVC 5
Entity Framework提供一個簡單的機制,可以將SQL操作自動記錄起來,寫到log。更好的是,不需編譯程式碼,只要修改組態檔案就行了。
在網站組態檔案之中,加入以下interceptors設定:
<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />
    </parameters>
  </defaultConnectionFactory>
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
  <interceptors>
    <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger,EntityFramework">
      <parameters>
        <parameter value="c:\temp\mylog.log"/>
        <parameter value="true" type="System.Boolean"/>
      </parameters>
    </interceptor>
  </interceptors>
</entityFramework>
第二個參數表示,log資料會附加到現有檔案,不會將原有檔案內容蓋掉。
存檔之後,只要執行程式,使用到Entity Framework功能,就會產生log檔案,參考記錄的log檔案內容:
 
 
沒有留言:
張貼留言