I have written a error handling framework using ILoggerFactory
in .Net Framework 4.8
It supports multiple providers including Application Insights.
Recently I checked the Application Insights Logs and observed that none of my logs are not logging into the app insights.
public AbcExceptionLogger(string categoryName)
{
_categoryName = categoryName;
var minimumLogLevelSetting = ConfigurationManager.AppSettings["MinimumLogLevel"];
if (!Enum.TryParse(minimumLogLevelSetting, true, out _minimumLogLevel))
{
_minimumLogLevel = LogLevel.Warning; // Default to Warning if the setting is not a valid LogLevel
}
// Create a new logger factory instance
_loggerFactory = new LoggerFactory();
var loggerProviderKey = ConfigurationManager.AppSettings["LoggerProvider"];
if (loggerProviderKey == "ApplicationInsights")
{
var appInsightsConnectionString = ConfigurationManager.AppSettings["ApplicationInsights:ConnectionString"];
//if (!string.IsNullOrEmpty(appInsightsConnectionString))
//{
// IOptions<TelemetryConfiguration> telemetryConfigurationOptions = Options.Create(new TelemetryConfiguration()
// {
// ConnectionString = appInsightsConnectionString,
// TelemetryInitializers = { new AbcTelemetryInitializer() }
// });
// IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions = Options.Create(new ApplicationInsightsLoggerOptions());
// // Add the ApplicationInsightsLoggerProvider to the logger factory
// _loggerFactory.AddProvider(new ApplicationInsightsLoggerProvider(
// telemetryConfigurationOptions,
// applicationInsightsLoggerOptions));
//}
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = appInsightsConnectionString;
var telemetryInitializer = new AbcTelemetryInitializer();
telemetryConfiguration.TelemetryInitializers.Add(telemetryInitializer);
var telemetryConfigOptions = Options.Create(telemetryConfiguration);
var options = new ApplicationInsightsLoggerOptions();
var appInsightsLoggerOptions = Options.Create(options);
telemetryConfiguration.TelemetryChannel = new InMemoryChannel
{
DeveloperMode = true
};
var aiLoggerProvider = new ApplicationInsightsLoggerProvider(telemetryConfigOptions, appInsightsLoggerOptions);
_loggerFactory.AddProvider(aiLoggerProvider);
}
else if (loggerProviderKey == "Serilog")
{
// Create a new logger that enriches log events with the category name
var serilogLogger = SerilogLoggerSingleton.Instance.ForContext("CategoryName", _categoryName);
_loggerFactory.AddProvider(new Serilog.Extensions.Logging.SerilogLoggerProvider(serilogLogger));
}
else if (loggerProviderKey == "Log4Net")
{
//Add Log4Net provider to the logger factory
_loggerFactory.AddProvider(new Log4NetProvider());
}
else
{
// Add the default logger provider to the logger factory
}
// Create a new logger instance using the logger factory
_logger = _loggerFactory.CreateLogger(_categoryName);
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
}
// Create a new logger instance using the logger factory
//var logger = _loggerFactory.CreateLogger(_categoryName);
// Log the message using the logger instance
_logger.Log(logLevel, eventId, state, exception, formatter);
}
My AbcTelemetryInitializer
public class AbcTelemetryInitializer: ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var httpContext = HttpContext.Current;
if (httpContext != null)
{
var request = httpContext.Request;
// Add context data from HttpContext
telemetry.Context.GlobalProperties["HttpMethod"] = request.HttpMethod;
telemetry.Context.GlobalProperties["Url.Scheme"] = request.Url.Scheme;
telemetry.Context.GlobalProperties["Url.Host"] = request.Url.Host;
telemetry.Context.GlobalProperties["Url.AbsolutePath"] = request.Url.AbsolutePath;
telemetry.Context.GlobalProperties["Url.AbsoluteUri"] = request.Url.AbsoluteUri;
telemetry.Context.GlobalProperties["Url.Query"] = request.Url.Query;
telemetry.Context.GlobalProperties["Url.IsDefaultPort"] = request.Url.IsDefaultPort.ToString();
telemetry.Context.GlobalProperties["DomainName"] = request.Url.Host;
// Add session ID if available
var sessionId = httpContext.Session?.SessionID;
if (sessionId != null)
{
telemetry.Context.GlobalProperties["SessionId"] = sessionId;
}
//Add browser info
var browser = request.Browser;
telemetry.Context.GlobalProperties["User.Browser"] = string.Format("{0} {1}", browser.Browser, browser.Version);
telemetry.Context.GlobalProperties["User.Platform"] = browser.Platform;
telemetry.Context.GlobalProperties["User.Agent"] = request.UserAgent;
telemetry.Context.GlobalProperties["User.IsMobile"] = browser.IsMobileDevice.ToString();
}
// Add module id if available
var moduleID = ConfigurationManager.AppSettings["ModuleID"];
if (!string.IsNullOrEmpty(moduleID))
{
telemetry.Context.GlobalProperties["ModuleId"] = moduleID;
}
// Add module name if available
var moduleName = ConfigurationManager.AppSettings["ModuleName"];
if (!string.IsNullOrEmpty(moduleName))
{
telemetry.Context.GlobalProperties["ModuleName"] = moduleName;
}
// Add trace ID if available
var traceId = System.Diagnostics.Trace.CorrelationManager.ActivityId;
if (traceId != Guid.Empty)
{
telemetry.Context.GlobalProperties["TraceId"] = traceId.ToString();
}
telemetry.Context.GlobalProperties["MachineName"] = Environment.MachineName.ToString();
}
}
Need to have Tls12 in order to communicate with the ingestion end point. After setting this it worked
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;