Skip to content

Commit

Permalink
Add Log CDP example (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Jul 16, 2019
1 parent 512880e commit ac46d8c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
37 changes: 37 additions & 0 deletions docfx_project/examples/LodCDP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# How to log messages between Puppeteer-Sharp and Chromium

_Contributors: [Darío Kondratiuk](https://www.hardkoded.com/)_

## Problem

You want to log the messages sent by your app to Chromium and the messages received by Chromium.

## Solution

Add [Serilog.Extensions.Logging.File](https://www.nuget.org/packages/Serilog.Extensions.Logging.File/) Nuget package.

First we need to create an `ILoggerFactory`

```js
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
}
```
5 changes: 4 additions & 1 deletion docfx_project/examples/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
href: Page.WaitForSelectorAsync.Searching.md
- name: Download and Reuse Chrome from a custom location
href: ReuseChrome.md

- name: Advanced
items:
- name: How to log CDP communication
href: LogCDP.md

0 comments on commit ac46d8c

Please sign in to comment.