In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
EntityFramework logs and records errors and analyzes the reasons for the long execution time. In view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Today we are going to talk about the logging of EF.
A good database operation record can not only help you record the user's actions.
What's more, it should help you get inefficient sentences to help you improve your running efficiency.
Environment and related technologies
The environment and technology adopted in this paper
System: WIN7
Database: SQL Server2008
Related technology: MVC5 EF6.0+
A simple record
I. modify the configuration file
Let's first take a look at the simplest EF logging. There is no need to change any code. Add the following configuration to your configuration file and record it automatically:
Add the following configuration under your EntityFramework node (note here that the first parameter is the output address of your log):
We can find the relevant log files under the corresponding address.
Second, simple packaging:
The base class for writing your own DBContext is as follows:
Public class DataBaseContext: DbContext where DbContext where SaveChanges method public override int SaveChanges () {string sql = ""; / / record the entity operation log this.Database.Log = (a) = > {sql + = a;}; / / the sql here is the operation log. Return base.SaveChanges () }}
To monitor through a low-level listening interface
If you just want to simply record, the above two ways should satisfy you.
In fact, the most important purpose of our recording is to analyze the performance and start our important show below.
Using IDbCommandInterceptor interface to monitor EF
People who have written ADO.NET should be familiar with these words. (because EF finally uses ADO.NET to access the database.)
Note: each execution has ed (monitoring after execution) and ing (monitoring at execution time)
Let's implement this interface step by step.
First define a class (name as you like):
/ / you can have any name, but be sure to inherit our listening interface--
Public class DatabaseLogger: IDbCommandInterceptor {}
And then we move on.
Define a static read-only ConcurrentDictionary as our record repository. Considering that multithreading is common in data access, we use thread-safe ConcurrentDictionary.
The code is as follows:
Public class DatabaseLogger: IDbCommandInterceptor {
Static readonly ConcurrentDictionary
MStartTime = new ConcurrentDictionary ();}
Next, let's implement the two methods we need, one for onStart to record the start time of SQL statement execution.
It is as follows:
/ / record the time when execution starts private static void OnStart (DbCommand command) {MStartTime.TryAdd (command, DateTime.Now);}
Then implement our log method to record the relevant SQL statements and error messages
Rivate static void Log (DbCommand command
DbCommandInterceptionContext interceptionContext) {DateTime startTime; TimeSpan duration; / / get the start time of this command MStartTime.TryRemove (command, out startTime)
If (startTime! = default (DateTime)) {duration = DateTime.Now-startTime;} elseduration = TimeSpan.Zero
Var parameters = new StringBuilder (); / / Loop to get the parameter value foreach (DbParameter param in command.Parameters) {parameters.AppendLine (param.ParameterName + "" + param.DbType + "=" + param.Value);}
/ / determine whether the statement has been executed for more than 1 second or whether there is an error if (duration.TotalSeconds > 1 | | exceptionContext.Exceptioninvalid null) {
/ / write the code here to record the execution of SQL statements and error messages for a long time}
Else {
/ / write your own code to record ordinary SQL statements here.
}
Now that we have got what we want, the specific way of recording will be random, so I will not write it down.
Then, we will implement six methods of this interface, as follows:
Public void NonQueryExecuted (DbCommand command, DbCommandInterceptionContext interceptionContext) {Log (command, interceptionContext)
}
Public void NonQueryExecuting (DbCommand command, DbCommandInterceptionContext interceptionContext) {OnStart (command);}
Public void ReaderExecuted (DbCommand command, DbCommandInterceptionContext interceptionContext) {Log (command, interceptionContext);}
Public void ReaderExecuting (DbCommand command, DbCommandInterceptionContext interceptionContext) {OnStart (command);}
Public void ScalarExecuted (DbCommand command, DbCommandInterceptionContext interceptionContext) {Log (command, interceptionContext);}
Public void ScalarExecuting (DbCommand command, DbCommandInterceptionContext interceptionContext) {
OnStart (command);}
In fact, it is very simple that all ing executes the OnStart method we wrote earlier, and all ed executes our log method.
Next, we need to inject this interface:
Here my Demo uses MVC, so I injected it directly into Application_Start (), as follows:
Protected void Application_Start () {/ / inject the listening DbInterception.Add (new MiniProfiler_EFModel.DatabaseLogger ()) written by yourself;}
In this way, we have completed the whole process of monitoring.
This is the answer to the question about the logging method of EntityFramework and the reason why it takes too long to record errors and analyze the execution time. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.