Sometimes we need to record the entire system running SQL for analysis, especially before going online this is very helpful for us to do internal testing, of course, there are many ways to record SQL, you can also use three-party components. Version 3.2 We have added a record framework at the bottom of the framework to run all the SQL procedures saved to the user-specified location for analysis to view, only in the configuration file to set the configuration item "Logsql" to true. The framework automatically records the operations of common databases such as Oracle, SQL Server, MySQL, and so on.
I. Web record SQL execution
1, in our web project to record SQL can be set in the Web configuration file Logsql configuration item is true, the default is False, The location of the profile MVC project is the System.config file under the Xmlconfig folder under the root directory of the Rdiframework.mvcapp project, the WebForm project is the Web. config file under the Rdiframework.webapp project root directory, set Configuration items as shown in the following settings:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302144754220-1808252529. PNG "style=" border:0px; "/>
As long as the set Logsql is true, the framework automatically records all SQL executions and saves them to the specified directory, typically by default in the Web project root directory under the log folder, such as:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302144836360-1477826254. PNG "style=" border:0px; "/>
2. View the recorded SQL.
Open a file, check the SQL for the year record, as follows:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302144921016-760249391. PNG "style=" border:0px; "/>
Ii. WinForm Record of SQL execution
WinForm Project Record SQL is similar to the Web, and you want to modify the configuration entry for the record SQL in config. ins, as shown in.
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302144959688-1537648280. PNG "style=" border:0px; "/>
Through the above configuration, the same as we open the framework after doing some operations to view the logged SQL case, such as:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302145018845-228075886. PNG "style=" border:0px; "/>
Third, public method invocation
If we need to record the execution of SQL separately, we can invoke the public interface provided by the framework, as shown in:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/157572/201703/157572-20170302145040954-1150244236. PNG "style=" border:0px; "/>
The following three public interface to write a log to share, you need to refer to yo.
#region public virtual void writelog (string commandtext, string filename = null) Write SQL query sentence log /// <summary> /// writing SQL query clause log /// </summary> /// <param name= " CommandText "></param> public virtual void writelog (String commandtext) { string filename = datetime.now.tostring ( Systeminfo.dateformat) + " _ " + this. Filename; writelog (commandText, FileName); } /// <summary> /// writing SQL query clause log /// </summary> /// <param name= "CommandText" > Exceptions </param> /// <param name= "filename" > file name </param> public virtual void writelog (string commandText, String filename = null) { if (String. IsNullOrEmpty (fileName)) { fileName = DateTime.Now.ToString (Systeminfo.dateformat) + " _ " + this. filename; } string returnvalue = string. empty; // system should be able to configure whether or not to record abnormal phenomenon if (! Systeminfo.logsql) { return; } // Write exception information to a local file string logdirectory = systeminfo.startuppath + @ "\\Log\\Query"; if (! System.IO.Directory.Exists (logdirectory)) { System.IO.Directory.CreateDirectory (logdirectory); } string writerfilename = logdirectory + "\" + fileName; if (! File.exists (writerfilename)) { FileStream Filestream = new filestream (Writerfilename, filemode.create, fileaccess.readwrite, fileshare.readwrite); &nBsp; filestream.close (); } StreamWriter Streamwriter = new streamwriter (Writerfilename, true, encoding.default); streamwriter.writeline (DateTime.Now.ToString ( Systeminfo.datetimeformat) + " " + commandtext); streamwriter.close (); } public virtual void writelog (string commandtext, idbdataparameter[] dbparameters = null, string filename = null) { // system should beTo configure whether abnormal phenomena are logged if (! Systeminfo.logsql) { return; } if (String. IsNullOrEmpty (fileName)) { fileName = DateTime.Now.ToString (Systeminfo.dateformat) + " _ " + FileName; } string message = string. empty; message = datetime.now.tostring (Systeminfo.datetimeformat) + system.environment.newline + "CommandText content" + System.Environment.NewLine + commandtext; if (dbParameters != null) { stringbuilder sb = new stringbuilder (); foreach (var parameter in dbparameters) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SB. Appendline (parameter. parametername + "=" + paramEter. Value); } message += System.Environment.NewLine + "Dbparameters content" + system.environment.newline + &NBSP;SB. ToString (); } string logdirectory = systeminfo.startuppath + @ "\log\query"; if (! System.IO.Directory.Exists (logdirectory)) { System.IO.Directory.CreateDirectory (logdirectory); } string writerfilename = logdirectory + "\" + fileName; if (! File.exists (writerfilename)) { FileStream Filestream = new filestream (Writerfilename, filemode.create, fileaccess.readwrite, fileshare.readwrite); filestream.close (); } streamwriter streamwriter = new streamwriter (Writerfilename, true, encoding.default); streamwriter.writeline (DateTime.Now.ToString (Systeminfo.datetimeformat) + " " + message); StreamWriter.Close (); } #endregion
This article is from the "Rapid Information System Development Framework" blog, please be sure to keep this source http://chinahuyong.blog.51cto.com/2721990/1917754
Rdiframework.net━. NET rapid Information System development Framework v3.2-> new record SQL execution process