protected override void UnsafeLog <TState>(
                LogLevel logLevel, EventId eventId, string message, TState state, Exception exception)
            {
                if ((eventId.Id == RelationalEventId.CommandExecuted.Id ||
                     eventId.Id == RelationalEventId.CommandError.Id ||
                     eventId.Id == RelationalEventId.CommandExecuting.Id))
                {
                    if (_shouldLogCommands)
                    {
                        base.UnsafeLog(logLevel, eventId, message, state, exception);
                    }

                    if (!IsRecordingSuspended &&
                        message != null &&
                        eventId.Id != RelationalEventId.CommandExecuting.Id)
                    {
                        var structure = (IReadOnlyList <KeyValuePair <string, object> >)state;

                        var parameters  = structure.Where(i => i.Key == "parameters").Select(i => (string)i.Value).First();
                        var commandText = structure.Where(i => i.Key == "commandText").Select(i => (string)i.Value).First();

                        if (!string.IsNullOrWhiteSpace(parameters))
                        {
                            Parameters.Add(parameters);
                            parameters = parameters.Replace(", ", _eol) + _eol + _eol;
                        }

                        SqlStatements.Add(parameters + commandText);
                    }
                }
                else
                {
                    base.UnsafeLog(logLevel, eventId, message, state, exception);
                }
            }
            protected override void UnsafeLog <TState>(
                LogLevel logLevel, EventId eventId, string message, TState state, Exception exception)
            {
                if (eventId.Id == CosmosEventId.ExecutingSqlQuery)
                {
                    if (_shouldLogCommands)
                    {
                        base.UnsafeLog(logLevel, eventId, message, state, exception);
                    }

                    if (message != null)
                    {
                        var structure = (IReadOnlyList <KeyValuePair <string, object> >)state;

                        var parameters  = structure.Where(i => i.Key == "parameters").Select(i => (string)i.Value).First();
                        var commandText = structure.Where(i => i.Key == "commandText").Select(i => (string)i.Value).First();

                        if (!string.IsNullOrWhiteSpace(parameters))
                        {
                            Parameters.Add(parameters);
                            parameters = parameters.Replace(", ", _eol) + _eol + _eol;
                        }

                        SqlStatements.Add(parameters + commandText);
                    }
                }
                if (eventId.Id == CosmosEventId.ExecutingReadItem)
                {
                    if (_shouldLogCommands)
                    {
                        base.UnsafeLog(logLevel, eventId, message, state, exception);
                    }

                    if (message != null)
                    {
                        var structure = (IReadOnlyList <KeyValuePair <string, object> >)state;

                        var parameters = structure.Where(i => i.Key == "parameters").Select(i => (string)i.Value).First();

                        SqlStatements.Add($"ReadItem({parameters})");
                    }
                }
                else
                {
                    base.UnsafeLog(logLevel, eventId, message, state, exception);
                }
            }
            void ILogger.Log <TState>(
                LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
            {
                var format = formatter(state, exception)?.Trim();

                lock (_sync) // Guard against tests with explicit concurrency
                {
                    if (format != null)
                    {
                        if (_cancellationTokenSource != null)
                        {
                            _cancellationTokenSource.Cancel();
                            _cancellationTokenSource = null;
                        }

                        var commandLogData = state as DbCommandLogData;

                        if (commandLogData != null)
                        {
                            var parameters = "";

                            if (commandLogData.Parameters.Any())
                            {
                                parameters
                                    = string.Join(
                                          _newLine,
                                          commandLogData.Parameters
                                          .Select(p => $"{p.Name}: {p.FormatParameter(quoteValues: false)}"))
                                      + _newLine + _newLine;
                            }

                            SqlStatements.Add(parameters + commandLogData.CommandText);

                            LogData.Add(commandLogData);
                        }
                        else
                        {
                            LogBuilder.AppendLine(format);
                        }

                        TestOutputHelper?.WriteLine(format + Environment.NewLine);
                    }
                }
            }
            void ILogger.Log <TState>(
                LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
            {
                var format = formatter(state, exception)?.Trim();

                lock (_sync) // Guard against tests with explicit concurrency
                {
                    if (format != null)
                    {
                        if (_cancellationTokenSource != null)
                        {
                            _cancellationTokenSource.Cancel();
                            _cancellationTokenSource = null;
                        }

                        if (eventId.Id == RelationalEventId.CommandExecuted.Id ||
                            eventId.Id == RelationalEventId.CommandError.Id)
                        {
                            var structure = (IReadOnlyList <KeyValuePair <string, object> >)state;

                            var parameters  = structure.Where(i => i.Key == "parameters").Select(i => (string)i.Value).First();
                            var commandText = structure.Where(i => i.Key == "commandText").Select(i => (string)i.Value).First();

                            if (!string.IsNullOrWhiteSpace(parameters))
                            {
                                Parameters.Add(parameters);
                                parameters = parameters.Replace(", ", _newLine) + _newLine + _newLine;
                            }

                            SqlStatements.Add(parameters + commandText);
                        }
                        else
                        {
                            LogBuilder.AppendLine(format);
                        }

                        if (eventId.Id != RelationalEventId.CommandExecuted.Id)
                        {
                            TestOutputHelper?.WriteLine(format + Environment.NewLine);
                        }
                    }
                }
            }
Beispiel #5
0
            public void Log(
                LogLevel logLevel,
                int eventId,
                object state,
                Exception exception,
                Func <object, Exception, string> formatter)
            {
                var format = formatter(state, exception);

                if (eventId == RelationalLoggingEventIds.Sql)
                {
                    if (_cancellationTokenSource != null)
                    {
                        _cancellationTokenSource.Cancel();
                        _cancellationTokenSource = null;
                    }

                    SqlStatements.Add(format);
                }
                else
                {
                    _log.AppendLine(format);
                }
            }
Beispiel #6
0
 private void CaptureSql(string sql)
 {
     SqlStatements.Add(sql);
 }