I am trying to write a wrapper to abstract away logging frameworks. As part of the effort, I am trying to instantiate logging services from string names. When I try the below code to create an instance of one of the services it fails with the stated error. At the same time another service implementing the same interface and with a similar constructor works.
ILoggerService.cs
public interface ILoggerService
{
void Log(string logLevel, string state, string eventId);
}
NLogService
public class NLogService : ILoggerService
{
private NLog.Logger _logger;
private NLogNotifications _notifications;
public NLogService(Func<LoggerConfiguration> notifiers)
{
_notifications = new NLogNotifications(notifiers);
_logger = NLog.LogManager.GetCurrentClassLogger();
}
public void Log(string logLevel, string logMessage, string category)
{
_logger = NLog.LogManager.GetLogger(category);
_logger.Log(GetLogLevel(logLevel), logMessage, category);
}
private NLog.LogLevel GetLogLevel(string levelName)
{
if (levelName == "Critical") levelName = "Fatal";
return NLog.LogLevel.FromString(levelName);
}
}
Log4NetService
public class Log4NetService : ILoggerService
{
private log4net.ILog _logger;
private Log4NetNotifications _notifications;
public Log4NetService(Func<LoggerConfiguration> config)
{
_logger =
log4net.LogManager.GetLogger(typeof(Log4NetService));
BasicConfigurator.Configure();
_notifications = new Log4NetNotifications(config);
}
void ILoggerService.Log(string logLevel, string logMessage, string category)
{
_logger = log4net.LogManager.GetLogger(category);
_logger.Logger.Log(GetLogEvent(logLevel, logMessage, category));
}
private log4net.Core.LoggingEvent GetLogEvent(string logLevel, string logMessage, string category)
{
return new log4net.Core.LoggingEvent(new log4net.Core.LoggingEventData() {
Level = GetLogLevel(logLevel),
Message = logMessage,
LoggerName = category
});
}
private Level GetLogLevel(string logLevel)
{
return _logger.Logger.Repository.LevelMap[logLevel];
}
}
The code that throws the exception looks like below -->
(_name, _getCurrentConfig, Logger) = (name, getCurrentConfig,
(ILoggerService?)Activator.CreateInstance(Type.GetType(lname)?.Assembly.FullName ?? "",
Type.GetType(lname)?.FullName ?? "", new [] { getCurrentConfig })?.Unwrap());
Aucun commentaire:
Enregistrer un commentaire