|
1 | 1 | # Logging |
2 | 2 |
|
3 | | -Npgsql includes a built-in feature for outputting logging events which can help debug issues. |
| 3 | +Npgsql uses Microsoft.Extensions.Logging to emit log events that can be routed to your logging library of choice - see [their docs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging) for more information. Note that Microsoft.Extensions.Logging support replaced Npgsql's own custom logging wrapper in 3.2. |
4 | 4 |
|
5 | | -Npgsql logging is disabled by default and must be turned on. Logging can be turned on by setting `NpgsqlLogManager.Provider` to a class implementing the `INpgsqlLoggingProvider` interface. Npgsql comes with a console implementation which can be set up as follows: |
| 5 | +Since Npgsql is an ADO.NET provider and does not support dependency injection, you must manually inject your ILoggingFactory by including the following code *before* starting to use any of Npgsql's other APIs: |
6 | 6 |
|
7 | 7 | ```c# |
8 | | -NpgsqlLogManager.Provider = new ??? |
| 8 | +NpgsqlLogManager.Provider = new LoggerFactory |
| 9 | + .AddConsole((text, logLevel) => logLevel >= LogLevel.Debug); |
9 | 10 | ``` |
10 | 11 |
|
11 | | -*Note: you must set the logging provider before invoking any other Npgsql method, at the very start of your program.* |
12 | | - |
13 | | -It's trivial to create a logging provider that passes log messages to whatever logging framework you use. You can find such an adaptor for NLog [here](http://ni). |
14 | | - |
15 | | -*Note:* the logging API is a first implementation and will probably improve/change - don't treat it as a stable part of the Npgsql API. Let us know if you think there are any missing messages or features! |
16 | | - |
17 | | -## ConsoleLoggingProvider |
18 | | - |
19 | | -Npgsql comes with one built-in logging provider: ConsoleLoggingProvider. It will simply dump all log messages with a given level or above to stdanrd output. |
20 | | -You can set it up by including the following line at the beginning of your application: |
21 | | - |
22 | | -```c# |
23 | | -NpgsqlLogManager.Provider = new ConsoleLoggingProvider(<min level>, <print level?>, <print connector id?>); |
24 | | -``` |
25 | | - |
26 | | -Level defaults to `NpgsqlLogLevel.Info` (which will only print warnings and errors). |
27 | | -You can also have log levels and connector IDs logged. |
| 12 | +This will make Npgsql log to the console, for messages of at least Debug level. Other providers exist for NLog and other logging packages and services. |
28 | 13 |
|
29 | 14 | ## Statement and Parameter Logging |
30 | 15 |
|
31 | 16 | Npgsql will log all SQL statements at level Debug, this can help you debug exactly what's being sent to PostgreSQL. |
32 | 17 |
|
33 | 18 | By default, Npgsql will not log parameter values as these may contain sensitive information. You can turn on |
34 | 19 | parameter logging by setting `NpgsqlLogManager.IsParameterLoggingEnabled` to true. |
35 | | - |
36 | | -## NLogLoggingProvider (or implementing your own) |
37 | | - |
38 | | -The following provider is used in the Npgsql unit tests to pass log messages to [NLog](http://nlog-project.org/). |
39 | | -You're welcome to copy-paste it into your project, or to use it as a starting point for implementing your own custom provider. |
40 | | - |
41 | | -```c# |
42 | | -class NLogLoggingProvider : INpgsqlLoggingProvider |
43 | | -{ |
44 | | - public NpgsqlLogger CreateLogger(string name) |
45 | | - { |
46 | | - return new NLogLogger(name); |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -class NLogLogger : NpgsqlLogger |
51 | | -{ |
52 | | - readonly Logger _log; |
53 | | - |
54 | | - internal NLogLogger(string name) |
55 | | - { |
56 | | - _log = LogManager.GetLogger(name); |
57 | | - } |
58 | | - |
59 | | - public override bool IsEnabled(NpgsqlLogLevel level) |
60 | | - { |
61 | | - return _log.IsEnabled(ToNLogLogLevel(level)); |
62 | | - } |
63 | | - |
64 | | - public override void Log(NpgsqlLogLevel level, int connectorId, string msg, Exception exception = null) |
65 | | - { |
66 | | - var ev = new LogEventInfo(ToNLogLogLevel(level), "", msg); |
67 | | - if (exception != null) |
68 | | - ev.Exception = exception; |
69 | | - if (connectorId != 0) |
70 | | - ev.Properties["ConnectorId"] = connectorId; |
71 | | - _log.Log(ev); |
72 | | - } |
73 | | - |
74 | | - static LogLevel ToNLogLogLevel(NpgsqlLogLevel level) |
75 | | - { |
76 | | - switch (level) |
77 | | - { |
78 | | - case NpgsqlLogLevel.Trace: |
79 | | - return LogLevel.Trace; |
80 | | - case NpgsqlLogLevel.Debug: |
81 | | - return LogLevel.Debug; |
82 | | - case NpgsqlLogLevel.Info: |
83 | | - return LogLevel.Info; |
84 | | - case NpgsqlLogLevel.Warn: |
85 | | - return LogLevel.Warn; |
86 | | - case NpgsqlLogLevel.Error: |
87 | | - return LogLevel.Error; |
88 | | - case NpgsqlLogLevel.Fatal: |
89 | | - return LogLevel.Fatal; |
90 | | - default: |
91 | | - throw new ArgumentOutOfRangeException("level"); |
92 | | - } |
93 | | - } |
94 | | -} |
95 | | -``` |
0 commit comments