Exemplo n.º 1
0
        private static Logger CreateDefaultLogger(ref LoggerCacheKey cacheKey)
        {
            cacheKey = new LoggerCacheKey(cacheKey.Name, typeof(Logger));

            var newLogger = new Logger();

            return(newLogger);
        }
Exemplo n.º 2
0
        private Logger GetLogger(LoggerCacheKey cacheKey)
        {
            lock (syncRoot)
            {
                Logger existingLogger = loggerCache.Retrieve(cacheKey);
                if (existingLogger != null)
                {
                    return(existingLogger);
                }

                Logger newLogger;

                if (cacheKey.ConcreteType != null && cacheKey.ConcreteType != typeof(Logger))
                {
                    string fullName = cacheKey.ConcreteType.FullName;
                    try
                    {
                        if (cacheKey.ConcreteType.IsStaticClass())
                        {
                            string errorMessage = $"GetLogger / GetCurrentClassLogger is '{fullName}' as loggerType can be a static class and should inherit from Logger";
                            Trace.TraceError(errorMessage);

                            newLogger = CreateDefaultLogger(ref cacheKey);
                        }
                        else
                        {
                            var instance = FactoryHelper.CreateInstance(cacheKey.ConcreteType);
                            newLogger = instance as Logger;
                            if (newLogger == null)
                            {
                                string errorMessage = $"GetLogger / GetCurrentClassLogger got '{fullName}' as loggerType which doesn't inherit from Logger";
                                Trace.TraceError(errorMessage);

                                newLogger = CreateDefaultLogger(ref cacheKey);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError($"GetLogger / GetCurrentClassLogger. Cannot create instance of type '{fullName}'. It should have an default contructor. ex:{ex}");

                        newLogger = CreateDefaultLogger(ref cacheKey);
                    }
                }
                else
                {
                    newLogger = new Logger();
                }

                if (cacheKey.ConcreteType != null)
                {
                    newLogger.Initialize(cacheKey.Name, this);
                }

                loggerCache.InsertOrUpdate(cacheKey, newLogger);
                return(newLogger);
            }
        }
Exemplo n.º 3
0
        public Logger Retrieve(LoggerCacheKey cacheKey)
        {
            WeakReference loggerReference;

            if (loggerCache.TryGetValue(cacheKey, out loggerReference))
            {
                return(loggerReference.Target as Logger);
            }

            return(null);
        }
Exemplo n.º 4
0
 public void InsertOrUpdate(LoggerCacheKey cacheKey, Logger logger)
 {
     loggerCache[cacheKey] = new WeakReference(logger);
 }