Majorsoft.Blazor.Server.Logging.Console 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Majorsoft.Blazor.Server.Logging.Console --version 1.0.0                
NuGet\Install-Package Majorsoft.Blazor.Server.Logging.Console -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Majorsoft.Blazor.Server.Logging.Console" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Majorsoft.Blazor.Server.Logging.Console --version 1.0.0                
#r "nuget: Majorsoft.Blazor.Server.Logging.Console, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Majorsoft.Blazor.Server.Logging.Console as a Cake Addin
#addin nuget:?package=Majorsoft.Blazor.Server.Logging.Console&version=1.0.0

// Install Majorsoft.Blazor.Server.Logging.Console as a Cake Tool
#tool nuget:?package=Majorsoft.Blazor.Server.Logging.Console&version=1.0.0                

Blazor Server 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 Server Hosting Model!

Features

Logger

This package implements Microsoft Extensions Logging abstraction to support using of ILogger and ILogger<T> interface for Server 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

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

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

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

Setup

Blazor Server hosted model requires more complicated setup. Since your code runs on the Sever and logs has to be transfered to the Browser.

Follow the next 3 required steps to make it work.

Step 1. Register service and LoggerProvider

Add the following code snippet to your Server hosted Blazor Application. Into the Program.cs file 'CreateHostBuilder' method.

using Blazor.Server.Logging.Console;

...

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureWebHostDefaults(webBuilder =>
		{
			webBuilder.ConfigureLogging(logger =>
			{
				logger.AddBrowserConsole()
					.SetMinimumLevel(LogLevel.Trace) //Setting LogLevel is optional
					.AddFilter("Microsoft", LogLevel.Information); //System logs can be filtered.
			});
			webBuilder.UseStartup<Startup>();
		});
Step 2. Register SignalR Hub

All SignalR Hubs must be registered with MapHub method in the Startup.cs file Configure method.

public void Configure(IApplicationBuilder app)
{
	...
	app.UseEndpoints(endpoints =>
	{
		//Blazor server code
		endpoints.MapBlazorHub();
		endpoints.MapFallbackToPage("/_Host");
		//Logging setup
		endpoints.MapHub<BlazorServerConsoleLoggingHub>(BlazorServerConsoleLoggingHub.HubUrl);
	});
}
Step 3. Start SignalR connection

Initialize and start SignalR connection with Sever. It should be executed only once. Best place to do it is in MainLayout.razor file.

@using Blazor.Server.Logging.Console
@inject IBrowserConsoleLoggerService _browserConsoleLogger

@implements IAsyncDisposable

@code{
	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if(firstRender)
		{
			//setup console log
			await _browserConsoleLogger.StartLoggerAsync();
		}
	}

	public async ValueTask DisposeAsync()
	{
		if (_browserConsoleLogger is not null)
		{
			await _browserConsoleLogger.DisposeAsync();
		}
	}
}

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");
	}
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Majorsoft.Blazor.Server.Logging.Console:

Repository Stars
majorimi/blazor-components
Components collection and extensions for Blazor applications.
Version Downloads Last updated
1.5.0 1,457 8/27/2021
1.4.0 452 7/15/2021
1.3.0 1,024 3/10/2021
1.2.0 495 1/5/2021
1.1.0 452 12/18/2020
1.0.1 496 11/30/2020
1.0.0 464 11/11/2020
0.9.52-rc.2.20479.15 257 10/30/2020
0.9.50-rc.2.20479.15 226 10/27/2020