コード例 #1
0
        /// <summary>
        /// This is a utility method to convert the values found in the LogFileSection
        /// to <see cref="LogFileSettings"/> and to substitute the appearance of a '~'
        /// for a call to <see cref="HttpServerUtility.MapPath"/> if this is run inside
        /// of a web context, or use the <see cref="Environment.CurrentDirectory"/>.
        /// </summary>
        /// <remarks>This assumes that there is &lt;logging /&gt; section in the app.config </remarks>
        /// <returns>A <see cref="LogFileSettings"/> object.</returns>
        public static LogFileSettings GetAsSettings()
        {
            LogFileSection section = (LogFileSection)ConfigurationManager.GetSection("logging");

            if (section == null)
            {
                return(null);
            }

            LogFileSettings settings = section.ToSettings();

            if (settings.DirectoryPath.Contains("~"))
            {
                string appDirectory = HostingEnvironment.ApplicationPhysicalPath ?? Environment.CurrentDirectory;
                settings.DirectoryPath = settings.DirectoryPath.Replace("~", appDirectory);
            }

            EventLogHelper.WriteInformation(settings.EventLogSource,
                                            "Writing log file to " + settings.DirectoryPath);

            return(settings);
        }