Beispiel #1
0
        /// <summary>
        /// 以 xml 配置文件的方式设置 log4net 日志记录者。
        /// </summary>
        /// <param name="appBuilder">应用构造。</param>
        /// <param name="configFilePath">配置文件路径。</param>
        public static void SetLog4NetLoggerByXmlConfig(this AppBuilder appBuilder, string configFilePath)
        {
            #region 参数校验

            if (string.IsNullOrEmpty(configFilePath))
            {
                throw new StringNullOrEmptyException(nameof(configFilePath));
            }

            #endregion

            FileInfo configFileInfo = new FileInfo(configFilePath);

            //校验配置文件是否存在。
            if (!configFileInfo.Exists)
            {
                throw new System.Exception($"路径({configFilePath})的文件未被发现。");
            }

            XmlConfigurator.Configure(configFileInfo);
            List <ILog> logs = LogManager.GetCurrentLoggers().ToList();
            foreach (ILog log in logs)
            {
                appBuilder.AddLogger(log.Logger.Name, new Log4NetLogger(log));
            }
        }