Example #1
0
        public void Does_log_all_statements()
        {
            var sbLogFactory = new StringBuilderLogFactory();

            LogManager.LogFactory = sbLogFactory;

            using (var db = OpenDbConnection())
            {
                db.DropTable <LogTest>();
                db.CreateTable <LogTest>();

                db.Insert(new LogTest {
                    CustomerId = 2, Name = "Foo"
                });

                var test = db.Single <LogTest>(x => x.CustomerId == 2);

                test.Name = "Bar";

                db.Update(test);

                test = db.Single <LogTest>(x => x.CustomerId == 2);

                db.DeleteById <LogTest>(test.Id);

                var logs = sbLogFactory.GetLogs();
                logs.Print();

                Assert.That(logs, Is.StringContaining("CREATE TABLE"));
                Assert.That(logs, Is.StringContaining("INSERT INTO"));
                Assert.That(logs, Is.StringContaining("SELECT"));
                Assert.That(logs, Is.StringContaining("UPDATE"));
                Assert.That(logs, Is.StringContaining("DELETE FROM"));
            }
        }
        public void Does_log_SQL_Insert_for_Saves_with_Auto_Ids()
        {
            var sbLogFactory = new StringBuilderLogFactory();

            OrmLiteConfig.ResetLogFactory(sbLogFactory);

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <PersonWithAutoId>();

                db.Save(new PersonWithAutoId {
                    Id = 1, FirstName = "first", LastName = "last", Age = 27
                });
            }

            var sql = sbLogFactory.GetLogs();

            Assert.That(sql, Does.Contain("INSERT INTO"));
            OrmLiteConfig.ResetLogFactory(null);
        }
        static void Main2(string[] args)
        {
            var sbLogFactory = new StringBuilderLogFactory();
            LogManager.LogFactory = sbLogFactory;
            var log = LogManager.GetLogger(typeof(Program));

            var clientManager = new PooledRedisClientManager(new[] { "localhost" })
            {
                PoolTimeout = 1000,
            };

            var mqHost = new RedisMqServer(clientManager, retryCount: 2);

            var msgsProcessed = 0;
            var sum = 0;
            mqHost.RegisterHandler<Incr>(c =>
            {
                var dto = c.GetBody();
                sum += dto.Value;
                log.InfoFormat("Received {0}, sum: {1}", dto.Value, sum);
                msgsProcessed++;
                return null;
            });

            mqHost.Start();

            10.Times(i =>
            {
                ThreadPool.QueueUserWorkItem(x =>
                {
                    using (var client = mqHost.CreateMessageQueueClient())
                    {
                        try
                        {
                            log.InfoFormat("Publish: {0}...", i);
                            client.Publish(new Incr { Value = i });
                        }
                        catch (Exception ex)
                        {
                            log.InfoFormat("Start Publish exception: {0}", ex.Message);
                            clientManager.GetClientPoolActiveStates().PrintDump();
                            clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                        }
                        Thread.Sleep(10);
                    }
                });
            });

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var client = (RedisClient)clientManager.GetClient())
                {
                    client.SetConfig("timeout", "1");
                    var clientAddrs = client.GetClientList().ConvertAll(x => x["addr"]);
                    log.InfoFormat("Killing clients: {0}...", clientAddrs.Dump());

                    try
                    {
                        clientAddrs.ForEach(client.ClientKill);
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Client exception: {0}", ex.Message);
                    }
                }
            });

            20.Times(i =>
            {
                using (var client = mqHost.CreateMessageQueueClient())
                {
                    try
                    {
                        log.InfoFormat("Publish: {0}...", i);
                        client.Publish(new Incr { Value = i });
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Publish exception: {0}", ex.Message);
                        clientManager.GetClientPoolActiveStates().PrintDump();
                        clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                    }
                }

                Thread.Sleep(1000);
            });

            Thread.Sleep(2000);
            "Messages processed: {0}".Print(msgsProcessed);
            "Logs: ".Print();
            sbLogFactory.GetLogs().Print();
            Console.ReadKey();
        }
Example #4
0
        static void Main2(string[] args)
        {
            var sbLogFactory = new StringBuilderLogFactory();

            LogManager.LogFactory = sbLogFactory;
            var log = LogManager.GetLogger(typeof(Program));

            var clientManager = new PooledRedisClientManager(new[] { "localhost" })
            {
                PoolTimeout = 1000,
            };

            var mqHost = new RedisMqServer(clientManager, retryCount: 2);

            var msgsProcessed = 0;
            var sum           = 0;

            mqHost.RegisterHandler <Incr>(c =>
            {
                var dto = c.GetBody();
                sum    += dto.Value;
                log.InfoFormat("Received {0}, sum: {1}", dto.Value, sum);
                msgsProcessed++;
                return(null);
            });

            mqHost.Start();

            10.Times(i =>
            {
                ThreadPool.QueueUserWorkItem(x =>
                {
                    using (var client = mqHost.CreateMessageQueueClient())
                    {
                        try
                        {
                            log.InfoFormat("Publish: {0}...", i);
                            client.Publish(new Incr {
                                Value = i
                            });
                        }
                        catch (Exception ex)
                        {
                            log.InfoFormat("Start Publish exception: {0}", ex.Message);
                            clientManager.GetClientPoolActiveStates().PrintDump();
                            clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                        }
                        Thread.Sleep(10);
                    }
                });
            });

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var client = (RedisClient)clientManager.GetClient())
                {
                    client.SetConfig("timeout", "1");
                    var clientAddrs = client.GetClientList().ConvertAll(x => x["addr"]);
                    log.InfoFormat("Killing clients: {0}...", clientAddrs.Dump());

                    try
                    {
                        clientAddrs.ForEach(client.ClientKill);
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Client exception: {0}", ex.Message);
                    }
                }
            });

            20.Times(i =>
            {
                using (var client = mqHost.CreateMessageQueueClient())
                {
                    try
                    {
                        log.InfoFormat("Publish: {0}...", i);
                        client.Publish(new Incr {
                            Value = i
                        });
                    }
                    catch (Exception ex)
                    {
                        log.InfoFormat("Publish exception: {0}", ex.Message);
                        clientManager.GetClientPoolActiveStates().PrintDump();
                        clientManager.GetReadOnlyClientPoolActiveStates().PrintDump();
                    }
                }

                Thread.Sleep(1000);
            });

            Thread.Sleep(2000);
            "Messages processed: {0}".Print(msgsProcessed);
            "Logs: ".Print();
            sbLogFactory.GetLogs().Print();
            Console.ReadKey();
        }