This series is a collection of knowledge about ASP .NET Core. It just is my notes.
Part 10: Make an HTTP Request.
Part 12: Authentication and Authorization.
The log is one of the approaches to troubleshoot the application. So, Don't forget to implement the logs for your application.
Which kind of log provider is best: built-in or third party???
If your app integrates with Azure or deploys on Azure => use Application Insight
If your app deploys on-premise => use a structured log third party, for example, NLog
- Log level
| LogLevel | Value | Method | Description |
|---|---|---|---|
| Trace | 0 | LogTrace | Contain the most detailed messages. These messages may contain sensitive app data. These messages are disabled by default and should not be enabled in production. |
| Debug | 1 | LogDebug | For debugging and development. Use caution in production due to the high volume. |
| Information | 2 | LogInformation | Tracks the general flow of the app. May have long-term value. |
| Warning | 3 | LogWarning | For abnormal or unexpected events. Typically includes errors or conditions that don't cause the app to fail. |
| Error | 4 | LogError | For errors and exceptions that cannot be handled. These messages indicate a failure in the current operation or request, not an app-wide failure. |
| Critical | 5 | LogCritical | For failures that require immediate attention. Examples: data loss scenarios, out of disk space. |
| None | 6 | Specifies that a logging category should not write any messages. |
- Built-in logging provider
- Third-party logging provider
- NLog
- Serilog
- How to use AppInsight and Telemetry
a. step 1: Enable .net core API use AppInsight
- Create an AppInsight resource via Azure Portal.
- Get the information of instrumentation key or connection string
b. step 2: Configure something of AppInsight in .net core API
- Add Nuget package
- Microsoft.ApplicationInsights.AspNetCore
- ConfigureService
public void ConfigureServices(IServiceCollection services)
{
...
services.AddApplicationInsightsTelemetry(Configuration["ApplicationInsights:ConnectionString"]);
}
- Important: some Azure region requires a connection string instead of Instrumentation key
- Note: You can also configure AppInsight following these recommendations
It's best practice to have an extension method to do the configuration, see:
public static IServiceCollection AddTelemetry(this IServiceCollection services, IConfiguration configuration)
{
services.AddApplicationInsightsTelemetry(options =>
{
var appSettings =configuration.GetAppSettings();
options.InstrumentationKey = appSettings.AIKey;
//we want to know when the worker stops
options.EnableHeartbeat = true;
options.EnableAdaptiveSampling = false;
});
services.AddTelemetryService(configuration.GetAppSettings());
return services;
}
c. step 3 raise custom metric in AppInsight
- Use TelemetryClient
TelemetryClient _telemetryClient;
...
[HttpGet]
public string Get()
{
_telemetryClient.TrackMetric("CustomMetricExample22", 1, new Dictionary<string, string> { { "Value", "22" } });
...
}
- The best practice is to create a wrapper class for TelemetryClient
References:



0 nhận xét:
Đăng nhận xét