Esempio n. 1
0
        /// <summary>
        /// Stops the watching.
        /// </summary>
        public void StopWatching()
        {
            lock (this)
            {
                foreach (FileSystemWatcher watcher in this.watchers)
                {
                    InternalLogger.Info("Stopping file watching for path '{0}' filter '{1}'", watcher.Path, watcher.Filter);
                    watcher.EnableRaisingEvents = false;
                    watcher.Dispose();
                }

                this.watchers.Clear();
            }
        }
Esempio n. 2
0
        protected ObjectHandleSerializer(SerializationInfo info, StreamingContext context)
        {
            Type type = null;

            try
            {
                type     = (Type)info.GetValue("wrappedtype", typeof(Type));
                _wrapped = info.GetValue("wrappedvalue", type);
            }
            catch (Exception ex)
            {
                _wrapped = string.Empty;    // Type cannot be resolved in this AppDomain
                InternalLogger.Info(ex, "ObjectHandleSerializer failed to deserialize object: {0}", type);
            }
        }
Esempio n. 3
0
        public void Watch(string fileName)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path                = Path.GetDirectoryName(fileName);
            watcher.Filter              = Path.GetFileName(fileName);
            watcher.NotifyFilter        = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size | NotifyFilters.Security | NotifyFilters.Attributes;
            watcher.Created            += new FileSystemEventHandler(this.OnWatcherChanged);
            watcher.Changed            += new FileSystemEventHandler(this.OnWatcherChanged);
            watcher.Deleted            += new FileSystemEventHandler(this.OnWatcherChanged);
            watcher.EnableRaisingEvents = true;
            InternalLogger.Info("Watching path '{0}' filter '{1}' for changes.", watcher.Path, watcher.Filter);

            lock (this)
            {
                _watchers.Add(watcher);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Load from url
        /// </summary>
        /// <param name="assemblyName">name without .dll</param>
        /// <returns></returns>
        public static Assembly LoadFromName(string assemblyName)
        {
            InternalLogger.Info("Loading assembly: {0}", assemblyName);

#if NETSTANDARD1_0 || WINDOWS_PHONE
            var name = new AssemblyName(assemblyName);
            return(Assembly.Load(name));
#elif SILVERLIGHT && !WINDOWS_PHONE
            //as embedded resource
            var      assemblyFile = assemblyName + ".dll";
            var      stream       = Application.GetResourceStream(new Uri(assemblyFile, UriKind.Relative));
            var      assemblyPart = new AssemblyPart();
            Assembly assembly     = assemblyPart.Load(stream.Stream);
            return(assembly);
#else
            Assembly assembly = Assembly.Load(assemblyName);
            return(assembly);
#endif
        }
Esempio n. 5
0
        private static bool IncludeConfigurationItem(object item, Type itemType, int level)
        {
            try
            {
                if (itemType == null)
                {
                    return(false);
                }

                if (item is NLog.LayoutRenderers.LayoutRenderer)
                {
                    return(true);
                }

                if (item is NLog.Layouts.Layout)
                {
                    return(true);
                }

                if (item is NLog.Targets.Target)
                {
                    return(true);
                }

                if (item is LoggingRule)
                {
                    return(true);
                }

                if (itemType.IsDefined(typeof(NLogConfigurationItemAttribute), true))
                {
                    return(true);
                }
            }
            catch (System.Exception ex)
            {
                InternalLogger.Info(ex, "{0}Type reflection not possible for: {1}. Maybe because of .NET Native.", new string(' ', level), item.ToString());
            }

            return(false);
        }
Esempio n. 6
0
        private static object GetConfigurationPropertyValue(object o, PropertyInfo prop, int level)
        {
            if (prop == null || prop.PropertyType == null || prop.PropertyType.IsPrimitive() || prop.PropertyType.IsEnum() || prop.PropertyType == typeof(string))
            {
                return(null);
            }

            try
            {
                if (prop.IsDefined(typeof(NLogConfigurationIgnorePropertyAttribute), true))
                {
                    return(null);
                }
            }
            catch (System.Exception ex)
            {
                InternalLogger.Info(ex, "{0}Type reflection not possible for property {1}. Maybe because of .NET Native.", new string(' ', level + 1), prop.Name);
                return(null);
            }

            return(prop.GetValue(o, null));
        }
Esempio n. 7
0
        /// <summary>
        /// Load from url
        /// </summary>
        /// <param name="assemblyFileName">file or path, including .dll</param>
        /// <param name="baseDirectory">basepath, optional</param>
        /// <returns></returns>
        public static Assembly LoadFromPath(string assemblyFileName, string baseDirectory = null)
        {
            string fullFileName = baseDirectory == null ? assemblyFileName : Path.Combine(baseDirectory, assemblyFileName);

            InternalLogger.Info("Loading assembly file: {0}", fullFileName);
#if NETSTANDARD1_5
            try
            {
                var assemblyName = System.Runtime.Loader.AssemblyLoadContext.GetAssemblyName(fullFileName);
                return(Assembly.Load(assemblyName));
            }
            catch (Exception ex)
            {
                // this doesn't usually work
                InternalLogger.Warn(ex, "Fallback to AssemblyLoadContext.Default.LoadFromAssemblyPath for file: {0}", fullFileName);
                return(System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(fullFileName));
            }
#else
            Assembly asm = Assembly.LoadFrom(fullFileName);
            return(asm);
#endif
        }
Esempio n. 8
0
        internal void Watch(string fileName)
        {
            var directory = Path.GetDirectoryName(fileName);

            if (!Directory.Exists(directory))
            {
                InternalLogger.Warn("Cannot watch {0} for changes as it doesn't exist", directory);
                return;
            }

            lock (this)
            {
                if (this.watcherMap.ContainsKey(fileName))
                {
                    return;
                }

                var watcher = new FileSystemWatcher
                {
                    Path         = directory,
                    Filter       = Path.GetFileName(fileName),
                    NotifyFilter = NotifyFilters
                };

                watcher.Created            += OnFileChanged;
                watcher.Changed            += OnFileChanged;
                watcher.Deleted            += OnFileChanged;
                watcher.Renamed            += OnFileChanged;
                watcher.Error              += OnWatcherError;
                watcher.EnableRaisingEvents = true;

                InternalLogger.Info("Watching path '{0}' filter '{1}' for changes.", watcher.Path, watcher.Filter);

                this.watcherMap.Add(fileName, watcher);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Load from url
        /// </summary>
        /// <param name="assemblyName">name without .dll</param>
        /// <returns></returns>
        public static Assembly LoadFromName(string assemblyName)
        {
            InternalLogger.Info("Loading assembly: {0}", assemblyName);

#if NETSTANDARD1_0 || WINDOWS_PHONE
            var name = new AssemblyName(assemblyName);
            return(Assembly.Load(name));
#elif SILVERLIGHT && !WINDOWS_PHONE
            //as embedded resource
            var      assemblyFile = assemblyName + ".dll";
            var      stream       = Application.GetResourceStream(new Uri(assemblyFile, UriKind.Relative));
            var      assemblyPart = new AssemblyPart();
            Assembly assembly     = assemblyPart.Load(stream.Stream);
            return(assembly);
#else
            try
            {
                Assembly assembly = Assembly.Load(assemblyName);
                return(assembly);
            }
            catch (FileNotFoundException)
            {
                var name = new AssemblyName(assemblyName);
                InternalLogger.Trace("Try find '{0}' in current domain", assemblyName);
                var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(domainAssembly => IsAssemblyMatch(name, domainAssembly.GetName()));
                if (loadedAssembly != null)
                {
                    InternalLogger.Trace("Found '{0}' in current domain", assemblyName);
                    return(loadedAssembly);
                }

                InternalLogger.Trace("Haven't found' '{0}' in current domain", assemblyName);
                throw;
            }
#endif
        }
Esempio n. 10
0
 private void StopWatching(FileSystemWatcher watcher)
 {
     InternalLogger.Info("Stopping file watching for path '{0}' filter '{1}'", watcher.Path, watcher.Filter);
     watcher.EnableRaisingEvents = false;
     watcher.Dispose();
 }
Esempio n. 11
0
        /// <remarks>ISet is not there in .net35, so using HashSet</remarks>
        private static void ScanProperties <T>(bool aggressiveSearch, List <T> result, object o, int level, HashSet <object> visitedObjects)
            where T : class
        {
            if (o == null)
            {
                return;
            }

            var type = o.GetType();

            try
            {
                if (type == null || !type.IsDefined(typeof(NLogConfigurationItemAttribute), true))
                {
                    return;
                }
            }
            catch (System.Exception ex)
            {
                InternalLogger.Info(ex, "{0}Type reflection not possible for: {1}. Maybe because of .NET Native.", new string(' ', level), o.ToString());
                return;
            }

            if (visitedObjects.Contains(o))
            {
                return;
            }

            visitedObjects.Add(o);

            if (InternalLogger.IsTraceEnabled)
            {
                InternalLogger.Trace("{0}Scanning {1} '{2}'", new string(' ', level), type.Name, o);
            }

            var t = o as T;

            if (t != null)
            {
                result.Add(t);
                if (!aggressiveSearch)
                {
                    return;
                }
            }

            foreach (PropertyInfo prop in PropertyHelper.GetAllReadableProperties(type))
            {
                if (prop == null || prop.PropertyType == null || prop.PropertyType.IsPrimitive() || prop.PropertyType.IsEnum() || prop.PropertyType == typeof(string))
                {
                    continue;
                }

                try
                {
                    if (prop.IsDefined(typeof(NLogConfigurationIgnorePropertyAttribute), true))
                    {
                        continue;
                    }
                }
                catch (System.Exception ex)
                {
                    InternalLogger.Info(ex, "{0}Type reflection not possible for property {1}. Maybe because of .NET Native.", new string(' ', level + 1), prop.Name);
                    continue;
                }

                object value = prop.GetValue(o, null);
                if (value == null)
                {
                    continue;
                }

                if (InternalLogger.IsTraceEnabled)
                {
                    InternalLogger.Trace("{0}Scanning Property {1} '{2}' {3}", new string(' ', level + 1), prop.Name, value.ToString(), prop.PropertyType.Namespace);
                }

                var list = value as IList;
                if (list != null)
                {
                    //try first icollection for syncroot
                    List <object> elements;
                    lock (list.SyncRoot)
                    {
                        elements = new List <object>(list.Count);
                        //no foreach. Even .Cast can lead to  Collection was modified after the enumerator was instantiated.
                        for (int i = 0; i < list.Count; i++)
                        {
                            var item = list[i];
                            elements.Add(item);
                        }
                    }
                    ScanPropertiesList(aggressiveSearch, result, elements, level + 1, visitedObjects);
                }
                else
                {
                    var enumerable = value as IEnumerable;
                    if (enumerable != null)
                    {
                        //new list to prevent: Collection was modified after the enumerator was instantiated.
                        var elements = enumerable as IList <object> ?? enumerable.Cast <object>().ToList();
                        //note .Cast is tread-unsafe! But at least it isn't a ICollection / IList
                        ScanPropertiesList(aggressiveSearch, result, elements, level + 1, visitedObjects);
                    }
                    else
                    {
#if NETSTANDARD
                        if (!prop.PropertyType.IsDefined(typeof(NLogConfigurationItemAttribute), true))
                        {
                            continue;   // .NET native doesn't always allow reflection of System-types (Ex. Encoding)
                        }
#endif
                        ScanProperties(aggressiveSearch, result, value, level + 1, visitedObjects);
                    }
                }
            }
        }