Skip to content

Commit

Permalink
Merge pull request #1268 from mysql-net/logging. Fixes #1110
Browse files Browse the repository at this point in the history
Switch to Microsoft.Extensions.Logging.
  • Loading branch information
bgrainger authored Dec 28, 2022
2 parents 12c64ab + 9f03ec7 commit 052af74
Show file tree
Hide file tree
Showing 23 changed files with 967 additions and 335 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void Log(MySqlConnectorLogLevel level, string message, object?[]? args =
_ => throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'."),
};

private static readonly Func<string, Exception, string> s_getMessage = static (s, e) => s;
private static readonly Func<(string Message, object?[] Args), Exception, string> s_messageFormatter = static (s, e) => string.Format(CultureInfo.InvariantCulture, s.Message, s.Args);
private static readonly Func<string, Exception?, string> s_getMessage = static (s, e) => s;
private static readonly Func<(string Message, object?[] Args), Exception?, string> s_messageFormatter = static (s, e) => string.Format(CultureInfo.InvariantCulture, s.Message, s.Args);

private readonly ILogger m_logger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions src/MySqlConnector/Core/CachedProcedure.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using MySqlConnector.Logging;
using MySqlConnector.Protocol.Serialization;

namespace MySqlConnector.Core;

internal sealed class CachedProcedure
{
public static async Task<CachedProcedure?> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, CancellationToken cancellationToken)
public static async Task<CachedProcedure?> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, ILogger logger, CancellationToken cancellationToken)
{
// try to use mysql.proc first, as it is much faster
if (connection.Session.ServerVersion.Version < ServerVersions.RemovesMySqlProcTable && !connection.Session.ProcAccessDenied)
Expand Down Expand Up @@ -44,15 +45,15 @@ internal sealed class CachedProcedure
}
catch (MySqlException ex)
{
Log.Info("Session{0} failed to retrieve metadata for Schema={1} Component={2}; falling back to INFORMATION_SCHEMA. Error: {3}", connection.Session.Id, schema, component, ex.Message);
Log.FailedToRetrieveProcedureMetadata(logger, ex, connection.Session.Id, schema, component, ex.Message);
if (ex.ErrorCode == MySqlErrorCode.TableAccessDenied)
connection.Session.ProcAccessDenied = true;
}
}

if (connection.Session.ServerVersion.Version < ServerVersions.SupportsProcedureCache)
{
Log.Info("Session{0} ServerVersion={1} does not support cached procedures", connection.Session.Id, connection.Session.ServerVersion.OriginalString);
Log.ServerDoesNotSupportCachedProcedures(logger, connection.Session.Id, connection.Session.ServerVersion.OriginalString);
return null;
}

Expand Down Expand Up @@ -90,8 +91,7 @@ FROM information_schema.parameters
}
}

if (Log.IsTraceEnabled())
Log.Trace("Procedure for Schema={0} Component={1} has RoutineCount={2}, ParameterCount={3}", schema, component, routineCount, parameters.Count);
Log.ProcedureHasRoutineCount(logger, schema, component, routineCount, parameters.Count);
return routineCount == 0 ? null : new CachedProcedure(schema, component, parameters);
}

Expand Down Expand Up @@ -231,7 +231,6 @@ private static CachedParameter CreateCachedParameter(int ordinal, string? direct

private string FullyQualified => $"`{m_schema}`.`{m_component}`";

private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(CachedProcedure));
private static readonly IReadOnlyDictionary<string, string> s_typeMapping = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{ "BOOL", "TINYINT" },
Expand Down
7 changes: 2 additions & 5 deletions src/MySqlConnector/Core/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq
// pre-requisite: Connection is non-null must be checked before calling this method
var connection = command.Connection!;

if (Log.IsTraceEnabled())
Log.Trace("Session{0} ExecuteReader {1} CommandCount: {2}", connection.Session.Id, ioBehavior, commands.Count);
Log.CommandExecutorExecuteReader(command.Logger, connection.Session.Id, ioBehavior, commands.Count);

Dictionary<string, CachedProcedure?>? cachedProcedures = null;
foreach (var command2 in commands)
Expand Down Expand Up @@ -57,7 +56,7 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq
}
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.QueryInterrupted && cancellationToken.IsCancellationRequested)
{
Log.Info("Session{0} query was interrupted", connection.Session.Id);
Log.QueryWasInterrupted(command.Logger, connection.Session.Id);
throw new OperationCanceledException(ex.Message, ex, cancellationToken);
}
catch (Exception ex) when (payload.Span.Length > 4_194_304 && (ex is SocketException or IOException or MySqlProtocolException))
Expand All @@ -75,6 +74,4 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq
throw;
}
}

private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(CommandExecutor));
}
5 changes: 1 addition & 4 deletions src/MySqlConnector/Core/ConcatenatedCommandPayloadCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
do
{
var command = commandListPosition.Commands[commandListPosition.CommandIndex];
if (Log.IsTraceEnabled())
Log.Trace("Session{0} Preparing command payload; CommandText: {1}", command.Connection!.Session.Id, command.CommandText);
Log.PreparingCommandPayload(command.Logger, command.Connection!.Session.Id, command.CommandText!);

isComplete = SingleCommandPayloadCreator.WriteQueryPayload(command, cachedProcedures, writer, commandListPosition.CommandIndex < commandListPosition.Commands.Count - 1 || appendSemicolon);
commandListPosition.CommandIndex++;
Expand All @@ -40,6 +39,4 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict

return true;
}

private static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(ConcatenatedCommandPayloadCreator));
}
Loading

0 comments on commit 052af74

Please sign in to comment.