EF6 SQL Logging – Part 2: Changing the content/formatting | One Unicorn
2016-05-21 13:17
351 查看
In part 1 we saw how DbContext.Database.Log can be used to easily log SQL to the console or some other TextWriter. This post covers how the context and formatting of the output can be changed.
Changing what is logged and how it is formatted can be achieved by creating a new class that derives from DatabaseLogFormatter and then overriding methods as appropriate. The most common methods to override are:
LogCommand – Override this to change how commands are logged before they are executed. By default LogCommand calls LogParameter for each parameter; you may choose to do the same in your override or handle parameters differently instead.
LogResult – Override this to change how the outcome from executing a command is logged.
LogParameter – Override this to change the formatting and content of parameter logging.
For example, suppose we wanted to log just a single line before each command is sent to the database. This can be done with two overrides:
Override LogCommand to format and write the single line of SQL
Override LogResult to do nothing. (Executed could be overridden instead and this would probably be slightly more efficient, but functionally equivalent.)
The code would look something like this:
To log output simply call the Write method which will send output to the configured write delegate.
(Note that this code does simplistic removal of line breaks just as an example. It will likely not work well for viewing complex SQL.)
转载:https://blog.oneunicorn.com/2013/05/09/ef6-sql-logging-part-2-changing-the-contentformatting/
来自为知笔记(Wiz)
DatabaseLogFormatter
Under the covers the Database.Log property makes use of a DatabaseLogFormatter object. This object effectively binds a IDbCommandInterceptor implementation (see part 3 of this series) to a delegate that accepts strings and a DbContext. This means that interception methods on DatabaseLogFormatter are called before and after the execution of commands by EF. These DatabaseLogFormatter methods gather and format log output and send it to the delegate.Changing what is logged and how it is formatted can be achieved by creating a new class that derives from DatabaseLogFormatter and then overriding methods as appropriate. The most common methods to override are:
LogCommand – Override this to change how commands are logged before they are executed. By default LogCommand calls LogParameter for each parameter; you may choose to do the same in your override or handle parameters differently instead.
LogResult – Override this to change how the outcome from executing a command is logged.
LogParameter – Override this to change the formatting and content of parameter logging.
For example, suppose we wanted to log just a single line before each command is sent to the database. This can be done with two overrides:
Override LogCommand to format and write the single line of SQL
Override LogResult to do nothing. (Executed could be overridden instead and this would probably be slightly more efficient, but functionally equivalent.)
The code would look something like this:
12345678910111213141516171819202122 | public class OneLineFormatter :DatabaseLogFormatter { public OneLineFormatter(DbContext context, Action< string > writeAction) : base (context, writeAction) { } public override void LogCommand<TResult>( DbCommand command,DbCommandInterceptionContext<TResult> interceptionContext) { Write( string .Format( "Context '{0}' is executing command '{1}'{2}" , Context.GetType().Name, command.CommandText.Replace(Environment.NewLine, "" ), Environment.NewLine)); } public override void LogResult<TResult>( DbCommand command, object result, DbCommandInterceptionContext<TResult> interceptionContext) { } } |
(Note that this code does simplistic removal of line breaks just as an example. It will likely not work well for viewing complex SQL.)
Setting the DatabaseLogFormatter
Once a new DatabaseLogFormatter class has been created it needs to be registered with EF. This is done using code-based configuration. In a nutshell this means creating a new class that derives from DbConfiguration in the same assembly as your DbContext class and then calling SetDatabaseLogFormatter in the constructor of this new class. For example:12345678 | public class MyDbConfiguration :DbConfiguration { public MyDbConfiguration() { SetDatabaseLogFormatter( (context, writeAction) => new OneLineFormatter(context, writeAction)); } } |
Using the new DatabaseLogFormatter
This new DatabaseLogFormatter will now be used anytime Database.Log is set. So, running the code from part 1 will now result in the following output:Context 'BlogContext' is executing command 'SELECT TOP (1) [Extent1].[Id] AS [Id], [Extent1].[Title] AS [Title]FROM [dbo].[Blogs] AS [Extent1]WHERE (N'One Unicorn' = [Extent1].[Title]) AND ([Extent1].[Title] IS NOT NULL)' Context 'BlogContext' is executing command 'SELECT [Extent1].[Id] AS [Id], [Extent1].[Title] AS [Title], [Extent1].[BlogId] AS [BlogId]FROM [dbo].[Posts] AS [Extent1]WHERE [Extent1].[BlogId] = @EntityKeyValue1' Context 'BlogContext' is executing command 'update [dbo].[Posts]set [Title] = @0where ([Id] = @1)' Context 'BlogContext' is executing command 'insert [dbo].[Posts]([Title], [BlogId])values (@0, @1)select [Id]from [dbo].[Posts]where @@rowcount > 0 and [Id] = scope_identity()'
More control?
In the next post we’ll take a look at implementing IDbCommandInterceptor directly for even more control of command interception and show an example that integrates directly with NLog without using the Database.Log property.转载:https://blog.oneunicorn.com/2013/05/09/ef6-sql-logging-part-2-changing-the-contentformatting/
来自为知笔记(Wiz)
相关文章推荐
- EF6 SQL Logging – Part 1: Simple Logging | One Unicorn
- Mysql备份与恢复
- 数据库设计
- MySql--数据表中文乱码解决办法
- sqlalchemy 的 ORM 与 Core 混合方式使用示例
- mysql 批量更新的四种方法
- oracle 用函数查询怎么直接返回一个表
- MYSQL用户操作
- SQL SERVER 字符串函数
- sqlalchemy 的 Core 方式使用示例
- SQL Server 2008 ——关系
- 【Linux】redis集群
- 了解数据库语句
- 使用C# 怎么生成.db后缀的文件,并且能够使用sql语言进行读取等 操作
- 图解用MySQL创建进销存数据库
- 在sqlserver中如何导入.db数据库文件
- jsp excel数据 添加到 数据库表中
- DB扩展名的数据库文件怎么打开:两种db数据库的打开方式
- Redis简介,安装和配置,停止,卸载(图解方式)
- sql 相关