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

loresoft / FluentCommand / 27284007534

10 Jun 2026 02:31PM UTC coverage: 65.051%. First build
27284007534

push

github

pwelter34
Capture and trim SQL Server PRINT/log messages

1737 of 3466 branches covered (50.12%)

Branch coverage included in aggregate %.

27 of 32 new or added lines in 3 files covered. (84.38%)

5535 of 7713 relevant lines covered (71.76%)

296.48 hits per line

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

68.42
/src/FluentCommand.SqlServer/MessageInterceptor.cs
1
using System.Data.Common;
2

3
using FluentCommand.Extensions;
4

5
using Microsoft.Data.SqlClient;
6
using Microsoft.Extensions.Logging;
7

8
namespace FluentCommand;
9

10
/// <summary>
11
/// Captures SQL Server informational messages such as PRINT output.
12
/// </summary>
13
/// <seealso cref="IDataConnectionInterceptor" />
14
public partial class MessageInterceptor : IDataConnectionInterceptor
15
{
16
    /// <summary>
17
    /// The default maximum length of the rendered SQL Server message.
18
    /// </summary>
19
    public const int DefaultMaxMessageLength = 1024;
20

21
    private const string FullMessagePropertyName = "FullMessage";
22

23
    /// <summary>
24
    /// Initializes a new instance of the <see cref="MessageInterceptor"/> class.
25
    /// </summary>
26
    /// <param name="logger">The logger.</param>
27
    public MessageInterceptor(ILogger<MessageInterceptor> logger)
28
        : this(logger, DefaultMaxMessageLength)
1✔
29
    {
30
    }
1✔
31

32
    /// <summary>
33
    /// Initializes a new instance of the <see cref="MessageInterceptor"/> class.
34
    /// </summary>
35
    /// <param name="logger">The logger.</param>
36
    /// <param name="maxMessageLength">The maximum length of the rendered SQL Server message.</param>
37
    public MessageInterceptor(ILogger<MessageInterceptor> logger, int maxMessageLength)
2✔
38
    {
39
        if (maxMessageLength <= 0)
2!
NEW
40
            throw new ArgumentOutOfRangeException(nameof(maxMessageLength), maxMessageLength, "The maximum message length must be greater than zero.");
×
41

42
        Logger = logger ?? throw new ArgumentNullException(nameof(logger));
2!
43
        MaxMessageLength = maxMessageLength;
2✔
44
    }
2✔
45

46
    /// <summary>
47
    /// Gets the logger.
48
    /// </summary>
49
    protected ILogger Logger { get; }
50

51
    /// <summary>
52
    /// Gets the maximum length of the rendered SQL Server message.
53
    /// </summary>
54
    protected int MaxMessageLength { get; }
55

56
    /// <inheritdoc />
57
    public virtual void ConnectionOpened(DbConnection connection, IDataSession session)
58
    {
59
        if (connection is SqlConnection sqlConnection)
2!
60
            sqlConnection.InfoMessage += OnInfoMessage;
2✔
61
    }
2✔
62

63
    /// <inheritdoc />
64
    public virtual Task ConnectionOpenedAsync(DbConnection connection, IDataSession session, CancellationToken cancellationToken = default)
65
    {
66
        ConnectionOpened(connection, session);
×
67
        return Task.CompletedTask;
×
68
    }
69

70
    /// <inheritdoc />
71
    public virtual void ConnectionClosing(DbConnection connection, IDataSession session)
72
    {
73
        if (connection is SqlConnection sqlConnection)
2!
74
            sqlConnection.InfoMessage -= OnInfoMessage;
2✔
75
    }
2✔
76

77
    /// <inheritdoc />
78
    public virtual Task ConnectionClosingAsync(DbConnection connection, IDataSession session, CancellationToken cancellationToken = default)
79
    {
80
        ConnectionClosing(connection, session);
×
81
        return Task.CompletedTask;
×
82
    }
83

84
    /// <summary>
85
    /// Handles SQL Server informational messages.
86
    /// </summary>
87
    /// <param name="sender">The event source.</param>
88
    /// <param name="e">The SQL Server message event data.</param>
89
    protected virtual void OnInfoMessage(object sender, SqlInfoMessageEventArgs e)
90
    {
91
        if (e is null)
2!
92
            return;
×
93

94
        var fullMessage = e.Message ?? string.Empty;
2!
95
        var message = fullMessage.Truncate(MaxMessageLength);
2✔
96

97
        var scope = new[] { new KeyValuePair<string, object?>(FullMessagePropertyName, fullMessage) };
2✔
98

99
        using (Logger.BeginScope(scope))
2✔
100
            LogInfoMessage(Logger, message);
2✔
101
    }
2✔
102

103
    [LoggerMessage(0, LogLevel.Information, "SQL Server Message: {Message}")]
104
    protected static partial void LogInfoMessage(ILogger logger, string message);
105
}
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