Skip to content

Commit 2eb2e0d

Browse files
committed
Add Microsoft.Extensions.Logging provider. Fixes #418
1 parent 2ed4b81 commit 2eb2e0d

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

MySqlConnector.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27004.2010
3+
VisualStudioVersion = 15.0.27130.2010
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector", "src\MySqlConnector\MySqlConnector.csproj", "{F82378AF-274E-4FBA-8E45-27126D607B85}"
66
EndProject
@@ -14,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Conformance.Tests", "tests\
1414
EndProject
1515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.Logging.log4net", "src\MySqlConnector.Logging.log4net\MySqlConnector.Logging.log4net.csproj", "{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}"
1616
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.Logging.Microsoft.Extensions.Logging", "src\MySqlConnector.Logging.Microsoft.Extensions.Logging\MySqlConnector.Logging.Microsoft.Extensions.Logging.csproj", "{6A3F1732-F874-463E-9BB8-21690E7B8ED0}"
18+
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1921
Debug|Any CPU = Debug|Any CPU
@@ -44,6 +46,10 @@ Global
4446
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Debug|Any CPU.Build.0 = Debug|Any CPU
4547
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Release|Any CPU.ActiveCfg = Release|Any CPU
4648
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Release|Any CPU.Build.0 = Release|Any CPU
49+
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
50+
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
51+
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
52+
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Release|Any CPU.Build.0 = Release|Any CPU
4753
EndGlobalSection
4854
GlobalSection(SolutionProperties) = preSolution
4955
HideSolutionNode = FALSE

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ after_test:
1313
- cmd: |-
1414
dotnet pack src\MySqlConnector\MySqlConnector.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
1515
dotnet pack src\MySqlConnector.Logging.log4net\MySqlConnector.Logging.log4net.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
16+
dotnet pack src\MySqlConnector.Logging.Microsoft.Extensions.Logging\MySqlConnector.Logging.Microsoft.Extensions.Logging.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
1617
notifications:
1718
- provider: Slack
1819
incoming_webhook:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.Globalization;
3+
using Microsoft.Extensions.Logging;
4+
5+
namespace MySqlConnector.Logging
6+
{
7+
public sealed class MicrosoftExtensionsLoggingLoggerProvider : IMySqlConnectorLoggerProvider
8+
{
9+
public MicrosoftExtensionsLoggingLoggerProvider(ILoggerFactory loggerFactory) => m_loggerFactory = loggerFactory;
10+
11+
public IMySqlConnectorLogger CreateLogger(string name) => new MicrosoftExtensionsLoggingLogger(m_loggerFactory.CreateLogger(name));
12+
13+
private class MicrosoftExtensionsLoggingLogger : IMySqlConnectorLogger
14+
{
15+
public MicrosoftExtensionsLoggingLogger(ILogger logger) => m_logger = logger;
16+
17+
public bool IsEnabled(MySqlConnectorLogLevel level) => m_logger.IsEnabled(GetLevel(level));
18+
19+
public void Log(MySqlConnectorLogLevel level, string message, object[] args = null, Exception exception = null)
20+
{
21+
if (args == null || args.Length == 0)
22+
m_logger.Log(GetLevel(level), 0, message, exception, s_getMessage);
23+
else
24+
m_logger.Log(GetLevel(level), 0, (message, args), exception, s_messageFormatter);
25+
}
26+
27+
private static LogLevel GetLevel(MySqlConnectorLogLevel level)
28+
{
29+
switch (level)
30+
{
31+
case MySqlConnectorLogLevel.Trace:
32+
return LogLevel.Trace;
33+
case MySqlConnectorLogLevel.Debug:
34+
return LogLevel.Debug;
35+
case MySqlConnectorLogLevel.Info:
36+
return LogLevel.Information;
37+
case MySqlConnectorLogLevel.Warn:
38+
return LogLevel.Warning;
39+
case MySqlConnectorLogLevel.Error:
40+
return LogLevel.Error;
41+
case MySqlConnectorLogLevel.Fatal:
42+
return LogLevel.Critical;
43+
default:
44+
throw new ArgumentOutOfRangeException(nameof(level), level, "Invalid value for 'level'.");
45+
}
46+
}
47+
48+
static readonly Func<string, Exception, string> s_getMessage = (s, e) => s;
49+
static readonly Func<(string Message, object[] Args), Exception, string> s_messageFormatter = (s, e) => string.Format(CultureInfo.InvariantCulture, s.Message, s.Args);
50+
51+
readonly ILogger m_logger;
52+
}
53+
54+
readonly ILoggerFactory m_loggerFactory;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5+
<Title>MySqlConnector Logging Adapter for Microsoft.Extensions.Logging</Title>
6+
<Description>Writes MySqlConnector logging output to Microsoft.Extensions.Logging with one line of code.</Description>
7+
<Copyright>Copyright 2017 Bradley Grainger</Copyright>
8+
<Authors>Bradley Grainger</Authors>
9+
<PackageTags>mysql;mysqlconnector;async;ado.net;database;netcore;logging</PackageTags>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\MySqlConnector\MySqlConnector.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)