Skip to content

Commit

Permalink
Merge pull request #149 from MaceWindu/feature/structured-logs
Browse files Browse the repository at this point in the history
Add support for structured logging
  • Loading branch information
Sicos1977 authored Mar 12, 2024
2 parents e93c5fe + 2152734 commit 4a680d4
Show file tree
Hide file tree
Showing 10 changed files with 334 additions and 217 deletions.
11 changes: 10 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[*.cs]

root = true

[*]
indent_style = space
indent_size = 4
insert_final_newline = true
end_of_line = crlf

[*.cs]

# IDE0055: Fix formatting
dotnet_diagnostic.IDE0055.severity = none
5 changes: 5 additions & 0 deletions ChromiumHtmlToPdf.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChromiumHtmlToPdfLib", "Chr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChromiumHtmlToPdfConsole", "ChromiumHtmlToPdfConsole\ChromiumHtmlToPdfConsole.csproj", "{0ECDC06A-D578-4F2C-A742-A5EFD5EA2C6F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3B2D53A2-692C-4A5A-89D8-3ABC3F8EE848}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
125 changes: 78 additions & 47 deletions ChromiumHtmlToPdfLib/Browser.cs

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions ChromiumHtmlToPdfLib/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ internal Connection(string url, int timeout, Logger? logger)
_url = url;
_timeout = timeout;
_logger = logger;
_logger?.WriteToLog($"Creating new websocket connection to url '{url}'");
_logger?.Info("Creating new websocket connection to url '{url}'", url);
_webSocket = new ClientWebSocket();
_receiveLoopCts = new CancellationTokenSource();
OpenWebSocketAsync().GetAwaiter().GetResult();
Expand Down Expand Up @@ -156,6 +156,10 @@ private static async Task ReceiveLoop(object? stateData)
{
// Ignore
}
catch (OperationCanceledException)
{
// Ignore
}
catch (Exception exception)
{
WebSocketOnError(state.Logger, new ErrorEventArgs(exception));
Expand All @@ -177,12 +181,12 @@ private async Task OpenWebSocketAsync()
{
if (_webSocket.State is WebSocketState.Open or WebSocketState.Connecting) return;

_logger?.WriteToLog($"Opening websocket connection with a timeout of {_timeout} milliseconds");
_logger?.Info("Opening websocket connection with a timeout of {timeout} milliseconds", _timeout);

try
{
await _webSocket.ConnectAsync(new Uri(_url), new CancellationTokenSource(_timeout).Token).ConfigureAwait(false);
_logger?.WriteToLog("Websocket opened");
_logger?.Info("Websocket opened");
}
catch (Exception exception)
{
Expand All @@ -199,7 +203,7 @@ private static void WebSocketOnMessageReceived(Logger? logger, Action<string> on
var error = CheckForError(response);

if (!string.IsNullOrEmpty(error))
logger?.WriteToLog(error!);
logger?.Error("{error}", error);

onMessageReceived(response);
}
Expand All @@ -211,7 +215,7 @@ private void OnMessageReceived(string response)

private static void WebSocketOnError(Logger? logger, ErrorEventArgs e)
{
logger?.WriteToLog(ExceptionHelpers.GetInnerException(e.Exception));
logger?.Error(e.Exception, "{exception}", ExceptionHelpers.GetInnerException(e.Exception));
}

private void WebSocketOnClosed(EventArgs e)
Expand Down Expand Up @@ -329,11 +333,11 @@ public async Task InternalDisposeAsync()

_receiveLoopCts.Cancel();

_logger?.WriteToLog($"Disposing websocket connection to url '{_url}'");
_logger?.Info("Disposing websocket connection to url '{url}'", _url);

if (_webSocket.State == WebSocketState.Open)
{
_logger?.WriteToLog("Closing web socket");
_logger?.Info("Closing web socket");

try
{
Expand All @@ -342,14 +346,14 @@ public async Task InternalDisposeAsync()
}
catch (Exception exception)
{
_logger?.WriteToLog($"An error occurred while closing the web socket, error: '{ExceptionHelpers.GetInnerException(exception)}'");
_logger?.Error("An error occurred while closing the web socket, error: '{error}'", ExceptionHelpers.GetInnerException(exception));
}

_logger?.WriteToLog("Websocket connection closed");
_logger?.Info("Websocket connection closed");

WebSocketOnClosed(EventArgs.Empty);
_webSocket.Dispose();
_logger?.WriteToLog("Web socket connection disposed");
_logger?.Info("Web socket connection disposed");
}

_disposed = true;
Expand Down Expand Up @@ -378,4 +382,4 @@ public async ValueTask DisposeAsync()
}
#endif
#endregion
}
}
142 changes: 77 additions & 65 deletions ChromiumHtmlToPdfLib/Converter.cs

Large diffs are not rendered by default.

Loading

0 comments on commit 4a680d4

Please sign in to comment.