From 79361b6fa287a3828a449e4b1824640141a1de55 Mon Sep 17 00:00:00 2001 From: Kirk Larkin <6025110+serpent5@users.noreply.github.com> Date: Wed, 15 Dec 2021 18:15:30 +0000 Subject: [PATCH] Prefer WebApplicationBuilder properties over ConfigureLogging, ConfigureServices, etc (#24323) --- aspnetcore/fundamentals/logging/index.md | 16 ++-- .../index/samples/6.x/WebApp/Program.cs | 78 ++++++++----------- 2 files changed, 42 insertions(+), 52 deletions(-) diff --git a/aspnetcore/fundamentals/logging/index.md b/aspnetcore/fundamentals/logging/index.md index 8e81d4f229..5a6bbac568 100644 --- a/aspnetcore/fundamentals/logging/index.md +++ b/aspnetcore/fundamentals/logging/index.md @@ -11,7 +11,7 @@ uid: fundamentals/logging/index # 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) @@ -893,9 +893,9 @@ To create a custom logger, see [Implement a custom logging provider in .NET](/do * * Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo. * -::: 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) @@ -1798,9 +1798,9 @@ The following example shows how to register filter rules in code: * Log level `Information` and higher. * 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 @@ -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). -::: moniker-end +:::moniker-end -::: moniker range=">= aspnetcore-3.0 < aspnetcore-6.0" +:::moniker range=">= aspnetcore-3.0 < aspnetcore-6.0" ## Create a custom logger @@ -1836,4 +1836,4 @@ To create a custom logger, see [Implement a custom logging provider in .NET](/do * * Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo. * -::: moniker-end +:::moniker-end diff --git a/aspnetcore/fundamentals/logging/index/samples/6.x/WebApp/Program.cs b/aspnetcore/fundamentals/logging/index/samples/6.x/WebApp/Program.cs index e61ca0da5b..56fbe74066 100644 --- a/aspnetcore/fundamentals/logging/index/samples/6.x/WebApp/Program.cs +++ b/aspnetcore/fundamentals/logging/index/samples/6.x/WebApp/Program.cs @@ -84,27 +84,24 @@ app.Run(); #elif FF #region snippet_FF var builder = WebApplication.CreateBuilder(); -builder.Host.ConfigureLogging(logging => +builder.Logging.AddFilter((provider, category, logLevel) => { - logging.AddFilter((provider, category, logLevel) => + if (provider.Contains("ConsoleLoggerProvider") + && category.Contains("Controller") + && logLevel >= LogLevel.Information) { - if (provider.Contains("ConsoleLoggerProvider") - && category.Contains("Controller") - && logLevel >= LogLevel.Information) - { - return true; - } - else if (provider.Contains("ConsoleLoggerProvider") - && category.Contains("Microsoft") - && logLevel >= LogLevel.Information) - { - return true; - } - else - { - return false; - } - }); + return true; + } + else if (provider.Contains("ConsoleLoggerProvider") + && category.Contains("Microsoft") + && logLevel >= LogLevel.Information) + { + return true; + } + else + { + return false; + } }); #endregion @@ -133,18 +130,17 @@ app.Run(); using Microsoft.Extensions.Logging.AzureAppServices; var builder = WebApplication.CreateBuilder(); -builder.Host.ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics()) - .ConfigureServices(serviceCollection => serviceCollection - .Configure(options => - { - options.FileName = "azure-diagnostics-"; - options.FileSizeLimit = 50 * 1024; - options.RetainedFileCountLimit = 5; - }) - .Configure(options => - { - options.BlobName = "log.txt"; - })); +builder.Logging.AddAzureWebAppDiagnostics(); +builder.Services.Configure(options => +{ + options.FileName = "azure-diagnostics-"; + options.FileSizeLimit = 50 * 1024; + options.RetainedFileCountLimit = 5; +}); +builder.Services.Configure(options => +{ + options.BlobName = "log.txt"; +}); #endregion builder.Services.AddRazorPages(); @@ -170,7 +166,7 @@ app.Run(); #elif MIN #region snippet_MIN var builder = WebApplication.CreateBuilder(); -builder.Host.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning)); +builder.Logging.SetMinimumLevel(LogLevel.Warning); #endregion builder.Services.AddRazorPages(); @@ -199,10 +195,9 @@ using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.Debug; var builder = WebApplication.CreateBuilder(); -builder.Host.ConfigureLogging(logging => - logging.AddFilter("System", LogLevel.Debug) - .AddFilter("Microsoft", LogLevel.Information) - .AddFilter("Microsoft", LogLevel.Trace)); +builder.Logging.AddFilter("System", LogLevel.Debug); +builder.Logging.AddFilter("Microsoft", LogLevel.Information); +builder.Logging.AddFilter("Microsoft", LogLevel.Trace); #endregion builder.Services.AddRazorPages(); @@ -227,16 +222,11 @@ app.MapRazorPages(); app.Run(); #elif WEL #region snippet_WEL -using Microsoft.Extensions.Logging.Console; -using Microsoft.Extensions.Logging.Debug; var builder = WebApplication.CreateBuilder(); -builder.Host.ConfigureLogging(logging => +builder.Logging.AddEventLog(eventLogSettings => { - logging.AddEventLog(eventLogSettings => - { - eventLogSettings.SourceName = "MyLogs"; - }); + eventLogSettings.SourceName = "MyLogs"; }); #endregion @@ -260,4 +250,4 @@ app.UseAuthorization(); app.MapRazorPages(); app.Run(); -#endif \ No newline at end of file +#endif