Skip to content

Commit 4742d2b

Browse files
authored
[Preview 4] Logging configuration in Blazor WASM (#17671)
1 parent 408ebbd commit 4742d2b

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ Configuration files are cached for offline use. With [Progressive Web Applicatio
127127

128128
For more information on how background updates are handled by PWAs, see <xref:blazor/progressive-web-app#background-updates>.
129129

130+
### Logging
131+
132+
For information on Blazor WebAssembly logging support, see <xref:fundamentals/logging/index#create-logs-in-blazor-webassembly>.
133+
130134
## Blazor Server
131135

132136
### Reflect the connection state in the UI

aspnetcore/fundamentals/logging/index.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ If you need to configure a service that depends on `ILogger<T>`, you can still d
159159

160160
The preceding highlighted code is a `Func` that runs the first time the DI container needs to construct an instance of `MyService`. You can access any of the registered services in this way.
161161

162+
### Create logs in Blazor WebAssembly
163+
164+
Configure logging in Blazor WebAssembly apps with the `WebAssemblyHostBuilder.Logging` property in `Program.Main`:
165+
166+
```csharp
167+
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
168+
169+
...
170+
171+
var builder = WebAssemblyHostBuilder.CreateDefault(args);
172+
173+
builder.Logging.SetMinimumLevel(LogLevel.Debug);
174+
builder.Logging.AddProvider(new CustomLoggingProvider());
175+
```
176+
177+
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`.
178+
162179
### No asynchronous logger methods
163180

164181
Logging should be so fast that it isn't worth the performance cost of asynchronous code. If your logging data store is slow, don't write to it directly. Consider writing the log messages to a fast store initially, then move them to the slow store later. For example, if you're logging to SQL Server, you don't want to do that directly in a `Log` method, since the `Log` methods are synchronous. Instead, synchronously add log messages to an in-memory queue and have a background worker pull the messages out of the queue to do the asynchronous work of pushing data to SQL Server. For more information, see [this](https://github.com/dotnet/AspNetCore.Docs/issues/11801) GitHub issue.

0 commit comments

Comments
 (0)