Azure Logging TelemetryClient vs ILogger
All articles from this blog can ONLY be redistributed on an Attribution-NonCommercial-NoDerivs basis. Please credit the source, thank you.
Twitter:@kelvinshen
Blog:Kelvin Shen's Blog
Summary
TelemetryClient is more feature rich comparing to ILogger. However, it depends on Microsoft Azure Application Insights. So, your logging code will not work on other competing cloud platforms.
So rule of thumb is to use ILogger by default. However, if you believe the subject application will stay with Azure and Application Insights forever, Application Insights can be a good option.
Start with End in Sight
How are you going to the information you want from large loads of logging data?
Search will be the answer and custom dimensions will make your query a lot more percise. Useful dimensions are: Exception Type, Exception, Inner Exception, Method, Class Type, Source File Path.
Technical: Libraries
Logs with Microsoft.Extensions.Logging.ILogger Logs using Microsoft.ApplicationInsights.TelemetryClient
Technical: Dependency Injection
First of all, you need to add the Logger class in the DI container in the Program.cs file.
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices((context, services) =>
{
services.AddLogging(); // Adding logger into DI container
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
})
.Build();
host.Run();
Then you can instantiate the ILogger object in library classes.
private readonly ILogger<CrmClient> _logger;
public CrmClient(IConfiguration config,
ILogger<CrmClient> logger)
{
_logger = logger;
}
Technical: Extension Methods for ILogger
public static class LoggerExtensions
{
public static void LogInfo(this ILogger logger, string reason, int calloutId, int? companyId = null, int? customerId = null,
[CallerMemberName] string method = "",
[CallerFilePath] string srcFilePath = "",
[CallerLineNumber] int srcLineNumber = 0)
{
logger.LogInformation("{reason}, {calloutId}, {companyId}, {customerId}, {method}, {srcFilePath}, {srcLineNumber}",
reason, calloutId, companyId, customerId, method, srcFilePath, srcLineNumber);
}
public static void LogWarn(this ILogger logger, string reason, int calloutId, int? companyId = null, int? customerId = null,
[CallerMemberName] string method = "",
[CallerFilePath] string srcFilePath = "",
[CallerLineNumber] int srcLineNumber = 0)
{
logger.LogWarning("{reason}, {calloutId}, {companyId}, {customerId}, {method}, {srcFilePath}, {srcLineNumber}",
reason, calloutId, companyId, customerId, method, srcFilePath, srcLineNumber);
}
public static void LogErr(this ILogger logger, Exception ex, string reason, int calloutId, int? companyId = null, int? customerId = null,
[CallerMemberName] string method = "",
[CallerFilePath] string srcFilePath = "",
[CallerLineNumber] int srcLineNumber = 0)
{
logger.LogError("{exType}, {reason}, {calloutId}, {companyId}, {customerId}, {method}, {srcFilePath}, {srcLineNumber}, {exDetails}",
ex.GetType().Name, reason, calloutId, companyId, customerId, method, srcFilePath, srcLineNumber, ex.ToString());
}
}
Source: https://hackernoon.com/fixing-logging-issues-in-aspnet-telemetryclient-vs-ilogger-sev339a
Technical: Gochya(s)
ILogger using ToString() method so complex type will be logged as its fully qualified name ONLY. The workaround is to ToJson() method for serialization before feeding it to ILogger.
References
Fixing Logging Issues In ASP.NET: TelemetryClient Vs. ILogger