Dapper extended SQL tracing and global cache notifications

Source: Internet
Author: User

The specific reason is not to say, the current project is composed of two parts, part is based on framework development, and the other is because the early business is the use of custom development model, although the data access layer is based on dapper,

But to do the global SQL tracking and cache notification, adjust the code is still a bit of trouble, and finally do a simple extension of a dapper source code, from the bottom to deal with.

Adjust the dapper source code has a very important class: CommandDefinition.cs

1. Execute SQL Pre-intercept event

2, after the execution of SQL interception event (after failure will not be triggered)

3, SQLMapper.cs add a line of code (here should be the author omitted, not read)

4. Attach SqlMapperTrace.cs Source code

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceDapper8 {9     /// <summary>Ten     ///SQL Execution Intercept event One     /// </summary> A     /// <param name= "Traceinfo" ></param> -      Public Delegate voidBeforecommandexecute (Traceinfo traceinfo); -     /// <summary> the     ///block events After SQL Run succeeds -     /// </summary> -     /// <param name= "Traceinfo" ></param> -      Public Delegate voidAftercommandexecute (Traceinfo traceinfo); +     /// <summary> -     ///Dappersql Performing traces +     /// </summary> A      Public classSqlmappertrace at     { -         StaticBeforecommandexecute Beforesqlcommand =NULL; -         StaticAftercommandexecute Aftersqlcommand =NULL; -         /// <summary> -         ///set Dappersql to perform interception events -         /// </summary> in         /// <param name= "Beforeexecutetrace" >Pre-execution Events</param> -         /// <param name= "Afterexecutetrace" >post-execution events</param> to          Public Static voidSetmappertrace (Beforecommandexecute beforeexecutetrace, Aftercommandexecute afterexecutetrace) +         { -             if(NULL!=Beforesqlcommand) the                 return; *Beforesqlcommand =Beforeexecutetrace; $Aftersqlcommand =Afterexecutetrace;Panax Notoginseng  -         } the  +       /// <summary> A       ///Execute SQL Pre-run intercept event the       /// </summary> +       /// <param name= "Traceinfo" ></param> -         Internal Static voidShellbeforecommandexecute (traceinfo traceinfo) $         { $             if(NULL!=Beforesqlcommand) - Beforesqlcommand (traceinfo); -         } the  -         /// <summary>Wuyi         ///The Execute SQL operation successfully intercepts the event, and if SQL execution fails, this method does not trigger the         /// </summary> -         /// <param name= "Traceinfo" ></param> Wu          Public Static voidShellaftercommandexecute (traceinfo traceinfo) -         { About             if(NULL!=Aftersqlcommand) $ Aftersqlcommand (traceinfo); -         } -     } -  A     /// <summary> +     ///SQL Execution State the     /// </summary> -      Public enumSqlState $     { the         /// <summary> the         ///in execution the         /// </summary> the Start, -         /// <summary> in         ///Complete the         /// </summary> the End, About         /// <summary> the         ///failed the         /// </summary> the Error +     } -  the     /// <summary>Bayi     ///Tracking Information the     /// </summary> the      Public classTraceinfo -     { -         /// <summary> the         ///operation identify key, use to track whether the same SQL is executed successfully the         /// </summary> the          Public stringToken {Get;Set; } the         /// <summary> -         ///SQL statements the         /// </summary> the          PublicString CommandText {Get;Set; } the         /// <summary>94         ///SQL Parameters the         /// </summary> the          Public ObjectSqlparams {Get;Set; } the 98         /// <summary> About         ///whether it is starting -         /// </summary>101          PublicSqlState Isstart {Get;Set; }102         /// <summary>103         ///execution time, performance statistics, you can according to two times the time difference to achieve104         /// </summary> the          PublicDateTime Executetime {Get;Set; }106 107         /// <summary>108         ///Prompt Information109         /// </summary> the          Public stringMessage {Get;Set; }111     } the 113}

5. Call Intercept Injection

6. Special Description:

Because of the database execution error in dapper, the SQL execution completion event is no longer executed, that is (the OnCompleted event of the 2nd step), so in the framework I deal with a layer

This makes it easy to implement SQL statement tracking, and the next step is to continue the log write and detach the table name from the SQL statement and execute the action, caching notification processing with Redis mates.

Dapper extended SQL tracing and global cache notifications

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.