3.6 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | no-loc | uid | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ASP.NET Core Blazor logging | guardrex | Learn about logging in Blazor apps, including log level configuration and how to write log messages from Razor components. | >= aspnetcore-3.1 | riande | mvc | 06/10/2020 |
|
blazor/fundamentals/logging |
ASP.NET Core Blazor logging
Blazor WebAssembly
Configure logging in Blazor WebAssembly apps with the WebAssemblyHostBuilder.Logging
property in Program.Main
:
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
...
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddProvider(new CustomLoggingProvider());
The Logging
property is of type xref:Microsoft.Extensions.Logging.ILoggingBuilder, so all of the extension methods available on xref:Microsoft.Extensions.Logging.ILoggingBuilder are also available on Logging
.
Logging configuration can be loaded from app settings files. For more information, see xref:blazor/fundamentals/configuration#logging-configuration.
Blazor Server
For general ASP.NET Core logging guidance, see xref:fundamentals/logging/index.
Blazor WebAssembly SignalR .NET client logging
Inject an xref:Microsoft.Extensions.Logging.ILoggerProvider to add a WebAssemblyConsoleLogger
to the logging providers passed to xref:Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder. Unlike a traditional xref:Microsoft.Extensions.Logging.Console.ConsoleLogger, WebAssemblyConsoleLogger
is a wrapper around browser-specific logging APIs (for example, console.log
). Use of WebAssemblyConsoleLogger
makes logging possible within Mono inside a browser context.
@using Microsoft.Extensions.Logging
@inject ILoggerProvider LoggerProvider
...
var connection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
.ConfigureLogging(logging => logging.AddProvider(LoggerProvider))
.Build();
Log in Razor components
Loggers respect app startup configuration.
The using
directive for xref:Microsoft.Extensions.Logging is required to support Intellisense completions for APIs, such as xref:Microsoft.Extensions.Logging.LoggerExtensions.LogWarning%2A and xref:Microsoft.Extensions.Logging.LoggerExtensions.LogError%2A.
The following example demonstrates logging with an xref:Microsoft.Extensions.Logging.ILogger in Razor components:
@page "/counter"
@using Microsoft.Extensions.Logging;
@inject ILogger<Counter> logger;
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
logger.LogWarning("Someone has clicked me!");
currentCount++;
}
}
The following example demonstrates logging with an xref:Microsoft.Extensions.Logging.ILoggerFactory in Razor components:
@page "/counter"
@using Microsoft.Extensions.Logging;
@inject ILoggerFactory LoggerFactory
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
var logger = LoggerFactory.CreateLogger<Counter>();
logger.LogWarning("Someone has clicked me!");
currentCount++;
}
}