Skip to content

Commit 0602473

Browse files
committed
Update logging documentation with migration instructions.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent cfd95c0 commit 0602473

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

docs/content/diagnostics/logging.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,67 @@ This allows your endpoints to get injected with MySQL connections which log to t
5454

5555
## Global Logging
5656

57+
### Migrating from Global Logging
58+
59+
<blockquote class="highlight">
60+
In MySqlConnector 2.4.0, the <tt>MySqlConnectorLogManager.Provider</tt> API has been deprecated.
61+
Follow these instructions to migrate to the new logging API.
62+
</blockquote>
63+
64+
With the deprecated logging framework, you may have had code that looked like this:
65+
66+
```csharp
67+
log4net.Config.XmlConfigurator.Configure();
68+
MySqlConnectorLogManager.Provider = new Log4netLoggerProvider();
69+
using var connection = new MySqlConnection(connectionString);
70+
```
71+
72+
To migrate to the new logging API, you will need to:
73+
74+
* Create a `LoggerFactory`
75+
* Connect that `LoggerFactory` to your desired logging framework
76+
* Create a `MySqlDataSource` configurated with that `LoggerFactory`
77+
* Create new `MySqlConnection` objects using `MySqlDataSource.CreateConnection()` (or `OpenConnection[Async]()`).
78+
79+
This will look like the following, depending on your exact configuration:
80+
81+
```csharp
82+
// create a LoggerFactory and configure it with the desired logging framework
83+
// use ONLY ONE of the "Add" methods below, depending on your logging framework
84+
var loggerFactory = LoggerFactory.Create(builder =>
85+
// if you just want console logging
86+
builder.AddConsole();
87+
88+
// connect to log4net via Microsoft.Extensions.Logging.Log4Net.AspNetCore
89+
builder.AddLog4Net(new Log4NetProviderOptions
90+
{
91+
UseWebOrAppConfig = true, // set this if you're storing your settings in Web.config instead of log4net.config
92+
ExternalConfigurationSetup = true, // set this instead if you're initializing log4net yourself
93+
// see other options at https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore
94+
}));
95+
96+
// connect to NLog via NLog.Extensions.Logging
97+
builder.AddNLog();
98+
99+
// connect to Serilog via Serilog.Extensions.Logging
100+
builder.AddSerilog(dispose: true);
101+
);
102+
103+
// now create a MySqlDataSource and configure it with the LoggerFactory
104+
using var dataSource = new MySqlDataSourceBuilder(yourConnectionString)
105+
.UseLoggerFactory(loggerFactory)
106+
.Build();
107+
108+
// create all MySqlConnection objects via the MySqlDataSource, not directly
109+
// DON'T: using var connection = new MySqlConnection(yourConnectionString);
110+
using var connection = dataSource.CreateConnection();
111+
112+
// you can also create open connections
113+
using var connection = await dataSource.OpenConnectionAsync();
114+
```
115+
116+
### Deprecated Logging Framework
117+
57118
<blockquote class="warning">
58119
The following information is for MySqlConnector versions prior to 2.3.0.
59120
The global logging interface that is described is deprecated and shouldn't be used in new code.

0 commit comments

Comments
 (0)