AspNetCore.Docs/aspnetcore/blazor/fundamentals/logging.md

4.6 KiB

title author description monikerRange ms.author ms.custom ms.date no-loc uid zone_pivot_groups
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 12/16/2020
Home
Privacy
Kestrel
appsettings.json
ASP.NET Core Identity
cookie
Cookie
Blazor
Blazor Server
Blazor WebAssembly
Identity
Let's Encrypt
Razor
SignalR
blazor/fundamentals/logging blazor-hosting-models

ASP.NET Core Blazor logging

::: zone pivot="webassembly"

Configure custom logging in Blazor WebAssembly apps with the xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.Logging?displayProperty=nameWithType property.

Add the namespace for xref:Microsoft.AspNetCore.Components.WebAssembly.Hosting?displayProperty=fullName to Program.cs:

using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

In Program.Main of Program.cs, set the minimum logging level with xref:Microsoft.Extensions.Logging.LoggingBuilderExtensions.SetMinimumLevel%2A?displayProperty=nameWithType and add the custom logging provider:

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.

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.

[!NOTE] WebAssemblyConsoleLogger is internal and not available for direct use in developer code.

Add the namespace for xref:Microsoft.Extensions.Logging?displayProperty=fullName and inject an xref:Microsoft.Extensions.Logging.ILoggerProvider into the component:

@using Microsoft.Extensions.Logging
@inject ILoggerProvider LoggerProvider

In the component's OnInitializedAsync method, use xref:Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilderExtensions.ConfigureLogging%2A?displayProperty=nameWithType:

var connection = new HubConnectionBuilder()
    .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
    .ConfigureLogging(logging => logging.AddProvider(LoggerProvider))
    .Build();

::: zone-end

::: zone pivot="server"

For general ASP.NET Core logging guidance that pertains to Blazor Server, see xref:fundamentals/logging/index.

::: zone-end

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 components.

Pages/Counter.razor:

::: moniker range=">= aspnetcore-5.0"

[!code-razor]

::: moniker-end

::: moniker range="< aspnetcore-5.0"

[!code-razor]

::: moniker-end

The following example demonstrates logging with an xref:Microsoft.Extensions.Logging.ILoggerFactory in components.

Pages/Counter.razor:

::: moniker range=">= aspnetcore-5.0"

[!code-razor]

::: moniker-end

::: moniker range="< aspnetcore-5.0"

[!code-razor]

::: moniker-end

Additional resources