• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 6134547577

10 Sep 2023 02:14AM UTC coverage: 50.788% (+0.4%) from 50.387%
6134547577

push

github

pwelter34
Update Script002.Tracker.Data.sql

937 of 2380 branches covered (0.0%)

Branch coverage included in aggregate %.

2736 of 4852 relevant lines covered (56.39%)

157.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.88
/src/FluentCommand/DataQueryLogger.cs
1
using System.Data;
2

3
using FluentCommand.Internal;
4

5
namespace FluentCommand;
6

7
/// <summary>
8
/// A class to log queries to string delegate
9
/// </summary>
10
/// <seealso cref="FluentCommand.IDataQueryLogger" />
11
public class DataQueryLogger : IDataQueryLogger
12
{
13
    private readonly Action<string> _logger;
14

15
    /// <summary>
16
    /// Initializes a new instance of the <see cref="DataQueryLogger"/> class.
17
    /// </summary>
18
    public DataQueryLogger() : this(null)
3✔
19
    {
20
    }
3✔
21

22
    /// <summary>
23
    /// Initializes a new instance of the <see cref="DataQueryLogger"/> class.
24
    /// </summary>
25
    /// <param name="logger">The logger.</param>
26
    public DataQueryLogger(Action<string> logger)
27
    {
28
        _logger = logger;
3✔
29
    }
3✔
30

31
    /// <summary>
32
    /// Log the current specified <paramref name="command" />
33
    /// </summary>
34
    /// <param name="command">The command to log.</param>
35
    /// <param name="duration">The execution duration.</param>
36
    /// <param name="exception">The exception thrown when executing the command.</param>
37
    /// <param name="state">The state used to control logging.</param>
38
    /// <exception cref="System.ArgumentNullException">command</exception>
39
    public virtual void LogCommand(IDbCommand command, TimeSpan duration, Exception exception = null, object state = null)
40
    {
41
        if (_logger == null)
×
42
            return;
×
43

44
        if (command is null)
×
45
            throw new ArgumentNullException(nameof(command));
×
46

47
        var output = FormatCommand(command, duration, exception);
×
48

49
        _logger(output);
×
50
    }
×
51

52
    /// <summary>
53
    /// Formats the command for logging.
54
    /// </summary>
55
    /// <param name="command">The command to log.</param>
56
    /// <param name="duration">The execution duration.</param>
57
    /// <param name="exception">The exception thrown when executing the command.</param>
58
    /// <returns>The command formatted as a string</returns>
59
    /// <exception cref="System.ArgumentNullException">command</exception>
60
    public static string FormatCommand(IDbCommand command, TimeSpan duration, Exception exception)
61
    {
62
        if (command == null)
136!
63
            throw new ArgumentNullException(nameof(command));
×
64

65
        var elapsed = duration.TotalMilliseconds;
136✔
66
        var commandType = command.CommandType;
136✔
67
        var commandTimeout = command.CommandTimeout;
136✔
68
        var resultText = exception == null ? "Executed" : "Error Executing";
136✔
69

70
        var buffer = StringBuilderCache.Acquire();
136✔
71

72
        buffer
136✔
73
            .Append(resultText)
136✔
74
            .Append(" DbCommand (")
136✔
75
            .Append(elapsed)
136✔
76
            .Append("ms) [CommandType='")
136✔
77
            .Append(commandType)
136✔
78
            .Append("', CommandTimeout='")
136✔
79
            .Append(commandTimeout)
136✔
80
            .Append("']")
136✔
81
            .AppendLine()
136✔
82
            .AppendLine(command.CommandText);
136✔
83

84
        foreach (IDataParameter parameter in command.Parameters)
834✔
85
        {
86
            int precision = 0;
281✔
87
            int scale = 0;
281✔
88
            int size = 0;
281✔
89

90
            if (parameter is IDbDataParameter dataParameter)
281✔
91
            {
92
                precision = dataParameter.Precision;
281✔
93
                scale = dataParameter.Scale;
281✔
94
                size = dataParameter.Size;
281✔
95
            }
96

97
            buffer
281✔
98
                .Append("-- ")
281✔
99
                .Append(parameter.ParameterName)
281✔
100
                .Append(": ")
281✔
101
                .Append(parameter.Direction)
281✔
102
                .Append(" ")
281✔
103
                .Append(parameter.DbType)
281✔
104
                .Append("(Size=")
281✔
105
                .Append(size)
281✔
106
                .Append("; Precision=")
281✔
107
                .Append(precision)
281✔
108
                .Append("; Scale=")
281✔
109
                .Append(scale)
281✔
110
                .Append(") [")
281✔
111
                .Append(parameter.Value)
281✔
112
                .Append("]")
281✔
113
                .AppendLine();
281✔
114
        }
115

116
        var output = StringBuilderCache.ToString(buffer);
136✔
117
        return output;
136✔
118
    }
119
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc