-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce InitAction in browser constructor (#1871)
- Loading branch information
Showing
8 changed files
with
105 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
# How to log messages between Puppeteer-Sharp and Chromium | ||
|
||
_Contributors: [Darío Kondratiuk](https://www.hardkoded.com/), [Barnabas Szenasi](https://outisnemo.com/)_ | ||
|
||
## Problem | ||
|
||
You want to log the messages sent by your app to Chromium and the messages received by Chromium. Below you can find two solutions for the most common logging frameworks. | ||
|
||
## Log CDP messages to files using Serilog | ||
|
||
Add [Serilog.Extensions.Logging.File](https://www.nuget.org/packages/Serilog.Extensions.Logging.File/) Nuget package. | ||
|
||
First we need to create an `ILoggerFactory` | ||
|
||
```cs | ||
private static ILoggerFactory GetLoggerFactory(string file) | ||
{ | ||
var factory = new LoggerFactory(); | ||
var filter = new FilterLoggerSettings | ||
{ | ||
{ "Connection", LogLevel.Trace } | ||
}; | ||
|
||
factory.WithFilter(filter).AddFile(file, LogLevel.Trace); | ||
|
||
return factory; | ||
} | ||
``` | ||
|
||
Now we can use `GetLoggerFactory` to inject a logger into Puppeteer. | ||
|
||
```cs | ||
using (var browser = await Puppeteer.LaunchAsync(browserOptions, GetLoggerFactory(fileName))) | ||
{ | ||
//Some code | ||
} | ||
``` | ||
|
||
## Log CDP messages to console using Microsoft.Extensions.Logging | ||
|
||
Add [Microsoft.Extensions.Logging.Console](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Console) Nuget package. | ||
|
||
First we need to create an `ILoggerFactory` | ||
|
||
```cs | ||
private static ILoggerFactory GetLoggerFactory() | ||
{ | ||
var loggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => | ||
{ | ||
builder | ||
.AddConsole(options => options.Format = ConsoleLoggerFormat.Systemd) | ||
.SetMinimumLevel(LogLevel.Trace); | ||
}); | ||
|
||
return loggerFactory; | ||
} | ||
``` | ||
|
||
Now we can use `GetLoggerFactory` to inject a logger into Puppeteer.LaunchAsync or Puppeteer.ConnectAsync method. | ||
|
||
```cs | ||
using (var browser = await Puppeteer.LaunchAsync(browserOptions, GetLoggerFactory())) | ||
{ | ||
//Some code | ||
} | ||
``` |
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 |
---|---|---|
|
@@ -17,4 +17,4 @@ | |
- name: Advanced | ||
items: | ||
- name: How to log CDP communication | ||
href: LogCDP.md | ||
href: LogCDPCommunication.md |
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
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