public void Run() { int n = int.Parse(Console.ReadLine()); IAppender[] appenders = new IAppender[n]; for (int i = 0; i < n; i++) { string[] appendersParts = Console.ReadLine() .Split(" "); string appenderType = appendersParts[0]; string layoutType = appendersParts[1]; ReportLevelEnum reportLevel = appenders.Length == 3 ? Enum.Parse <ReportLevelEnum>(appendersParts[2], true) : ReportLevelEnum.Info; ILayout layout = LayoutCreator(layoutType); IAppender appender = AppenderCreator(appenderType, layout, reportLevel); appenders[i] = appender; } string input = string.Empty; while ((input = Console.ReadLine()) != "END") { string[] tokens = input.Split('|'); string reportLevel = tokens[0]; string date = tokens[1]; string message = tokens[2]; ILogger logger = new Loggers.Logger(appenders); switch (reportLevel.ToLower()) { case "info": logger.Info(date, message); break; case "warning": logger.Warning(date, message); break; case "error": logger.Error(date, message); break; case "critical": logger.Critical(date, message); break; case "fatal": logger.Fatal(date, message); break; } } Console.WriteLine("Logger info"); foreach (var appender in appenders) { Console.WriteLine(appender.ToString()); } }
public bool Write(object target, object value, DataAccessContext context) { Dictionary <string, ItemModel> model = (Dictionary <string, ItemModel>)target; if (model == null) { return(false); } if (value == null || string.IsNullOrWhiteSpace(value.ToString())) { return(false); } if (value != null && Filter != null) { if (Filter == "sa-tags" && value.ToString().Contains("sa-tags")) { string[] tagList = value.ToString().Replace("[", "").Replace("]", "").Replace("sa-tags:", "").Split(','); List <Tag> lstTags = new List <Tag>(); foreach (var i in tagList) { if (!string.IsNullOrEmpty(i)) { Tag tag = new Tag(); tag.Name = i; tag.Available = false; lstTags.Add(tag); } } Database dbContext = Sitecore.Configuration.Factory.GetDatabase("master"); var items = dbContext.GetItem(TagRoots.TagsRoot).Axes.GetDescendants(); foreach (var tag in lstTags) { foreach (var item in items) { if (tag.Name.ToLower() == item.Name.ToLower()) { tag.ItemId = item.ID.ToString(); tag.Available = true; } } if (!tag.Available && !string.IsNullOrEmpty(tag.Name)) { TextInfo ti = CultureInfo.CurrentCulture.TextInfo; using (new SecurityDisabler()) { Item parentItem = dbContext.Items[TagRoots.TagsRoot]; TemplateItem template = dbContext.GetTemplate(Templates.Tag); string tagName = Regex.Replace(tag.Name, "[^a-zA-Z0-9_]+", "-"); tagName = tagName.Trim('-'); tagName = tagName.Trim(' '); Item newItem = parentItem.Add(tagName, template); if (newItem != null) { newItem.Editing.BeginEdit(); newItem.Appearance.DisplayName = ti.ToTitleCase(tagName); newItem.Editing.EndEdit(); } tag.ItemId = newItem.ID.ToString(); tag.Available = true; } } } string tagvalues = string.Empty; if (lstTags.Count > 0) { foreach (var tag in lstTags) { tagvalues = tagvalues + tag.ItemId + "|"; } value = tagvalues.Remove(tagvalues.Length - 1, 1); } } else if (Filter == "sa-locations" && value.ToString().Contains("sa-locations")) { string[] tagList = value.ToString().Replace("[", "").Replace("]", "").Replace("sa-locations:", "").Split(','); List <Tag> lstTags = new List <Tag>(); foreach (var i in tagList) { if (!string.IsNullOrEmpty(i)) { Tag tag = new Tag(); tag.Name = i; tag.Available = false; lstTags.Add(tag); } } Database dbContext = Sitecore.Configuration.Factory.GetDatabase("master"); var items = dbContext.GetItem(TagRoots.LocationTagsRoot).Axes.GetDescendants(); foreach (var tag in lstTags) { foreach (var item in items) { if (tag.Name.ToLower() == item.Name.ToLower()) { tag.ItemId = item.ID.ToString(); tag.Available = true; } } if (!tag.Available && !string.IsNullOrEmpty(tag.Name)) { TextInfo ti = CultureInfo.CurrentCulture.TextInfo; using (new SecurityDisabler()) { Item parentItem = dbContext.Items[TagRoots.LocationTagsRoot]; TemplateItem template = dbContext.GetTemplate(Templates.Tag); Item newItem = parentItem.Add(tag.Name.ToLower(), template); if (newItem != null) { newItem.Editing.BeginEdit(); newItem.Appearance.DisplayName = ti.ToTitleCase(tag.Name.Replace("-", " ")); newItem.Editing.EndEdit(); } tag.ItemId = newItem.ID.ToString(); tag.Available = true; } } } string tagvalues = string.Empty; if (lstTags.Count > 0) { foreach (var tag in lstTags) { tagvalues = tagvalues + tag.ItemId + "|"; } value = tagvalues.Remove(tagvalues.Length - 1, 1); } } else { Loggers.Logger log = new Loggers.Logger(); object ItemId = null; object ItemName = null; model[Language].TryGetValue("ItemID", out ItemId); model[Language].TryGetValue("ItemName", out ItemName); log.Error(string.Format("{0} - {1}, Invalid tag : {2}", ItemId, ItemName, value)); value = string.Empty; } } model[Language][Field] = value; return(true); }
public bool Write(object target, object value, DataAccessContext context) { Dictionary <string, ItemModel> model = (Dictionary <string, ItemModel>)target; if (model == null) { return(false); } if (value == null || string.IsNullOrWhiteSpace(value.ToString())) { return(false); } XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(value.ToString()); var node = xdoc.SelectSingleNode("//hero/*[1]/@type")?.Value; string imageCaption = string.Empty; string imagePath = string.Empty; if (node == "image") { XmlAttributeCollection xnode = xdoc.SelectSingleNode("//hero/*").Attributes; foreach (XmlAttribute attr in xnode) { if (attr.Name.Contains("heroimageCaption")) { imageCaption = attr.Value.ToString(); break; } } xnode = xdoc.SelectSingleNode("//hero/*[1]/image").Attributes; foreach (XmlAttribute attr in xnode) { if (attr.Name.Contains("fileReference")) { imagePath = attr.Value.ToString(); break; } } } else { value = string.Empty; } if (!string.IsNullOrEmpty(imagePath)) { value = imagePath.Remove(imagePath.Length - 4, 4); string hostname = "http://www.saudiaramco.com"; string suffix = "/_jcr_content/renditions/original"; string newsMediaFolderPath = "/SaudiAramco/News"; var tempFolder = HostingEnvironment.ApplicationPhysicalPath + "temp\\dxfmediaimport"; Directory.CreateDirectory(tempFolder); var fileName = Path.GetFileNameWithoutExtension(imagePath); //validate if filename is not in English, then rename filename with article name if (!Regex.IsMatch(fileName, "^[a-zA-Z0-9]*$")) { string itemName = xdoc.DocumentElement.Attributes["itemName"].Value; fileName = itemName; } fileName = Regex.Replace(fileName, "[^a-zA-Z0-9_]+", "-"); fileName = fileName.Trim('-'); fileName = fileName.Trim(' '); var tempFilename = Guid.NewGuid() + Path.GetExtension(imagePath); string localPath = Path.Combine(tempFolder, tempFilename); string imageUrl = hostname + imagePath + suffix; using (WebClient client = new WebClient()) { try { client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.DownloadFile(imageUrl, localPath); } catch (Exception ex) { Loggers.Logger log = new Loggers.Logger(); log.Error(string.Format("Error - Image Writer - | ImageUrl = {0} | LocalPath = {1}", imageUrl, localPath)); return(false); } } var mediaRoot = ItemIDs.MediaLibraryRoot; var mediaRootItem = MasterDb.GetItem(mediaRoot); var path = mediaRootItem.Paths.Path; var directoryName = Path.GetDirectoryName(localPath); var destination = Path.AltDirectorySeparatorChar + Combineparts(path, newsMediaFolderPath, fileName); var media = MasterDb.GetItem(destination); if (media == null) { var options = new MediaCreatorOptions { FileBased = false, Destination = destination, AlternateText = fileName }; var newMedia = MediaManager.Creator.CreateFromFile(localPath, options); value = string.Format(XmlValue, newMedia.ID); } else { value = string.Format(XmlValue, media.ID); } } // Update Image and Caption Field Value if (!string.IsNullOrEmpty(imageCaption)) { model[Language]["Caption"] = imageCaption; } else { model[Language]["Caption"] = null; } if (!string.IsNullOrEmpty(value.ToString())) { model[Language][Field] = value; } else { model[Language][Field] = null; } return(true); }