nlog配置文件的自动查找

 

https://github.com/NLog/NLog/blob/dev/src/NLog/Config/ConfigurationItemFactory.cs#L530

  internal static IEnumerable<KeyValuePair<string, Assembly>> GetAutoLoadingFileLocations()
        {
            var nlogAssembly = typeof(ILogger).GetAssembly();
            var assemblyLocation = PathHelpers.TrimDirectorySeparators(AssemblyHelpers.GetAssemblyFileLocation(nlogAssembly));
            if (!string.IsNullOrEmpty(assemblyLocation))
                yield return new KeyValuePair<string, Assembly>(assemblyLocation, nlogAssembly);

            var entryAssembly = Assembly.GetEntryAssembly();
            var entryLocation = PathHelpers.TrimDirectorySeparators(AssemblyHelpers.GetAssemblyFileLocation(Assembly.GetEntryAssembly()));
            if (!string.IsNullOrEmpty(entryLocation) && !string.Equals(entryLocation, assemblyLocation, StringComparison.OrdinalIgnoreCase))
                yield return new KeyValuePair<string, Assembly>(entryLocation, entryAssembly);

            // TODO Consider to prioritize AppDomain.PrivateBinPath
            var baseDirectory = PathHelpers.TrimDirectorySeparators(LogFactory.CurrentAppDomain.BaseDirectory);
            InternalLogger.Debug("Auto loading based on AppDomain-BaseDirectory found location: {0}", baseDirectory);
            if (!string.IsNullOrEmpty(baseDirectory) && !string.Equals(baseDirectory, assemblyLocation, StringComparison.OrdinalIgnoreCase))
                yield return new KeyValuePair<string, Assembly>(baseDirectory, null);
        }

/// <summary>
        /// Builds the default configuration item factory.
        /// </summary>
        /// <returns>Default factory.</returns>
        private static ConfigurationItemFactory BuildDefaultFactory()
        {
            var nlogAssembly = typeof(ILogger).GetAssembly();
            var factory = new ConfigurationItemFactory(LogManager.LogFactory.ServiceRepository, null, nlogAssembly);
            factory.RegisterExternalItems();

#if !NETSTANDARD1_3
            try
            {
                var assemblyLocation = string.Empty;
                var extensionDlls = ArrayHelper.Empty<string>();
                var fileLocations = GetAutoLoadingFileLocations();
                foreach (var fileLocation in fileLocations)
                {
                    if (string.IsNullOrEmpty(fileLocation.Key))
                        continue;

                    if (string.IsNullOrEmpty(assemblyLocation))
                        assemblyLocation = fileLocation.Key;

                    extensionDlls = GetNLogExtensionFiles(fileLocation.Key);
                    if (extensionDlls.Length > 0)
                    {
                        assemblyLocation = fileLocation.Key;
                        break;
                    }
                }

                InternalLogger.Debug("Start auto loading, location: {0}", assemblyLocation);
                LoadNLogExtensionAssemblies(factory, nlogAssembly, extensionDlls);
            }
            catch (System.Security.SecurityException ex)
            {
                InternalLogger.Warn(ex, "Seems that we do not have permission");
                if (ex.MustBeRethrown())
                {
                    throw;
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                InternalLogger.Warn(ex, "Seems that we do not have permission");
                if (ex.MustBeRethrown())
                {
                    throw;
                }
            }
            InternalLogger.Debug("Auto loading done");
#endif
            return factory;
        }

 

上一篇:abp vnext 微服务-基于Exceptionless实现分布式日志记录


下一篇:ASP.NET Core中使用NLog记录日志