Prefer WebApplicationBuilder properties over ConfigureLogging, ConfigureServices, etc (#24323)

pull/24321/head
Kirk Larkin 2021-12-15 18:15:30 +00:00 committed by GitHub
parent 60e33714d5
commit 79361b6fa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 52 deletions

View File

@ -11,7 +11,7 @@ uid: fundamentals/logging/index
# Logging in .NET Core and ASP.NET Core # Logging in .NET Core and ASP.NET Core
::: moniker range=">= aspnetcore-6.0" :::moniker range=">= aspnetcore-6.0"
By [Kirk Larkin](https://twitter.com/serpent5), [Juergen Gutsch](https://github.com/JuergenGutsch), and [Rick Anderson](https://twitter.com/RickAndMSFT) By [Kirk Larkin](https://twitter.com/serpent5), [Juergen Gutsch](https://github.com/JuergenGutsch), and [Rick Anderson](https://twitter.com/RickAndMSFT)
@ -893,9 +893,9 @@ To create a custom logger, see [Implement a custom logging provider in .NET](/do
* <xref:fundamentals/logging/loggermessage> * <xref:fundamentals/logging/loggermessage>
* Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo. * Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo.
* <xref:blazor/fundamentals/logging> * <xref:blazor/fundamentals/logging>
::: moniker-end :::moniker-end
::: moniker range=">= aspnetcore-3.0 < aspnetcore-6.0" :::moniker range=">= aspnetcore-3.0 < aspnetcore-6.0"
By [Kirk Larkin](https://twitter.com/serpent5), [Juergen Gutsch](https://github.com/JuergenGutsch), and [Rick Anderson](https://twitter.com/RickAndMSFT) By [Kirk Larkin](https://twitter.com/serpent5), [Juergen Gutsch](https://github.com/JuergenGutsch), and [Rick Anderson](https://twitter.com/RickAndMSFT)
@ -1798,9 +1798,9 @@ The following example shows how to register filter rules in code:
* Log level `Information` and higher. * Log level `Information` and higher.
* All categories starting with `"Microsoft"`. * All categories starting with `"Microsoft"`.
::: moniker-end :::moniker-end
::: moniker range=">= aspnetcore-5.0 < aspnetcore-6.0" :::moniker range=">= aspnetcore-5.0 < aspnetcore-6.0"
## Automatically log scope with SpanId, TraceId, and ParentId ## Automatically log scope with SpanId, TraceId, and ParentId
@ -1823,9 +1823,9 @@ The logging libraries implicitly create a scope object with `SpanId`, `TraceId`,
If the `traceparent` http request header is set, the `ParentId` in the log scope shows the W3C `parent-id` from in-bound `traceparent` header and the `SpanId` in the log scope shows the updated `parent-id` for the next out-bound step/span. For more information, see [Mutating the traceparent Field](https://www.w3.org/TR/trace-context/#mutating-the-traceparent-field). If the `traceparent` http request header is set, the `ParentId` in the log scope shows the W3C `parent-id` from in-bound `traceparent` header and the `SpanId` in the log scope shows the updated `parent-id` for the next out-bound step/span. For more information, see [Mutating the traceparent Field](https://www.w3.org/TR/trace-context/#mutating-the-traceparent-field).
::: moniker-end :::moniker-end
::: moniker range=">= aspnetcore-3.0 < aspnetcore-6.0" :::moniker range=">= aspnetcore-3.0 < aspnetcore-6.0"
## Create a custom logger ## Create a custom logger
@ -1836,4 +1836,4 @@ To create a custom logger, see [Implement a custom logging provider in .NET](/do
* <xref:fundamentals/logging/loggermessage> * <xref:fundamentals/logging/loggermessage>
* Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo. * Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo.
* <xref:blazor/fundamentals/logging> * <xref:blazor/fundamentals/logging>
::: moniker-end :::moniker-end

View File

@ -84,10 +84,8 @@ app.Run();
#elif FF #elif FF
#region snippet_FF #region snippet_FF
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureLogging(logging => builder.Logging.AddFilter((provider, category, logLevel) =>
{ {
logging.AddFilter((provider, category, logLevel) =>
{
if (provider.Contains("ConsoleLoggerProvider") if (provider.Contains("ConsoleLoggerProvider")
&& category.Contains("Controller") && category.Contains("Controller")
&& logLevel >= LogLevel.Information) && logLevel >= LogLevel.Information)
@ -104,7 +102,6 @@ builder.Host.ConfigureLogging(logging =>
{ {
return false; return false;
} }
});
}); });
#endregion #endregion
@ -133,18 +130,17 @@ app.Run();
using Microsoft.Extensions.Logging.AzureAppServices; using Microsoft.Extensions.Logging.AzureAppServices;
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics()) builder.Logging.AddAzureWebAppDiagnostics();
.ConfigureServices(serviceCollection => serviceCollection builder.Services.Configure<AzureFileLoggerOptions>(options =>
.Configure<AzureFileLoggerOptions>(options => {
{
options.FileName = "azure-diagnostics-"; options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024; options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5; options.RetainedFileCountLimit = 5;
}) });
.Configure<AzureBlobLoggerOptions>(options => builder.Services.Configure<AzureBlobLoggerOptions>(options =>
{ {
options.BlobName = "log.txt"; options.BlobName = "log.txt";
})); });
#endregion #endregion
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
@ -170,7 +166,7 @@ app.Run();
#elif MIN #elif MIN
#region snippet_MIN #region snippet_MIN
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning)); builder.Logging.SetMinimumLevel(LogLevel.Warning);
#endregion #endregion
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
@ -199,10 +195,9 @@ using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug; using Microsoft.Extensions.Logging.Debug;
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureLogging(logging => builder.Logging.AddFilter("System", LogLevel.Debug);
logging.AddFilter("System", LogLevel.Debug) builder.Logging.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information);
.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) builder.Logging.AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace);
.AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace));
#endregion #endregion
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
@ -227,16 +222,11 @@ app.MapRazorPages();
app.Run(); app.Run();
#elif WEL #elif WEL
#region snippet_WEL #region snippet_WEL
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.Logging.Debug;
var builder = WebApplication.CreateBuilder(); var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureLogging(logging => builder.Logging.AddEventLog(eventLogSettings =>
{ {
logging.AddEventLog(eventLogSettings =>
{
eventLogSettings.SourceName = "MyLogs"; eventLogSettings.SourceName = "MyLogs";
});
}); });
#endregion #endregion