使用XtendedEvents C#无法捕获查询详细信息

时间:2020-02-05 15:05:58

标签: c# sql-server

我正在尝试使用自动化测试运行期间正在执行的C#通过SQL Server跟踪捕获实体框架查询。当我运行以下代码时,我得到了异常

问题

未捕获在后台执行的查询。我可以尝试获取在后台运行的select / insert / update查询的其他任何事情

代码

class Program
{
    static void Main(string[] args)
    {
        using (SqlConnection conn = new SqlConnection(@"Data Source = XXXXXX\XXXXX; Initial Catalog = Test; Integrated Security = SSPI"))
        {
            XEStore store = new XEStore(new SqlStoreConnection(conn));

            string sessionName = "abc";

            if (store.Sessions[sessionName] != null)
            {
                Console.WriteLine("dropping existing session");
                store.Sessions[sessionName].Drop();
            }

            Session s = store.CreateSession(sessionName);
            s.MaxMemory = 4096;
            s.MaxDispatchLatency = 30;
            s.EventRetentionMode = Session.EventRetentionModeEnum.AllowMultipleEventLoss;

            Event rpc = s.AddEvent("rpc_completed");
            rpc.AddAction("username");
            rpc.AddAction("database_name");
            rpc.AddAction("sql_text");
            rpc.PredicateExpression = @"sqlserver.username NOT LIKE '%testuser'";

            s.Create();
            s.Start();

            int i = 0;
            while (i < 15000)
            {
                s.Refresh();
                foreach (var prop in rpc.Actions)
                {
                    Console.WriteLine(prop.Description);
                    Console.WriteLine(prop.KeyChain);
                    Console.WriteLine(prop.IdentityKey);
                    Console.WriteLine(prop.Metadata);
                    Console.WriteLine(prop.ModuleID);
                    Console.WriteLine(prop.Name);
                    Console.WriteLine(prop.PackageName);
                    Console.WriteLine(prop.Parent);
                    Console.WriteLine(prop.Properties);
                    Console.WriteLine(prop.State);
                    Console.WriteLine(prop.Urn);

                }
                Thread.Sleep(1000);
            }
        }
    }
}

0 个答案:

没有答案