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
::: 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
* <xref:fundamentals/logging/loggermessage>
* Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo.
* <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)
@ -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
* <xref:fundamentals/logging/loggermessage>
* Logging bugs should be created in the [github.com/dotnet/runtime/](https://github.com/dotnet/runtime/issues) repo.
* <xref:blazor/fundamentals/logging>
::: moniker-end
:::moniker-end

View File

@ -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<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
})
.Configure<AzureBlobLoggerOptions>(options =>
{
options.BlobName = "log.txt";
}));
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
});
builder.Services.Configure<AzureBlobLoggerOptions>(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<DebugLoggerProvider>("Microsoft", LogLevel.Information)
.AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace));
builder.Logging.AddFilter("System", LogLevel.Debug);
builder.Logging.AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information);
builder.Logging.AddFilter<ConsoleLoggerProvider>("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
#endif