Skip to content

Commit

Permalink
Add Microsoft.Extensions.Logging provider. Fixes #418
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Jan 1, 2018
1 parent 2ed4b81 commit 2eb2e0d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
8 changes: 7 additions & 1 deletion MySqlConnector.sln
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2010
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector", "src\MySqlConnector\MySqlConnector.csproj", "{F82378AF-274E-4FBA-8E45-27126D607B85}"
EndProject
Expand All @@ -14,6 +14,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Conformance.Tests", "tests\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MySqlConnector.Logging.log4net", "src\MySqlConnector.Logging.log4net\MySqlConnector.Logging.log4net.csproj", "{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -44,6 +46,10 @@ Global
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A15647B8-FA3F-4536-BF4E-4F93F2FBFC75}.Release|Any CPU.Build.0 = Release|Any CPU
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A3F1732-F874-463E-9BB8-21690E7B8ED0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Expand Up @@ -13,6 +13,7 @@ after_test:
- cmd: |-
dotnet pack src\MySqlConnector\MySqlConnector.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
dotnet pack src\MySqlConnector.Logging.log4net\MySqlConnector.Logging.log4net.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
dotnet pack src\MySqlConnector.Logging.Microsoft.Extensions.Logging\MySqlConnector.Logging.Microsoft.Extensions.Logging.csproj -c Release -p:SourceLinkCreate=true -p:SourceLinkTest=true
notifications:
- provider: Slack
incoming_webhook:
Expand Down
@@ -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;
}
}
@@ -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.

Copy link
@ejball

ejball Apr 12, 2018

Contributor

Could this just be Microsoft.Extensions.Logging.Abstractions?

This comment has been minimized.

Copy link
@bgrainger

bgrainger Apr 12, 2018

Author Member

Yes, it could. Thanks!

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MySqlConnector\MySqlConnector.csproj" />
</ItemGroup>

</Project>

0 comments on commit 2eb2e0d

Please sign in to comment.