SQL statement logs that are automatically generated and executed by the ef-logger

Source: Internet
Author: User

In EntityFramework's Codefirst mode, we want to log the SQL statements and execution procedures generated automatically by the program to the logs for easy viewing and analysis later.

In the 6.x version of EF, there is a Database attribute in DbContext, and Database.Log is used to specifically record such logs.

Database.Log is a action<string> delegate that assigns a value to a function on the line.

The code is as follows:

usingModel;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceDAL { Public classDemocontext:dbcontext {//connection string using the Name=efdemo         PublicDemocontext ():Base("EFDemo") {            //set the database initialization to the latest version of the Migration (update) database//democontext mapping databases and tables//DAL. Migrations.configuration is the migration configurationDatabase.setinitializer (NewMigratedatabasetolatestversion<democontext, DAL. Migrations.configuration>()); //logs the executed SQL statement to the logDatabase.Log = Message=>console.writeline ("[{0}]{1}--{2}", Thread.currentthread.managedthreadid,datetime.now.tostring ("YYYY/MM/DD HH:mm:ss.fff"), message.        Trim ()); }        //students property corresponds to the student table in the database         Public VirtualDbset<student> Students {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {Base.        Onmodelcreating (ModelBuilder); }    }}

The code in the application is as follows:

using(Democontext context =NewDemocontext ()) {                //forcing a database to be created before using the model class true: Force creationContext. Database.initialize (true); #regionNewStudent Stu=NewStudent {Name="Zhaozicheng", BirthDay= DateTime.Parse ("1990-08-01"), age= -                }; //Add a new student entity, equivalent to a new data in the student tablecontext.                              Students.add (Stu); //Savecontext.                              SaveChanges (); #endregion}

The results of the log record are as follows:

[Ten] ./ -/ in  -: $:28.971--Opened connection at ./ -/ in  -: $: -+ ,:xx[Ten] ./ -/ in  -: $:28.972--Started transaction at ./ -/ in  -: $: -+ ,:xx[Ten] ./ -/ in  -: $:28.977--DECLARE @generated_keys table ([ID] uniqueidentifier) INSERT [dbo]. [Student] ([Name], [BirthDay], [age], [Address]) OUTPUT inserted. [ID] into @generated_keysVALUES (@0, @1, @2, NULL) SELECT T.[id], T.[rowversion]from @generated_keys as G JOIN [dbo]. [Student] As T on G.[id]=T.[id]where @ @ROWCOUNT>0[Ten] ./ -/ in  -: $:28.977--[Ten] ./ -/ in  -: $:28.978-- -- @0:'Zhaozicheng'(Type = String, Size = -)[Ten] ./ -/ in  -: $:28.978-- -- @1:'1990/08/01 00:00:00'(Type =DateTime2) [Ten] ./ -/ in  -: $:28.978-- -- @2:' -'(Type =Int32) [Ten] ./ -/ in  -: $:28.978----Executing at ./ -/ in  -: $: -+ ,:xx[Ten] ./ -/ in  -: $:28.993----Completedinch  -ms with result:sqldatareader[Ten] ./ -/ in  -: $:28.993--[Ten] ./ -/ in  -: $:28.995--Committed transaction at ./ -/ in  -: $: -+ ,:xx[Ten] ./ -/ in  -: $:28.996--Closed connection at ./ -/ in  -: $: -+ ,:xx

SQL statement logs that are automatically generated and executed by the ef-logger

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.