Exemple #1
0
        /// <summary>
        /// Get default file paths (including filename) for possible NLog config files.
        /// </summary>
        public IEnumerable <string> GetDefaultCandidateConfigFilePaths(string fileName)
        {
            // NLog.config from application directory
            string nlogConfigFile = fileName ?? "NLog.config";
            string baseDirectory  = PathHelpers.TrimDirectorySeparators(LogFactory.CurrentAppDomain?.BaseDirectory);

            if (!string.IsNullOrEmpty(baseDirectory))
            {
                yield return(Path.Combine(baseDirectory, nlogConfigFile));
            }

            string nLogConfigFileLowerCase           = nlogConfigFile.ToLower();
            bool   platformFileSystemCaseInsensitive = nlogConfigFile == nLogConfigFileLowerCase || PlatformDetector.IsWin32;

            if (!platformFileSystemCaseInsensitive && !string.IsNullOrEmpty(baseDirectory))
            {
                yield return(Path.Combine(baseDirectory, nLogConfigFileLowerCase));
            }

#if !SILVERLIGHT && !NETSTANDARD1_3
            var entryAssemblyLocation = PathHelpers.TrimDirectorySeparators(AssemblyHelpers.GetAssemblyFileLocation(System.Reflection.Assembly.GetEntryAssembly()));
            if (!string.IsNullOrEmpty(entryAssemblyLocation))
            {
                if (!string.Equals(entryAssemblyLocation, baseDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    yield return(Path.Combine(entryAssemblyLocation, nlogConfigFile));

                    if (!platformFileSystemCaseInsensitive)
                    {
                        yield return(Path.Combine(entryAssemblyLocation, nLogConfigFileLowerCase));
                    }
                }
            }
#endif

            if (string.IsNullOrEmpty(baseDirectory))
            {
                yield return(nlogConfigFile);

                if (!platformFileSystemCaseInsensitive)
                {
                    yield return(nLogConfigFileLowerCase);
                }
            }

            if (fileName == null)
            {
                // Current config file with .config renamed to .nlog
                string configurationFile = LogFactory.CurrentAppDomain?.ConfigurationFile;
                if (!StringHelpers.IsNullOrWhiteSpace(configurationFile))
                {
                    yield return(Path.ChangeExtension(configurationFile, ".nlog"));

                    // .nlog file based on the non-vshost version of the current config file
                    const string vshostSubStr = ".vshost.";
                    if (configurationFile.Contains(vshostSubStr))
                    {
                        yield return(Path.ChangeExtension(configurationFile.Replace(vshostSubStr, "."), ".nlog"));
                    }
                }
            }

            IEnumerable <string> privateBinPaths = LogFactory.CurrentAppDomain.PrivateBinPath;
            if (privateBinPaths != null)
            {
                foreach (var privatePath in privateBinPaths)
                {
                    var path = PathHelpers.TrimDirectorySeparators(privatePath);
                    if (!StringHelpers.IsNullOrWhiteSpace(path) && !string.Equals(path, baseDirectory, StringComparison.OrdinalIgnoreCase))
                    {
                        yield return(Path.Combine(path, nlogConfigFile));

                        if (!platformFileSystemCaseInsensitive)
                        {
                            yield return(Path.Combine(path, nLogConfigFileLowerCase));
                        }
                    }
                }
            }

#if !SILVERLIGHT && !NETSTANDARD1_0
            if (fileName == null)
            {
                // Get path to NLog.dll.nlog only if the assembly is not in the GAC
                var nlogAssembly = typeof(LogFactory).GetAssembly();
                if (!string.IsNullOrEmpty(nlogAssembly?.Location) && !nlogAssembly.GlobalAssemblyCache)
                {
                    yield return(nlogAssembly.Location + ".nlog");
                }
            }
#endif
        }
Exemple #2
0
 private static string LookupEntryAssemblyLocation()
 {
     return(AssemblyHelpers.GetAssemblyFileLocation(System.Reflection.Assembly.GetEntryAssembly()));
 }