Introduction To Logging Framework In Blazor With .Net Core

Logging is a very critical and essential part of any software. It helps us in the investigation of the essence of problems.

Introduction
 
Logging is a very critical and essential part of any software. It helps us in the investigation of the essence of problems. The .NET Core Framework has built-in support for logging APIs that is able to work with logging providers.
 
Blazor is a new experimental .NET web framework using C#/Razor and HTML that runs in the browser with Web Assembly. Blazor endows full stack web development with the consistency, stability, and productivity of .NET. Many changes are going on in this framework and also many new extensions are available that work with Blazor.
 
In this article, we will learn about the Blazor extension for logging. Now logging extension for Blazor is available. This extension has an implementation for the Microsoft.Extensions.Logging to support the ILogger interface with Blazor code. When this extension is configured, all the logs appear in the browser's console. This Blazor extension provides nearly the same feature as Microsoft.Extensions.Logging provides for .NET Core.
 
How to configure the provider
 
Logging extension is not added with default project template. This extension is available on the NuGet package manager. Using the following command, we can add logging to our project.
 
Using Package Manager
  1. PM > Install-Package Blazor.Extensions.Logging -Version 0.1.2  
Using .NET CLI
  1. >dotnet add package Blazor.Extensions.Logging --version 0.1.2  
The next step is to register this extension service by adding the logging service to BrowserServiceProvider. Using the following code snippet, we can configure browser console logger.
  1. var serviceProvider = new BrowserServiceProvider(services =>  
  2. {  
  3.     // Add Blazor.Extensions.Logging.BrowserConsoleLogger  
  4.     services.AddLogging(builder => builder  
  5.         .AddBrowserConsole() // Register the logger with the ILoggerBuilder  
  6.     );  
  7. });  
How to Use
 
With .NET Core, Logger is available as DI (dependency injection) to every controller by default. In the Blazor, there is no controller. So, we need to inject the logger service to Blazor component. Using the following code we can consume the logger in a Blazor component.
 
Using cshtml
  1. @using Microsoft.Extensions.Logging;  
  2. @inject ILogger<Index> logger  
  3. @page "/"  
  4.   
  5. <h1>Test Logger</h1>  
  6.   
  7. @functions {  
  8.     protected override async Task OnInitAsync()  
  9.     {  
  10.         logger.LogDebug("OnInitAsync");  
  11.     }  
  12. }  
Outside the cshtml
  1. [Inject]  
  2. protected ILogger<MyTestClass> logger { getset; }  
  3.   
  4. public void LogInfo(string message)  
  5. {  
  6.   logger.LogDebug(message);  
  7. }  
The current version of Blazor and MEL (Microsoft Extensions Logging) only supports the code based configuration and lightweight Microsoft Extensions Configuration based configuration is not supported.
 
Log Level
 
Blazor extension for logger is supporting all the log levels defined in MEL (Microsoft Extensions Logging). Using a SetMinimumLevel method of ILoggingBuilder interface, we can set a minimum log level.
 
Example
 
In the following example, I have set minimum log level to Information, so log levels below information (i.e. trace & debug) are ignored for logging.
  1. using Blazor.Extensions.Logging;  
  2. using Microsoft.AspNetCore.Blazor.Browser.Rendering;  
  3. using Microsoft.AspNetCore.Blazor.Browser.Services;  
  4. using Microsoft.Extensions.DependencyInjection;  
  5. using Microsoft.Extensions.Logging;  
  6. using System;  
  7.   
  8. namespace BlazorDemoApp  
  9. {  
  10.     public class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.               
  15.             var serviceProvider = new BrowserServiceProvider(services =>  
  16.             {  
  17.                 // Add Blazor.Extensions.Logging.BrowserConsoleLogger  
  18.                 services.AddLogging(builder => builder  
  19.                 .AddBrowserConsole() // Register the logger with the ILoggerBuilder  
  20.                 .SetMinimumLevel(LogLevel.Information) // Set the minimum log level to Information  
  21.             );  
  22.             });  
  23.   
  24.             new BrowserRenderer(serviceProvider).AddComponent<App>("app");  
  25.         }  
  26.     }  
  27. }  
Summary
 
Blazor extension for Logging is very simple and nearly the same as MEL (Microsoft Extensions Logging). Furthermore a provider may be added in the later release. Blazor is an experimental project and not yet in production. Some of the content of this article may become invalid in future releases of Blazor.
 
You can view or download the source code from the GitHub link here.