GithubHelp home page GithubHelp logo

philippe-laval / serilog-sinks-browserhttp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nblumhardt/serilog-sinks-browserhttp

0.0 1.0 0.0 305 KB

Serilog sink for client-side Blazor, with matching ASP.NET Core server relay

Home Page: https://serilog.net

License: Apache License 2.0

C# 92.45% PowerShell 7.55%

serilog-sinks-browserhttp's Introduction

Serilog.Sinks.BrowserHttp Build status

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.

Getting started

In a Blazor client (WASM) app, first install Serilog.Sinks.BrowserHttp:

dotnet add package Serilog.Sinks.BrowserHttp

The sink is enabled using WriteTo.BrowserHttp:

var builder = WebAssemblyHostBuilder.CreateDefault(args);
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(levelSwitch)
    .WriteTo.BrowserConsole()
    .WriteTo.BrowserHttp($"{builder.HostEnvironment.BaseAddress}ingest")
    .CreateLogger();

Log.Information("Hello, browser!");

Events will be POSTed in newline-delimited JSON batches to the given URL. See Serilog.Formatting.Compact for a description of the JSON schema that is used.

Server-side relay for ASP.NET Core

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 builder = WebAssemblyHostBuilder.CreateDefault(args);
var levelSwitch = new LoggingLevelSwitch();
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(levelSwitch)
    .WriteTo.BrowserHttp($"{builder.HostEnvironment.BaseAddress}ingest", controlLevelSwitch: levelSwitch)
    .CreateLogger();

Log.Information("Hello, browser!");

See the /samples directory in this repository for a working end-to-end example.

serilog-sinks-browserhttp's People

Contributors

nblumhardt avatar zbecknell avatar mattgwagner avatar benhysell avatar philippe-laval avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.