A simple logger for logging SQL and other database operations to the console or a file. A logger can be registered in code or in the application's web.config /app.config file.
Inheritance: IDisposable, IDbConfigurationInterceptor
Esempio n. 1
0
        public void DatabaseLogger_can_append_to_a_file()
        {
            using (var context = new SimpleModelContext())
            {
                var output = CaptureFileOutput(
                    f =>
                    {
                        using (var logger = new DatabaseLogger(f, append: false))
                        {
                            logger.StartLogging();

                            context.Categories.ToArray();
                        }

                        using (var logger = new DatabaseLogger(f, append: true))
                        {
                            logger.StartLogging();

                            context.Products.ToArray();
                        }
                    });

                Assert.Contains("FROM [dbo].[Categories]", output);
                Assert.Contains("FROM [dbo].[Products]", output);
            }
        }
Esempio n. 2
0
        public void DatabaseLogger_can_log_to_a_file()
        {
            Assert.Contains(
                "FROM [dbo].[Products]",
                CaptureFileOutput(
                    f =>
                    {
                        using (var logger = new DatabaseLogger(f))
                        {
                            using (var context = new SimpleModelContext())
                            {
                                logger.StartLogging();

                                context.Products.ToArray();
                            }
                        }
                    }));
        }
Esempio n. 3
0
        public void Logging_can_be_started_and_stopped()
        {
            using (var context = new SimpleModelContext())
            {
                var output = CaptureConsoleOutput(
                    () =>
                    {
                        using (var logger = new DatabaseLogger())
                        {
                            context.Products.ToArray();

                            logger.StartLogging();
                            logger.StopLogging();

                            context.Products.ToArray();

                            logger.StartLogging();

                            context.Categories.ToArray();

                            logger.StopLogging();

                            context.Products.ToArray();
                        }
                    });

                var foundIndex = output.IndexOf("FROM [dbo].[Categories]");
                Assert.True(foundIndex > 0);
                foundIndex = output.IndexOf("FROM [dbo].[Categories]", foundIndex + 1);
                Assert.Equal(-1, foundIndex);

                Assert.DoesNotContain("FROM [dbo].[Products]", output);
            }
        }
Esempio n. 4
0
        public void Dispose_stops_logging()
        {
            using (var context = new SimpleModelContext())
            {
                var output = CaptureConsoleOutput(
                    () =>
                    {
                        using (var logger = new DatabaseLogger())
                        {
                            logger.StartLogging();

                            context.Categories.ToArray();
                        }

                        context.Products.ToArray();
                    });

                Assert.Contains("FROM [dbo].[Categories]", output);
                Assert.DoesNotContain("FROM [dbo].[Products]", output);
            }
        }
Esempio n. 5
0
        public void Starting_is_no_op_if_already_started_and_likewise_for_stopping()
        {
            using (var context = new SimpleModelContext())
            {
                var output = CaptureConsoleOutput(
                    () =>
                    {
                        using (var logger = new DatabaseLogger())
                        {
                            logger.StopLogging();

                            context.Products.ToArray();

                            logger.StartLogging();
                            logger.StartLogging();

                            context.Categories.ToArray();

                            logger.StopLogging();
                            logger.StopLogging();

                            context.Products.ToArray();
                        }
                    });

                Assert.Contains("FROM [dbo].[Categories]", output);
                Assert.DoesNotContain("FROM [dbo].[Products]", output);
            }
        }
        [Fact] // CodePlex 2568
        public void DatabaseLogger_can_be_used_concurrently()
        {
            CaptureFileOutput(
                f =>
                {
                    using (var logger = new DatabaseLogger(f))
                    {
                        logger.StartLogging();

                        ExecuteInParallel(
                            () =>
                            {
                                using (var context = new SimpleModelContext())
                                {
                                    for (var i = 0; i < 200; i++)
                                    {
                                        context.Products.AsNoTracking().Load();
                                    }
                                }
                            }, 30);
                    }
                });
        }