public void NonQueryExecuting_validates_arguments()
            {
                var formatter = new DatabaseLogFormatter(new StringWriter().Write);

                Assert.Equal(
                    "command",
                    Assert.Throws<ArgumentNullException>(() => formatter.NonQueryExecuting(null, new DbCommandInterceptionContext<int>()))
                        .ParamName);
                Assert.Equal(
                    "interceptionContext",
                    Assert.Throws<ArgumentNullException>(() => formatter.NonQueryExecuting(new Mock<DbCommand>().Object, null)).ParamName);
            }
        private void StartLogging(IDbDependencyResolver resolver)
        {
            DebugCheck.NotNull(resolver);

            if (_formatter == null)
            {
                _formatter = resolver.GetService<Func<DbContext, Action<string>, DatabaseLogFormatter>>()(
                    null,
                    WriteThreadSafe);

                DbInterception.Add(_formatter);
            }
        }
 /// <summary>
 ///     Stops logging. This method is a no-op if logging is not started.
 /// </summary>
 public void StopLogging()
 {
     if (_formatter != null)
     {
         DbInterception.Remove(_formatter);
         _formatter = null;
     }
 }
            public void Opened_validates_arguments()
            {
                var formatter = new DatabaseLogFormatter(new StringWriter().Write);

                Assert.Equal(
                    "connection",
                    Assert.Throws<ArgumentNullException>(
                        () => formatter.Opened(null, new DbConnectionInterceptionContext()))
                        .ParamName);
                Assert.Equal(
                    "interceptionContext",
                    Assert.Throws<ArgumentNullException>(() => formatter.Opened(new Mock<DbConnection>().Object, null)).ParamName);
            }
 private static long GetElapsed(DatabaseLogFormatter formatter)
 {
     formatter.Stopwatch.Restart();
     Thread.Sleep(10);
     formatter.Stopwatch.Stop();
     return formatter.Stopwatch.ElapsedMilliseconds;
 }
            public void LogResult_logs_elapsed_time_for_canceled_commands()
            {
                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);
                var elapsed = GetElapsed(formatter);

                var interceptionContext = new DbCommandInterceptionContext<int>();
                interceptionContext.MutableData.TaskStatus = TaskStatus.Canceled;

                formatter.LogResult(new Mock<DbCommand>().Object, interceptionContext);

                Assert.Equal(Strings.CommandLogCanceled(elapsed, ""), GetSingleLine(writer));
            }
            public void LogResult_logs_elapsed_time_for_failed_commands()
            {
                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);
                var elapsed = GetElapsed(formatter);

                formatter.LogResult(
                    new Mock<DbCommand>().Object,
                    new DbCommandInterceptionContext<int> { Exception = new Exception("I do not like them!") });

                Assert.Equal(Strings.CommandLogFailed(elapsed, "I do not like them!", ""), GetSingleLine(writer));
            }
            public void LogResult_logs_elapsed_time_for_completed_commands()
            {
                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);
                var elapsed = GetElapsed(formatter);

                formatter.LogResult(new Mock<DbCommand>().Object, new DbCommandInterceptionContext<int> { Result = 77 });

                Assert.Equal(Strings.CommandLogComplete(elapsed, "77", ""), GetSingleLine(writer));
            }
            public void Executed_logs_every_command_when_context_not_set()
            {
                var context1 = new Mock<DbContext>().Object;
                var context2 = new Mock<DbContext>().Object;

                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);

                formatter.Executed(CreateCommand(""), new DbCommandInterceptionContext<string> { Result = "Would you like them" });

                var interceptionContext = new DbCommandInterceptionContext<string>().WithDbContext(context1);
                interceptionContext.Result = "Here or there?";
                formatter.Executed(CreateCommand(""), interceptionContext);

                interceptionContext = new DbCommandInterceptionContext<string>().WithDbContext(context2);
                interceptionContext.Result = "I would not like them";
                formatter.Executed(CreateCommand(""), interceptionContext);

                var lines = GetLines(writer);
                Assert.Equal(Strings.CommandLogComplete(0, "Would you like them", ""), lines[0]);
                Assert.Equal(Strings.CommandLogComplete(0, "Here or there?", ""), lines[2]);
                Assert.Equal(Strings.CommandLogComplete(0, "I would not like them", ""), lines[4]);
            }
            public void Executed_filters_by_context_when_set()
            {
                var context1 = new Mock<DbContext>().Object;
                var context2 = new Mock<DbContext>().Object;

                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);

                var interceptionContext = new DbCommandInterceptionContext<string>().WithDbContext(context1);
                interceptionContext.Result = "Sam-I-am";
                formatter.Executed(CreateCommand(""), interceptionContext);

                interceptionContext = new DbCommandInterceptionContext<string>().WithDbContext(context2);
                interceptionContext.Result = "I do not like";
                formatter.Executed(CreateCommand(""), interceptionContext);

                formatter.Executed(CreateCommand(""), new DbCommandInterceptionContext<string> { Result = "Green eggs and ham" });

                Assert.Equal(Strings.CommandLogComplete(0, "Sam-I-am", ""), GetSingleLine(writer));
            }
            public void Executing_logs_every_command_when_context_not_set()
            {
                var context1 = new Mock<DbContext>().Object;
                var context2 = new Mock<DbContext>().Object;

                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);
                formatter.Executing(CreateCommand("Do you like"), new DbCommandInterceptionContext<int>());
                formatter.Executing(CreateCommand("Green eggs and ham?"), new DbCommandInterceptionContext<int>().WithDbContext(context1));
                formatter.Executing(CreateCommand("I do not like them"), new DbCommandInterceptionContext<int>().WithDbContext(context2));

                var lines = GetLines(writer);
                Assert.Equal("Do you like", lines[0]);
                Assert.Equal("Green eggs and ham?", lines[2]);
                Assert.Equal("I do not like them", lines[4]);
            }
            public void Executing_filters_by_context_when_set()
            {
                var context1 = new Mock<DbContext>().Object;
                var context2 = new Mock<DbContext>().Object;

                var writer = new StringWriter();
                var formatter = new DatabaseLogFormatter(writer.Write);
                formatter.Executing(CreateCommand("That Sam-I-am!"), new DbCommandInterceptionContext<int>().WithDbContext(context1));
                formatter.Executing(CreateCommand("I do not like"), new DbCommandInterceptionContext<int>().WithDbContext(context2));
                formatter.Executing(CreateCommand("that Sam-I-am"), new DbCommandInterceptionContext<int>());

                Assert.Equal("That Sam-I-am!", GetSingleLine(writer));
            }