protected LogEntry CreateEntry(DbTracing tracing) { if (tracing.Exception != null) { using (var writer = new StringWriter()) { writer.WriteLine("执行数据查询时出现异常"); writer.WriteLine("执行的查询:"); writer.WriteLine(tracing.QueryObject); writer.WriteLine(new string( '-', 30 )); if (tracing.CommandObject != null) { writer.WriteLine("命令对象:"); writer.WriteLine(tracing.CommandObject); writer.WriteLine(new string( '-', 30 )); } writer.WriteLine("查询花费时间: {0}", tracing.QueryTime); writer.WriteLine("异常详细信息:"); writer.WriteLine(tracing.Exception); writer.WriteLine(new string( '=', 30 )); return(new LogEntry(writer.ToString(), new LogMeta() { Type = LogType.Exception })); } } else { using (var writer = new StringWriter()) { writer.WriteLine("成功执行查询"); writer.WriteLine("执行的查询:"); writer.WriteLine(tracing.QueryObject); writer.WriteLine(new string( '-', 30 )); writer.WriteLine("命令对象:"); writer.WriteLine(tracing.CommandObject); writer.WriteLine(new string( '-', 30 )); writer.WriteLine("数据库执行时间: {0}", tracing.ExecutionTime); writer.WriteLine("查询总计花费: {0}", tracing.QueryTime); writer.WriteLine(new string( '=', 30 )); return(new LogEntry(writer.ToString(), new LogMeta() { Type = LogType.Info })); } } }
protected virtual void LogQueryCompleted(DbTracing tracing) { if (tracing.Exception != null) { Logger.LogError(tracing.Exception, $"execute query {tracing.QueryObject} error. elapsed {tracing.QueryTime.TotalMilliseconds}ms."); } else { Logger.LogInformation(tracing.Exception, $"execute query {tracing.QueryObject} success. elapsed {tracing.QueryTime.TotalMilliseconds}ms."); } }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); // Enable Tracing queries DbTracing.Enable(); // Adding the listener (implementation of IDbTracingListener) DbTracing.AddListener(new DbTracingListener()); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); AuthConfig.RegisterAuth(); DbTracing.Enable( new GenericDbTracingListener() .OnFinished(c => logger.Trace("-- Command finished - time: {0}{1}{2}", c.Duration, Environment.NewLine, c.Command.ToTraceString())) .OnFailed(c => logger.Trace("-- Command failed - time: {0}{1}{2}", c.Duration, Environment.NewLine, c.Command.ToTraceString())) ); }
public void Can_catch_execute_sql_command() { var mock = MockListener(); DbTracing.AddListener(mock.Object); try { using (var context = new CodeFirstContext()) { context.Database.ExecuteSqlCommand("select 1; select @date", new SqlParameter("date", DateTime.Now)); context.SaveChanges(); } } finally { DbTracing.RemoveListener(mock.Object); } mock.Verify(); }
public void Can_catch_entity_framework_command() { var mock = MockListener(reader: true); DbTracing.AddListener(mock.Object); try { using (var context = new CodeFirstContext()) { context.TestEntity1.Add(new TestEntity1()); context.TestEntity2.Add(new TestEntity2()); context.SaveChanges(); } } finally { DbTracing.RemoveListener(mock.Object); } mock.Verify(); }
protected virtual void LogTracing(DbTracing tracing) { Logger.LogEntry(CreateEntry(tracing)); }
static DbTracingTests() { Bootstrap.Startup(); DbTracing.Enable(); }
private static void EnableTracing() { DbTracing.Enable(); DbTracing.AddListener(new EFTraceListener()); }