Skip to content

Commit 55d1585

Browse files
committed
Add helper method for adding logging.
1 parent d1616ce commit 55d1585

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/UsingMicrosoftExtensionsLogging.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
## How to Use
22

3-
To integrate MySqlConnector with Microsoft.Extensions.Logging, add the following line of code to your `Startup.Configure` method (before any `MySqlConnector` objects have been used):
3+
To integrate MySqlConnector with Microsoft.Extensions.Logging, add the following line of code to `Program.cs` method (before any `MySqlConnector` objects have been used):
4+
5+
```csharp
6+
app.Services.UseMySqlConnectorLogging();
7+
```
8+
9+
Alternatively, obtain an `ILoggerFactory` through dependency injection and add:
410

511
```csharp
612
MySqlConnectorLogManager.Provider = new MicrosoftExtensionsLoggingLoggerProvider(loggerFactory);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
using MySqlConnector.Logging;
4+
5+
namespace Microsoft.Extensions.DependencyInjection;
6+
7+
public static class MySqlConnectorLoggingExtensions
8+
{
9+
public static IServiceProvider UseMySqlConnectorLogging(this IServiceProvider services)
10+
{
11+
var loggerFactory = (ILoggerFactory) services.GetService(typeof(ILoggerFactory));
12+
if (loggerFactory is null)
13+
throw new InvalidOperationException("No ILoggerFactory service has been registered.");
14+
MySqlConnectorLogManager.Provider = new MicrosoftExtensionsLoggingLoggerProvider(loggerFactory);
15+
return services;
16+
}
17+
}

src/MySqlConnector.Logging.Microsoft.Extensions.Logging/docs/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ This package integrates MySqlConnector logging with the Microsoft.Extensions.Log
44

55
## How to Use
66

7-
Add the following line of code to your `Startup.Configure` method (before any `MySqlConnector` objects have been used):
7+
Add the following line of code to `Program.cs` method (before any `MySqlConnector` objects have been used):
8+
9+
```csharp
10+
app.Services.UseMySqlConnectorLogging();
11+
```
12+
13+
Alternatively, obtain an `ILoggerFactory` through dependency injection and add:
814

915
```csharp
1016
MySqlConnectorLogManager.Provider = new MicrosoftExtensionsLoggingLoggerProvider(loggerFactory);

0 commit comments

Comments
 (0)