csharp 控制台拦截器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 控制台拦截器相关的知识,希望对你有一定的参考价值。

class ConsoleWriterInterceptor : IDbCommandInterceptor
{
    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        WriteInfo(interceptionContext.IsAsync, command.CommandText);
    }

    private void WriteInfo(bool isAsync, string commandText)
    {
        Console.WriteLine($"IsAsync: {isAsync}, Command Text:{ commandText}");
    }
}
DbInterception.Add(new ConsoleWriterInterceptor());

以上是关于csharp 控制台拦截器的主要内容,如果未能解决你的问题,请参考以下文章

csharp DatabaseLogger(EF默认记录器拦截器)

csharp Log EntityFramework sql调用(拦截器甚至可以是更强的记录器)

使用数据库拦截器的实体框架软删除实现不起作用

csharp C#控制台提示用户输入

csharp 控制台记录器

csharp 控制台的标准输入