8000 Add logging support 🔊 by KoditkarVedant · Pull Request #183 · notion-dotnet/notion-sdk-net · GitHub
[go: up one dir, main page]

Skip to content

Add logging support 🔊 #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions Src/Notion.Client/Logging/Log.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
16 changes: 16 additions & 0 deletions Src/Notion.Client/Logging/NotionClientLogging.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions Src/Notion.Client/Notion.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0"/>
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 29 additions & 0 deletions Src/Notion.Client/RestClient/LoggingHandler.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> 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;
}
}
}
}
3 changes: 2 additions & 1 deletion Src/Notion.Client/RestClient/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 24 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
16 changes: 16 additions & 0 deletions examples/list-users/Program.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +17,19 @@ static async Task Main(string[] args)
AuthToken = "<Token>"
});

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());
Expand Down
7 changes: 7 additions & 0 deletions examples/list-users/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Logging": {
"LogLevel": {
"Notion.Client": "Trace"
}
}
}
25 changes: 14 additions & 11 deletions examples/list-users/list-users.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>list_users</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Notion.Net" Version="1.0.4"/>
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>list_users</RootNamespace>
</PropertyGroup>
<ItemGroup>
<!-- <PackageReference Include="Notion.Net" Version="1.0.4"/> -->
<ProjectReference Include="..\..\Src\Notion.Client\Notion.Client.csproj"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0"/>
</ItemGroup>
</Project>
0