Aspnetcore.docs: [HOLD] Apply Blazor logging updates to Logging topic after rewrite

Created on 24 Apr 2020  Â·  5Comments  Â·  Source: dotnet/AspNetCore.Docs

  • Augment logging documentation for Blazor (#17980)
  • [Preview 4] Logging configuration in Blazor WASM (#17671)

@guardrex when #17820 merges, you'll probably need to apply these

But I'd like them towards the end of the file, so you'll have to add something like:

For Blazor WebAssembly, see [Create logs in Blazor WebAssembly](#clbw) in this doc.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Blazor P0 doc-bug

Most helpful comment

All that content made it.

All 5 comments

Isn't it just going to 💥 merge conflict 💥 when the logging PR goes off of draft and then be resolved there (🤞 in the GH editor we hope 🤞) ?

No need for the anchor-thingy ... the Blazor topics link to headings. I put the content below :point_down:, so it's an easy cut-'n-paste.

### Create logs in Blazor WebAssembly

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

------------- CODEcsharp
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

...

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddProvider(new CustomLoggingProvider());
------------- CODE

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

... and ...

### Create logs in Blazor

#### Blazor WebAssembly

------------- EXISTING TEXT START -------------
Configure logging in Blazor WebAssembly apps with the `WebAssemblyHostBuilder.Logging` property in `Program.Main`:

------------- CODEcsharp
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

...

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddProvider(new CustomLoggingProvider());
------------- CODE

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`.
------------- EXISTING TEXT END -------------

#### Log in Razor components

Loggers respect app startup configuration in the Razor components of Blazor WebAssembly and Blazor Server apps.

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:

------------- CODErazor
@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++;
    }
}
------------- CODE

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

------------- CODErazor
@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++;
    }
}
------------- CODE

@guardrex the only merge conflict was the date. GitHub did a mircale merge itself.

All that content made it.

WOOT! 😀

Was this page helpful?
0 / 5 - 0 ratings