Skip to content

Commit 0a6d6a7

Browse files
authored
Augment logging documentation for Blazor (#17980)
1 parent c9d6cb9 commit 0a6d6a7

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

aspnetcore/blazor/handle-errors.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Discover how ASP.NET Core Blazor how Blazor manages unhandled excep
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 03/29/2020
8+
ms.date: 04/23/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/handle-errors
1111
---
@@ -106,6 +106,8 @@ During development, Blazor usually sends the full details of exceptions to the b
106106

107107
You must decide which incidents to log and the level of severity of logged incidents. Hostile users might be able to trigger errors deliberately. For example, don't log an incident from an error where an unknown `ProductId` is supplied in the URL of a component that displays product details. Not all errors should be treated as high-severity incidents for logging.
108108

109+
For more information, see <xref:fundamentals/logging/index#create-logs-in-blazor>.
110+
109111
## Places where errors may occur
110112

111113
Framework and app code may trigger unhandled exceptions in any of the following locations:

aspnetcore/blazor/hosting-model-configuration.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn about Blazor hosting model configuration, including how to in
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 04/16/2020
8+
ms.date: 04/23/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/hosting-model-configuration
1111
---
@@ -129,7 +129,7 @@ For more information on how background updates are handled by PWAs, see <xref:bl
129129

130130
### Logging
131131

132-
For information on Blazor WebAssembly logging support, see <xref:fundamentals/logging/index#create-logs-in-blazor-webassembly>.
132+
For information on Blazor WebAssembly logging support, see <xref:fundamentals/logging/index#create-logs-in-blazor>.
133133

134134
## Blazor Server
135135

@@ -247,3 +247,7 @@ To configure the SignalR client in the *Pages/_Host.cshtml* file:
247247
});
248248
</script>
249249
```
250+
251+
### Logging
252+
253+
For information on Blazor Server logging support, see <xref:fundamentals/logging/index#create-logs-in-blazor>.

aspnetcore/fundamentals/logging/index.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to use the logging framework provided by the Microsoft.Ex
55
monikerRange: '>= aspnetcore-2.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 4/17/2020
8+
ms.date: 4/23/2020
99
uid: fundamentals/logging/index
1010
---
1111
# Logging in .NET Core and ASP.NET Core
@@ -159,7 +159,9 @@ 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
162+
### Create logs in Blazor
163+
164+
#### Blazor WebAssembly
163165

164166
Configure logging in Blazor WebAssembly apps with the `WebAssemblyHostBuilder.Logging` property in `Program.Main`:
165167

@@ -176,6 +178,63 @@ builder.Logging.AddProvider(new CustomLoggingProvider());
176178

177179
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`.
178180

181+
#### Log in Razor components
182+
183+
Loggers respect app startup configuration.
184+
185+
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>.
186+
187+
The following example demonstrates logging with an <xref:Microsoft.Extensions.Logging.ILogger> in Razor components:
188+
189+
```razor
190+
@page "/counter"
191+
@using Microsoft.Extensions.Logging;
192+
@inject ILogger<Counter> logger;
193+
194+
<h1>Counter</h1>
195+
196+
<p>Current count: @currentCount</p>
197+
198+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
199+
200+
@code {
201+
private int currentCount = 0;
202+
203+
private void IncrementCount()
204+
{
205+
logger.LogWarning("Someone has clicked me!");
206+
207+
currentCount++;
208+
}
209+
}
210+
```
211+
212+
The following example demonstrates logging with an <xref:Microsoft.Extensions.Logging.ILoggerFactory> in Razor components:
213+
214+
```razor
215+
@page "/counter"
216+
@using Microsoft.Extensions.Logging;
217+
@inject ILoggerFactory LoggerFactory
218+
219+
<h1>Counter</h1>
220+
221+
<p>Current count: @currentCount</p>
222+
223+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
224+
225+
@code {
226+
private int currentCount = 0;
227+
228+
private void IncrementCount()
229+
{
230+
var logger = LoggerFactory.CreateLogger<Counter>();
231+
logger.LogWarning("Someone has clicked me!");
232+
233+
currentCount++;
234+
}
235+
}
236+
```
237+
179238
### No asynchronous logger methods
180239

181240
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)