我正在尝试使用自动化测试运行期间正在执行的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);
}
}
}
}