-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from notion-dotnet/tech/12-add-logging-support
Add logging support 🔊
- Loading branch information
Showing
10 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Notion.Client": "Trace" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |