Basic programming, .NET technology.

7. ASP .Net core - Fundamentals - Logs

 This series is a collection of knowledge about ASP .NET Core. It just is my notes.

Part 1: Startup file.

Part 2: DI.

Part 3: Middleware.

Part 4: Host and Servers.

Part 5: Configurations.

Part 6: Environment.

Part 8: Error Handling.

Part 9: Routing.

Part 10: Make an HTTP Request.

Part 11: Static files.

Part 12: Authentication and Authorization.

Part 13: CORS.

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

  1. Log level

LogLevelValueMethodDescription
Trace0LogTraceContain the most detailed messages. These messages may contain sensitive app data. These messages are disabled by default and should not be enabled in production.
Debug1LogDebugFor debugging and development. Use caution in production due to the high volume.
Information2LogInformationTracks the general flow of the app. May have long-term value.
Warning3LogWarningFor abnormal or unexpected events. Typically includes errors or conditions that don't cause the app to fail.
Error4LogErrorFor errors and exceptions that cannot be handled. These messages indicate a failure in the current operation or request, not an app-wide failure.
Critical5LogCriticalFor failures that require immediate attention. Examples: data loss scenarios, out of disk space.
None6
Specifies that a logging category should not write any messages.
  1. Built-in logging provider

  1. Third-party logging provider
    • NLog
    • Serilog

  1. 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:

Share:

0 nhận xét:

Đăng nhận xét

Featured Posts

Data type 3 - string type