Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Microsoft.Extensions.Logging provider. Fixes #418
- Loading branch information
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...onnector.Logging.Microsoft.Extensions.Logging/MicrosoftExtensionsLoggingLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Globalization; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MySqlConnector.Logging | ||
{ | ||
public sealed class MicrosoftExtensionsLoggingLoggerProvider : IMySqlConnectorLoggerProvider | ||
{ | ||
public MicrosoftExtensionsLoggingLoggerProvider(ILoggerFactory loggerFactory) => m_loggerFactory = loggerFactory; | ||
|
||
public IMySqlConnectorLogger CreateLogger(string name) => new MicrosoftExtensionsLoggingLogger(m_loggerFactory.CreateLogger(name)); | ||
|
||
private class MicrosoftExtensionsLoggingLogger : IMySqlConnectorLogger | ||
{ | ||
public MicrosoftExtensionsLoggingLogger(ILogger logger) => m_logger = logger; | ||
|
||
public bool IsEnabled(MySqlConnectorLogLevel level) => m_logger.IsEnabled(GetLevel(level)); | ||
|
||
public void Log(MySqlConnectorLogLevel level, string message, object[] args = null, Exception exception = null) | ||
{ | ||
if (args == null || args.Length == 0) | ||
m_logger.Log(GetLevel(level), 0, message, exception, s_getMessage); | ||
else | ||
m_logger.Log(GetLevel(level), 0, (message, args), exception, s_messageFormatter); | ||
} | ||
|
||
private static LogLevel GetLevel(MySqlConnectorLogLevel level) | ||
{ | ||
switch (level) | ||
{ | ||
case MySqlConnectorLogLevel.Trace: | ||
return LogLevel.Trace; | ||
case MySqlConnectorLogLevel.Debug: | ||
return LogLevel.Debug; | ||
case MySqlConnectorLogLevel.Info: | ||
return LogLevel.Information; | ||
case MySqlConnectorLogLevel.Warn: | ||
return LogLevel.Warning; | ||
case MySqlConnectorLogLevel.Error: | ||
return LogLevel.Error; | ||
case MySqlConnectorLogLevel.Fatal: | ||
return LogLevel.Critical; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'."); | ||
} | ||
} | ||
|
||
static readonly Func<string, Exception, string> s_getMessage = (s, e) => s; | ||
static readonly Func<(string Message, object[] Args), Exception, string> s_messageFormatter = (s, e) => string.Format(CultureInfo.InvariantCulture, s.Message, s.Args); | ||
|
||
readonly ILogger m_logger; | ||
} | ||
|
||
readonly ILoggerFactory m_loggerFactory; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...g.Microsoft.Extensions.Logging/MySqlConnector.Logging.Microsoft.Extensions.Logging.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
<Title>MySqlConnector Logging Adapter for Microsoft.Extensions.Logging</Title> | ||
<Description>Writes MySqlConnector logging output to Microsoft.Extensions.Logging with one line of code.</Description> | ||
<Copyright>Copyright 2017 Bradley Grainger</Copyright> | ||
<Authors>Bradley Grainger</Authors> | ||
<PackageTags>mysql;mysqlconnector;async;ado.net;database;netcore;logging</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" /> | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MySqlConnector\MySqlConnector.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Could this just be
Microsoft.Extensions.Logging.Abstractions
?