Skip to content

Commit b4238ce

Browse files
committed
Update logging documentation for 2.3.0.
1 parent f1a728a commit b4238ce

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

docs/content/diagnostics/logging.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,64 @@ title: Logging
88
weight: 10
99
---
1010

11-
Logging
12-
=======
11+
# Logging
12+
13+
MySqlConnector 2.3.0 and later supports logging through the standard [Microsoft.Extensions.Logging](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging) interfaces.
14+
15+
## Console Programs
16+
17+
To set up logging in MySqlConnector, create your `ILoggerFactory` as usual, and then configure a `MySqlDataSource` with it.
18+
Any use of connections handed out by the data source will log via your provided logger factory.
19+
20+
The following shows a minimal console application logging to the console via [Microsoft.Extensions.Logging.Console](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console):
21+
22+
```csharp
23+
using Microsoft.Extensions.Logging;
24+
using MySqlConnector;
25+
26+
// Create a Microsoft.Extensions.Logging LoggerFactory, configuring it with the providers,
27+
// log levels and other desired configuration.
28+
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
29+
30+
// Create a MySqlDataSourceBuilder, configuring it with our LoggerFactory.
31+
var dataSourceBuilder = new MySqlDataSourceBuilder("Host=localhost;User ID=root;Password=pass");
32+
dataSourceBuilder.UseLoggerFactory(loggerFactory);
33+
await using var dataSource = dataSourceBuilder.Build();
34+
35+
// Any connections handed out by the data source will log via the LoggerFactory:
36+
await using var connection = await dataSource.OpenConnectionAsync();
37+
await using var command = new MySqlCommand("SELECT 1", connection);
38+
_ = await command.ExecuteScalarAsync();
39+
```
40+
41+
## ASP.NET and Dependency Injection
42+
43+
If you're using ASP.NET, you can use the additional [MySqlConnector.DependencyInjection package](https://www.nuget.org/packages/MySqlConnector.DependencyInjection), which provides seamless integration with dependency injection and logging:
44+
45+
```csharp
46+
var builder = WebApplication.CreateBuilder(args);
47+
builder.Logging.AddConsole();
48+
builder.Services.AddMySqlDataSource("Host=localhost;User ID=root;Password=pass");
49+
```
50+
51+
The `AddMySqlDataSource` method registers a data source with the DI container.
52+
This data source automatically uses the logger factory configured by ASP.NET by default.
53+
This allows your endpoints to get injected with MySQL connections which log to the application's logger factory.
54+
55+
## Global Logging
56+
57+
<blockquote class="warning">
58+
The following information is for MySqlConnector versions prior to 2.3.0.
59+
The global logging interface that is described is deprecated and shouldn't be used in new code.
60+
</blockquote>
1361

1462
MySqlConnector implements a custom logging framework with the `IMySqlConnectorLogger` and `IMySqlConnectorLoggerProvider` interfaces.
1563
To connect MySqlConnector to an existing logging framework, write an implementation of `IMySqlConnectorLoggerProvider` that adapts
1664
the existing logging framework, and install it by setting `MySqlConnector.Logging.MySqlConnectorLogManager.Provider = provider;`.
1765

1866
The `MySqlConnectorLogManager.Provider` property may only be set once, and must be set before any other MySqlConnector library methods are called.
1967

20-
Debug-level logging is useful for diagnosing problems with MySqlConnector itself; it is recommended that applications limit MySqlConnector
68+
Trace-level logging is useful for diagnosing problems with MySqlConnector itself; it is recommended that applications limit MySqlConnector
2169
logging to Info or higher.
2270

2371
### Existing Logging Providers

0 commit comments

Comments
 (0)