RDIFramework.NET ━ .NET Rapid Information System Development Framework V3.2->Add record SQL execution process

  Sometimes we need to record the SQL that the entire system runs for analysis, especially before going online. This is also very helpful for us to do internal testing. Of course, there are many ways to record SQL, and third-party components can also be used. In version 3.2, we added the logging of all SQl processes running by the framework at the bottom of the framework and saved them to the place specified by the user for analysis and viewing. Just set the configuration item "LogSQL" to True in the configuration file. The framework will automatically record the operation of various commonly used databases such as Oracle, SqlServer, MySQL, etc.
  1. Web record Sql execution
  1. To record SQL in our Web project, we can set the LogSql configuration item in the Web configuration file to True, the default is False, the location of the configuration file MVC project is in the RDIFramework.MvcApp project root directory The system.config file in the XmlConfig folder below, the WebForm project is the Web.Config file in the root directory of the RDIFramework.WebApp project, and the configuration items are set as shown in the following figure:

  As long as LogSQL is set to True, the framework will automatically record all Sql execution processes and save them to the specified directory, generally under the Log folder in the root directory of the Web project by default, as shown below:

 

  2. View the recorded Sql.

  Open a file and check the Sql situation recorded in the year, as follows:

  2. WinForm records the execution of Sql

  The WinForm project record Sql is similar to the Web, and it is the same to modify the configuration item of the record Sql in Config.xml, as shown in the following figure.

  After the above configuration, we open the framework and do some operations to view the recorded Sql situation, as shown below:

  3. Public method call

  If we need to record the execution of sql separately, we can call the public interface provided by the framework, as shown in the following figure:

  The three public interfaces for writing logs are shared below. You can refer to them if you need them.

#region public virtual void WriteLog(string commandText, string fileName = null) Write sql query log
        /// <summary>
        /// Write the sql query 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>
        /// Write the sql query log
        /// </summary>
        /// <param name="commandText">异常</param>
        /// <param name="fileName">filename</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;
            // It should be possible to configure whether to record exceptions in the system
            if (!SystemInfo.LogSQL)
            {
                return;
            }
            // Write exception information to 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);
                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)
        {
            // It should be possible to configure whether to record exceptions in the system
            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内容" + System.Environment.NewLine + commandText;
            if (dbParameters != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var parameter in dbParameters)
                {
                    sb.AppendLine(parameter.ParameterName + "=" + parameter.Value);
                }
                message += System.Environment.NewLine + "dbParameters内容" + System.Environment.NewLine + 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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326062979&siteId=291194637