Događaj
Izgradite inteligentne aplikacije
17. mar 21 - 21. mar 10
Pridružite se seriji sastanaka kako biste izgradili skalabilna AI rešenja zasnovana na stvarnim slučajevima korišćenja sa kolegama programerima i stručnjacima.
Registrujte se odmahOvaj pregledač više nije podržan.
Nadogradite na Microsoft Edge biste iskoristili najnovije funkcije, bezbednosne ispravke i tehničku podršku.
When you're trying to generate meaningful diagnostic messages for exceptions, maintaining the inclusion of pertinent information can pose a challenge. The standard exception message often lacks critical details that accompany the exception, while invoking the Exception.ToString method yields an excess of state information.
This article relies on the Microsoft.Extensions.Diagnostics.ExceptionSummarization NuGet package.
Metric tags typically support a limited number of distinct values, and as such they are not suitable to represent values which are highly variable, such as the result of Exception.ToString(). An exception summary represents a low-cardinality version of an exception's information, suitable for such cases.
The goal of exception summarization is twofold:
The IExceptionSummarizer interface offers methods for extracting crucial details from recognized exception types, thereby furnishing a singular string
that serves as the foundation for crafting top-quality diagnostic messages.
The IExceptionSummarizer.Summarize method systematically traverses the roster of registered summarizers until it identifies a summarizer capable of handling the specific exception type. In the event that no summarizer is capable of recognizing the exception type, a meaningful default exception summary is provided instead.
The result of the Summarize
method returns an ExceptionSummary struct, and it contains the following properties:
The following example demonstrates how to use the IExceptionSummarizer
interface to retrieve a summary of an exception.
using System.Net;
using System.Net.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.ExceptionSummarization;
// Add exception summarization services.
var services = new ServiceCollection()
.AddExceptionSummarizer(static builder => builder.AddHttpProvider());
var provider = services.BuildServiceProvider();
// Get the exception summarizer.
IExceptionSummarizer summarizer = provider.GetRequiredService<IExceptionSummarizer>();
// Define exceptions to summarize.
Exception[] exceptions =
[
new OperationCanceledException("Operation cancelled..."),
new TaskCanceledException("Task cancelled..."),
new SocketException(10_024, "Too many sockets open..."),
new WebException("Keep alive failure...",
WebExceptionStatus.KeepAliveFailure)
];
foreach (var exception in exceptions)
{
// Summarize the exception.
ExceptionSummary summary = summarizer.Summarize(exception);
Console.WriteLine(summary);
}
Console.ReadLine();
The preceding code:
AddExceptionSummarizer
extension method accepts a delegate that is used to configure the ExceptionSummarizerBuilder
instance.builder
is used to add the HTTP provider, which handles exceptions of type:
ServiceProvider
instance from the ServiceCollection
instance.IExceptionSummarizer
interface from the ServiceProvider
instance.Summarize
method on each exception and displaying the result.Napomena
The primary focus in the design of all exception summarization implementations is to provide diagnostic convenience, rather than prioritizing the protection of personally identifiable information (PII). The ExceptionSummary.Description doesn't contain sensitive information, but the ExceptionSummary.AdditionalDetails might contain sensitive information depending on the implementation.
Povratne informacije za .NET
.NET je projekat otvorenog koda. Izaberite vezu da biste pružili povratne informacije:
Događaj
Izgradite inteligentne aplikacije
17. mar 21 - 21. mar 10
Pridružite se seriji sastanaka kako biste izgradili skalabilna AI rešenja zasnovana na stvarnim slučajevima korišćenja sa kolegama programerima i stručnjacima.
Registrujte se odmahObuka
Modul
Implement exception handling in C# console applications - Training
This module explores the use of exceptions and the exception handling process in C# console applications. Hands-on activities provide experience implementing exception handling patterns for various coding scenarios.