Note: this project is currently at the proof-of-concept stage, and may eat your laundry.
A Serilog sink for client-side Blazor that posts batched events using the browser's HTTP stack. These can be sent to any remote HTTP endpoint, including the app's origin server. A companion package, Serilog.AspNetCore.Ingestion, is also published from this repository, and can be used to relay events from the client to Serilog running in the ASP.NET Core server process.
In a Blazor client (WASM) app, first install Serilog.Sinks.BrowserHttp:
dotnet add package Serilog.Sinks.BrowserHttp
The sink is enabled using WriteTo.BrowserHttp
:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.WriteTo.BrowserConsole()
.WriteTo.BrowserHttp("https://logs.example.com")
.CreateLogger();
Log.Information("Hello, browser!");
Events will be POST
ed in newline-delimited JSON batches to the given URL. See
Serilog.Formatting.Compact for a description of the JSON
schema that is used.
To use the server-side relay in an ASP.NET Core app that uses Serilog, install Serilog.AspNetCore.Ingestion:
dotnet add package Serilog.AspNetCore.Ingestion
Then, add UseSerilogIngestion()
to the app builder in Startup.cs:
app.UseSerilogIngestion();
The client app should be configured with LoggingLevelSwitch
so that the server can control the client's logging level. An endpoint address is not required. For example:
// In a Blazor WASM Program.cs file
var levelSwitch = new LoggingLevelSwitch();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.WriteTo.BrowserHttp(controlLevelSwitch: levelSwitch)
.CreateLogger();
Log.Information("Hello, browser!");
See the /samples
directory in this repository for a working end-to-end example.