Skip to content

Commit

Permalink
Added option EnableChromiumLogging to enable Chromium logging (for de…
Browse files Browse the repository at this point in the history
…bugging)
  • Loading branch information
Kees committed May 31, 2024
1 parent a0463e7 commit fee147c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ChromiumHtmlToPdfLib/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ internal async Task NavigateToAsync(

var pageSetDocumentContent = new Message { Method = "Page.setDocumentContent" };
pageSetDocumentContent.AddParameter("frameId", frameResult.Result.FrameTree.Frame.Id);
pageSetDocumentContent.AddParameter("html", html!);
pageSetDocumentContent.AddParameter("html", html);
await _pageConnection.SendAsync(pageSetDocumentContent).ConfigureAwait(false);
// When using setDocumentContent a Page.frameNavigated event is never fired, so we have to set the waitForNetworkIdle to true our self
pageLoadingState = PageLoadingState.WaitForNetworkIdle;
Expand Down Expand Up @@ -603,7 +603,7 @@ public async Task RunJavascriptAsync(string script, CancellationToken cancellati
errorDescription = evaluateError.Result.ExceptionDetails.Exception.Description;

if (!string.IsNullOrEmpty(errorDescription))
throw new ChromiumException(errorDescription!);
throw new ChromiumException(errorDescription);

var evaluate = Evaluate.FromJson(result);
var internalResult = evaluate.Result?.Result?.ToString();
Expand Down
42 changes: 39 additions & 3 deletions ChromiumHtmlToPdfLib/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ private enum OutputFormat
private string? _instanceId;

private CancellationTokenSource? _cancellationTokenSource;

/// <summary>
/// <see cref="DoNotDeleteTempDirectory"/>
/// </summary>
private bool _doNotDeleteTempDirectory;
#endregion

#region Properties
Expand Down Expand Up @@ -353,6 +358,7 @@ public string? TempDirectory
throw new DirectoryNotFoundException($"The directory '{value}' does not exists");

_tempDirectory = value;
_logger?.Info($"Temp directory set to '{value}'");
CurrentCacheDirectory = null;
}
}
Expand All @@ -369,7 +375,10 @@ private DirectoryInfo GetTempDirectory
: new DirectoryInfo(Path.Combine(_tempDirectory, Guid.NewGuid().ToString()));

if (!CurrentTempDirectory.Exists)
{
_logger?.Info($"Creating temp directory '{CurrentTempDirectory.FullName}'");
CurrentTempDirectory.Create();
}

return CurrentTempDirectory;
}
Expand All @@ -381,7 +390,15 @@ private DirectoryInfo GetTempDirectory
/// <remarks>
/// For debugging purposes
/// </remarks>
public bool DoNotDeleteTempDirectory { get; set; }
public bool DoNotDeleteTempDirectory
{
get => _doNotDeleteTempDirectory;
set
{
_logger?.Info($"Setting DoNotDeleteTempDirectory to '{value}'");
_doNotDeleteTempDirectory = value;
}
}

/// <summary>
/// The directory used for temporary files (<see cref="GetTempDirectory"/>)
Expand Down Expand Up @@ -464,7 +481,7 @@ private WebProxy? WebProxy
public bool CaptureSnapshot { get; set; }

/// <summary>
/// The <see cref="System.IO.Stream" /> where to write the page snapshot when <see cref="CaptureSnapshot" />
/// The <see cref="Stream" /> where to write the page snapshot when <see cref="CaptureSnapshot" />
/// is set to <c>true</c>
/// </summary>
public Stream? SnapshotStream { get; set; }
Expand Down Expand Up @@ -543,6 +560,18 @@ public bool UseOldHeadlessMode
}
}
}

/// <summary>
/// Enables Chromium logging;<br/>
/// - The output will be saved to the file <b>chrome_debug.log</b> in Chrome's user data directory<br/>
/// - Logs are overwritten each time you restart chrome<br/>
/// </summary>
/// <remarks>
/// If the environment variable CHROME_LOG_FILE is set, Chrome will write its debug log to its specified location.<br/>
/// Example: Setting CHROME_LOG_FILE to "chrome_debug.log" will cause the log file to be written to the Chrome process's<br/>
/// current working directory while setting it to "D:\chrome_debug.log" will write the log to the root of your computer's D: drive.
/// </remarks>
public bool EnableChromiumLogging { get; set; }
#endregion

#region Constructor
Expand Down Expand Up @@ -917,6 +946,13 @@ public void ResetChromiumArguments()
AddChromiumArgument("--run-all-compositor-stages-before-draw");
AddChromiumArgument("--remote-debugging-port", "0"); // With a value of 0, Chrome will automatically select a useable port and will set navigator.webdriver to true.

if (EnableChromiumLogging)
{
_logger?.Info("Enabling Chromium logging");
AddChromiumArgument("--enable-logging");
AddChromiumArgument("--v=1");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_logger?.Info("Detected Linux operating system, adding the parameter '--no-sandbox'");
Expand Down Expand Up @@ -1474,7 +1510,7 @@ private async Task ConvertAsync(
}

_logger?.Info("Waiting for window.status '{status}' or a timeout of {timeout} milliseconds", waitForWindowStatus, waitForWindowsStatusTimeout);
var match = await _browser.WaitForWindowStatusAsync(waitForWindowStatus!, waitForWindowsStatusTimeout, cancellationToken).ConfigureAwait(false);
var match = await _browser.WaitForWindowStatusAsync(waitForWindowStatus, waitForWindowsStatusTimeout, cancellationToken).ConfigureAwait(false);
if (!match)
_logger?.Info("Waiting timed out");
else
Expand Down
3 changes: 1 addition & 2 deletions ChromiumHtmlToPdfLib/Helpers/DocumentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
using AngleSharp.Html.Dom;
using AngleSharp.Io.Network;
using ChromiumHtmlToPdfLib.Loggers;
using ChromiumHtmlToPdfLib.Protocol;
using ChromiumHtmlToPdfLib.Settings;
using Ganss.Xss;
using Svg;
Expand Down Expand Up @@ -1043,4 +1042,4 @@ public void Dispose()
_stopwatch?.Stop();
}
#endregion
}
}

0 comments on commit fee147c

Please sign in to comment.