Skip to content

Commit 3eabedb

Browse files
Merge pull request #183 from notion-dotnet/tech/12-add-logging-support
Add logging support 🔊
2 parents bc97ff5 + 8b13761 commit 3eabedb

File tree

10 files changed

+157
-12
lines changed

10 files changed

+157
-12
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ var complexFiler = new CompoundFilter(
120120
- [x] Retrieve your token's bot user
121121
- [x] Search
122122

123+
## Enable internal logs
124+
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.
125+
126+
To enable logging you need to add the below code at startup of the application.
127+
128+
```csharp
129+
// pass the ILoggerFactory instance
130+
NotionClientLogging.ConfigureLogger(logger);
131+
132+
```
133+
134+
You can set the LogLevel in config file.
135+
```json
136+
{
137+
"Logging": {
138+
"LogLevel": {
139+
"Notion.Client": "Trace"
140+
}
141+
}
142+
}
143+
```
144+
145+
You can also refer `examples/list-users` example.
123146

124147
## Contributors
125148
This project exists thanks to all the people who contribute.

Src/Notion.Client/Logging/Log.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Notion.Client
5+
{
6+
internal static class Log
7+
{
8+
internal static ILogger logger;
9+
10+
internal static void Trace(string message, params object[] args)
11+
{
12+
logger?.LogTrace(message, args);
13+
}
14+
15+
internal static void Information(string message, params object[] args)
16+
{
17+
logger?.LogInformation(message, args);
18+
}
19+
20+
internal static void Error(Exception ex, string message, params object[] args)
21+
{
22+
logger?.LogError(ex, message, args);
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace Notion.Client
4+
{
5+
public static class NotionClientLogging
6+
{
7+
internal static ILoggerFactory factory;
8+
9+
public static void ConfigureLogger(ILoggerFactory loggerFactory)
10+
{
11+
factory = loggerFactory;
12+
13+
Log.logger = Log.logger == null ? factory?.CreateLogger("Notion.Client") : Log.logger;
14+
}
15+
}
16+
}

Src/Notion.Client/Notion.Client.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</PackageReference>
2323
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
2424
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
25+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/>
2526
</ItemGroup>
2627

2728
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Notion.Client
7+
{
8+
public class LoggingHandler : DelegatingHandler
9+
{
10+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
11+
{
12+
Log.Trace("Request: {request}", request);
13+
14+
try
15+
{
16+
var response = await base.SendAsync(request, cancellationToken);
17+
18+
Log.Trace("Response: {response}", response);
19+
20+
return response;
21+
}
22+
catch (Exception ex)
23+
{
24+
Log.Error(ex, "Failed to get response: {exception}", ex);
25+
throw;
26+
}
27+
}
28+
}
29+
}

Src/Notion.Client/RestClient/RestClient.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ private HttpClient EnsureHttpClient()
153153
{
154154
if (_httpClient == null)
155155
{
156-
_httpClient = new HttpClient();
156+
var pipeline = new LoggingHandler() { InnerHandler = new HttpClientHandler() };
157+
_httpClient = new HttpClient(pipeline);
157158
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
158159
}
159160

docs/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ var complexFiler = new CompoundFilter(
8787
- [x] Retrieve your token's bot user
8888
- [x] Search
8989

90+
## Enable internal logs
91+
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.
92+
93+
To enable logging you need to add the below code at startup of the application.
94+
95+
```csharp
96+
// pass the ILoggerFactory instance
97+
NotionClientLogging.ConfigureLogger(logger);
98+
99+
```
100+
101+
You can set the LogLevel in config file.
102+
```json
103+
{
104+
"Logging": {
105+
"LogLevel": {
106+
"Notion.Client": "Trace"
107+
}
108+
}
109+
}
110+
```
111+
112+
You can also refer `examples/list-users` example.
113+
90114
## Contribution Guideline
91115

92116
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.

examples/list-users/Program.cs

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Logging;
47
using Notion.Client;
58

69
namespace list_users
@@ -14,6 +17,19 @@ static async Task Main(string[] args)
1417
AuthToken = "<Token>"
1518
});
1619

20+
var configuration = new ConfigurationBuilder()
21+
.AddJsonFile(Directory.GetCurrentDirectory() + "/appsettings.json")
22+
.Build();
23+
24+
var factory = LoggerFactory.Create(builder =>
25+
{
26+
builder.ClearProviders();
27+
builder.AddConfiguration(configuration.GetSection("Logging"));
28+
builder.AddConsole();
29+
});
30+
31+
NotionClientLogging.ConfigureLogger(factory);
32+
1733
var usersList = await client.Users.ListAsync();
1834

1935
Console.WriteLine(usersList.Results.Count());

examples/list-users/appsettings.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Notion.Client": "Trace"
5+
}
6+
}
7+
}

examples/list-users/list-users.csproj

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
3-
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFramework>net5.0</TargetFramework>
6-
<RootNamespace>list_users</RootNamespace>
7-
</PropertyGroup>
8-
9-
<ItemGroup>
10-
<PackageReference Include="Notion.Net" Version="1.0.4"/>
11-
</ItemGroup>
12-
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>list_users</RootNamespace>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<!-- <PackageReference Include="Notion.Net" Version="1.0.4"/> -->
9+
<ProjectReference Include="..\..\Src\Notion.Client\Notion.Client.csproj"/>
10+
</ItemGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0"/>
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0"/>
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0"/>
15+
</ItemGroup>
1316
</Project>

0 commit comments

Comments
 (0)