Skip to content

Latest commit

 

History

History
89 lines (62 loc) · 3.32 KB

WebAssemblyHostedLogging.md

File metadata and controls

89 lines (62 loc) · 3.32 KB

Blazor WebAssembly Hosted model console logging

Build Status Package Version NuGet Downloads License

About

Blazor extension for logging to browser console. Important NOTE: this package only works for apps using WebAssemly Hosting Model!

You can try it out by using the demo app.

NOTE: in .NET 5 WebAssembly Apps will log to browser's console automatically. This package uses Console.WriteLine() non JS based logging. So all log level messages will shown regardless of browser log filter.

Features

Logger

This package implements Microsoft Extensions Logging abstraction to support using of ILogger and ILogger<T> interface for WebAssemly Blazor Console logging.

When this package installed and configured all logs written by ILogger and ILogger<T> will reach Browser console logger and log messages will appear in the browser's developer tools Console tab.

Log levels

The logger supports the LogLevels defined by Microsoft LogLevel enum.

Configuration

Installation

Majorsoft.Blazor.Components.Deboudnce.Input is available on NuGet.

dotnet add package Majorsoft.Blazor.WebAssembly.Logging.Console

Use the --version option to specify a preview version to install.

Setup

Add the following code snippet to your WebAssembly hosted (client side) Blazor Application. Into the **Program.cs** file 'Main' method.

using Majorsoft.Blazor.WebAssembly.Logging.Console;

...

builder.Logging.AddBrowserConsole()
	.SetMinimumLevel(LogLevel.Debug) //Setting LogLevel is optional
	.AddFilter("Microsoft", LogLevel.Information); //System logs can be filtered.

Usage

After setup usage is very simple. Just use by standard logging with injected ILogger object. The following code snippet shows how to use logger in a Blazor component.

@using Microsoft.Extensions.Logging

@inject ILogger<Index> _logger
@code {
	protected override void OnInitialized()
	{
		_logger.LogDebug("Index init");
	}
}

The following code snippet shows how to use logger in .cs files.

using Microsoft.Extensions.Logging;

...

public class CustomCode
{
	private readonly ILogger<CustomCode> _logger;
	public CustomCode(ILogger<CustomCode> logger)
	{
		_logger = logger;
		_logger.LogDebug("CustomCode init");
	}
}