Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging API #396

Merged
merged 4 commits into from
Dec 1, 2017
Merged

Add logging API #396

merged 4 commits into from
Dec 1, 2017

Conversation

bgrainger
Copy link
Member

Fixes #390.

public static void Error(this IMySqlConnectorLogger log, string message, params object[] args) => log.Log(MySqlConnectorLogLevel.Error, message, args, null);
public static void Fatal(this IMySqlConnectorLogger log, string message, params object[] args) => log.Log(MySqlConnectorLogLevel.Fatal, message, args, null);

public static void Trace(this IMySqlConnectorLogger log, string message, Exception exception) => log.Log(MySqlConnectorLogLevel.Trace, message, null, exception);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put the exception before the message so that it can't be confused with the arguments. Or just require that callers use IMySqlConnectorLogger.Log directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I should have done this.

sb.Append(m_name);
sb.Append('\t');

if (args == null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use Append instead of AppendFormat if args.Length == 0.

if (args == null)
sb.Append(message);
else
sb.AppendFormat(message, args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use CultureInfo.InvariantCulture here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If only to demonstrate how to correctly write a logging implementation, sure.


var sb = new StringBuilder();
sb.Append(s_levels[(int) level]);
sb.Append('\t');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just a space? Tabs inconsistently waste horizontal space on a console.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a "production quality" logger; it's a sample implementation that can be used for testing. To get console output formatted the way you would like, hook IMySqlConnectorLogger up to log4net and use a ConsoleAppender (sample code/NuGet package for that hopefully coming soon).

Why not just a space?

I prefer tabs in my console output; keeps the columns lined up.

@@ -326,6 +359,14 @@ private ConnectionPool(ConnectionSettings cs)
cs.LoadBalance == MySqlLoadBalance.Random ? RandomLoadBalancer.Instance :
cs.LoadBalance == MySqlLoadBalance.FewestConnections ? new FewestConnectionsLoadBalancer(this) :
(ILoadBalancer) new RoundRobinLoadBalancer();

Id = Interlocked.Increment(ref s_poolId);
m_logArguments = new object[] { "Pool{0} ".FormatInvariant(Id) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd find the log statements more readable if you omitted the trailing space here and included them after {0} elsewhere.

@@ -350,6 +391,7 @@ public IEnumerable<string> LoadBalance(IReadOnlyList<string> hosts)
readonly ConnectionPool m_pool;
}

static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger("ConnectionPool");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameof(ConnectionPool)?

@@ -44,10 +52,12 @@ internal sealed class ConnectionPool
}
if (session != null)
{
Log.Debug("{0}Found an existing session; checking it for validity", m_logArguments);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent capitalization of first word (unless there's meaning I'm missing).

@@ -37,10 +38,10 @@ public void Log(MySqlConnectorLogLevel level, string message, object[] args = nu
sb.Append(m_name);
sb.Append('\t');

if (args == null)
if (args == null || args.Length == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably fix the docs as well.

#if NET45 || NET46
public IMySqlConnectorLogger CreateLogger(string name) => new Log4netLogger(LogManager.GetLogger(typeof(Log4netLogger).Assembly, "MySqlConnector." + name));
#else
public IMySqlConnectorLogger CreateLogger(string name) => new Log4netLogger(LogManager.GetLogger(typeof(Log4netLogger).GetTypeInfo().Assembly, "MySqlConnector." + name));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not work on NET45 || NET46? Or is this an optimization?

Would it be worth caching the assembly in a static?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; that code path works for both. I'll add a static.

@bgrainger
Copy link
Member Author

I ran the Benchmark suite before and after; didn't notice any significant increase in time or allocations from these changes.

@bgrainger bgrainger merged commit 1aa9a7e into mysql-net:master Dec 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants