diff --git a/README.md b/README.md
index eb26cc61..eec01522 100644
--- a/README.md
+++ b/README.md
@@ -120,6 +120,29 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve your token's bot user
- [x] Search
+## Enable internal logs
+The library make use of `ILoggerFactory` interface exposed by `Microsoft.Extensions.Logging`. Which allow you to have ability to enable the internal logs when developing application to get additional information.
+
+To enable logging you need to add the below code at startup of the application.
+
+```csharp
+// pass the ILoggerFactory instance
+NotionClientLogging.ConfigureLogger(logger);
+
+```
+
+You can set the LogLevel in config file.
+```json
+{
+ "Logging": {
+ "LogLevel": {
+ "Notion.Client": "Trace"
+ }
+ }
+}
+```
+
+You can also refer `examples/list-users` example.
## Contributors
This project exists thanks to all the people who contribute.
diff --git a/Src/Notion.Client/Logging/Log.cs b/Src/Notion.Client/Logging/Log.cs
new file mode 100644
index 00000000..7fda66a5
--- /dev/null
+++ b/Src/Notion.Client/Logging/Log.cs
@@ -0,0 +1,25 @@
+using System;
+using Microsoft.Extensions.Logging;
+
+namespace Notion.Client
+{
+ internal static class Log
+ {
+ internal static ILogger logger;
+
+ internal static void Trace(string message, params object[] args)
+ {
+ logger?.LogTrace(message, args);
+ }
+
+ internal static void Information(string message, params object[] args)
+ {
+ logger?.LogInformation(message, args);
+ }
+
+ internal static void Error(Exception ex, string message, params object[] args)
+ {
+ logger?.LogError(ex, message, args);
+ }
+ }
+}
diff --git a/Src/Notion.Client/Logging/NotionClientLogging.cs b/Src/Notion.Client/Logging/NotionClientLogging.cs
new file mode 100644
index 00000000..b77ff15e
--- /dev/null
+++ b/Src/Notion.Client/Logging/NotionClientLogging.cs
@@ -0,0 +1,16 @@
+using Microsoft.Extensions.Logging;
+
+namespace Notion.Client
+{
+ public static class NotionClientLogging
+ {
+ internal static ILoggerFactory factory;
+
+ public static void ConfigureLogger(ILoggerFactory loggerFactory)
+ {
+ factory = loggerFactory;
+
+ Log.logger = Log.logger == null ? factory?.CreateLogger("Notion.Client") : Log.logger;
+ }
+ }
+}
diff --git a/Src/Notion.Client/Notion.Client.csproj b/Src/Notion.Client/Notion.Client.csproj
index 9c7f053f..31f53181 100644
--- a/Src/Notion.Client/Notion.Client.csproj
+++ b/Src/Notion.Client/Notion.Client.csproj
@@ -22,6 +22,7 @@
+
diff --git a/Src/Notion.Client/RestClient/LoggingHandler.cs b/Src/Notion.Client/RestClient/LoggingHandler.cs
new file mode 100644
index 00000000..8655bb97
--- /dev/null
+++ b/Src/Notion.Client/RestClient/LoggingHandler.cs
@@ -0,0 +1,29 @@
+using System;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Notion.Client
+{
+ public class LoggingHandler : DelegatingHandler
+ {
+ protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ Log.Trace("Request: {request}", request);
+
+ try
+ {
+ var response = await base.SendAsync(request, cancellationToken);
+
+ Log.Trace("Response: {response}", response);
+
+ return response;
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Failed to get response: {exception}", ex);
+ throw;
+ }
+ }
+ }
+}
diff --git a/Src/Notion.Client/RestClient/RestClient.cs b/Src/Notion.Client/RestClient/RestClient.cs
index ccd93fa9..b52b0ce3 100644
--- a/Src/Notion.Client/RestClient/RestClient.cs
+++ b/Src/Notion.Client/RestClient/RestClient.cs
@@ -153,7 +153,8 @@ private HttpClient EnsureHttpClient()
{
if (_httpClient == null)
{
- _httpClient = new HttpClient();
+ var pipeline = new LoggingHandler() { InnerHandler = new HttpClientHandler() };
+ _httpClient = new HttpClient(pipeline);
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
}
diff --git a/docs/README.md b/docs/README.md
index 542fe472..f1445c87 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -87,6 +87,30 @@ var complexFiler = new CompoundFilter(
- [x] Retrieve your token's bot user
- [x] Search
+## Enable internal logs
+The library make use of `ILoggerFactory` interface exposed by `Microsoft.Extensions.Logging`. Which allow you to have ability to enable the internal logs when developing application to get additional information.
+
+To enable logging you need to add the below code at startup of the application.
+
+```csharp
+// pass the ILoggerFactory instance
+NotionClientLogging.ConfigureLogger(logger);
+
+```
+
+You can set the LogLevel in config file.
+```json
+{
+ "Logging": {
+ "LogLevel": {
+ "Notion.Client": "Trace"
+ }
+ }
+}
+```
+
+You can also refer `examples/list-users` example.
+
## Contribution Guideline
Hello! Thank you for choosing to help contribute to this open source library. There are many ways you can contribute and help is always welcome. You can read the detailed [Contribution Guideline](https://github.com/notion-dotnet/notion-sdk-net/blob/main/CONTRIBUTING.md) defined here - we will continue to improve it.
diff --git a/examples/list-users/Program.cs b/examples/list-users/Program.cs
index c2f46fb4..340cdbb6 100644
--- a/examples/list-users/Program.cs
+++ b/examples/list-users/Program.cs
@@ -1,6 +1,9 @@
using System;
+using System.IO;
using System.Linq;
using System.Threading.Tasks;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
using Notion.Client;
namespace list_users
@@ -14,6 +17,19 @@ static async Task Main(string[] args)
AuthToken = ""
});
+ var configuration = new ConfigurationBuilder()
+ .AddJsonFile(Directory.GetCurrentDirectory() + "/appsettings.json")
+ .Build();
+
+ var factory = LoggerFactory.Create(builder =>
+ {
+ builder.ClearProviders();
+ builder.AddConfiguration(configuration.GetSection("Logging"));
+ builder.AddConsole();
+ });
+
+ NotionClientLogging.ConfigureLogger(factory);
+
var usersList = await client.Users.ListAsync();
Console.WriteLine(usersList.Results.Count());
diff --git a/examples/list-users/appsettings.json b/examples/list-users/appsettings.json
new file mode 100644
index 00000000..d685b3da
--- /dev/null
+++ b/examples/list-users/appsettings.json
@@ -0,0 +1,7 @@
+{
+ "Logging": {
+ "LogLevel": {
+ "Notion.Client": "Trace"
+ }
+ }
+}
diff --git a/examples/list-users/list-users.csproj b/examples/list-users/list-users.csproj
index 469b003b..c3d3affe 100644
--- a/examples/list-users/list-users.csproj
+++ b/examples/list-users/list-users.csproj
@@ -1,13 +1,16 @@
-
-
- Exe
- net5.0
- list_users
-
-
-
-
-
-
+
+ Exe
+ net5.0
+ list_users
+
+
+
+
+
+
+
+
+
+