From 863761d30a371028dee47910fb2b5ccdd8ae6abd Mon Sep 17 00:00:00 2001 From: Daniel Stolt Date: Thu, 9 Jul 2015 22:34:21 +0100 Subject: [PATCH] Improved logging in recipe components. --- .../Services/RecipeHarvester.cs | 5 +- .../Orchard.Recipes/Services/RecipeParser.cs | 94 +++++++++---------- .../Services/RecipeScheduler.cs | 9 +- .../Services/RecipeStepQueue.cs | 3 + 4 files changed, 57 insertions(+), 54 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeHarvester.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeHarvester.cs index 768261930..d177be9ef 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeHarvester.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeHarvester.cs @@ -31,20 +31,19 @@ namespace Orchard.Recipes.Services { public IEnumerable HarvestRecipes(string extensionId) { var recipes = new List(); + var extension = _extensionManager.GetExtension(extensionId); if (extension != null) { var recipeLocation = Path.Combine(extension.Location, extensionId, "Recipes"); var recipeFiles = _webSiteFolder.ListFiles(recipeLocation, true); recipeFiles.Where(r => r.EndsWith(".recipe.xml", StringComparison.OrdinalIgnoreCase)).ToList().ForEach(r => { - try { recipes.Add(_recipeParser.ParseRecipe(_webSiteFolder.ReadFile(r))); } catch (Exception ex) { - Logger.Error(new Exception(string.Format("Invalid recipe file: {0}\nError: {1}", r, ex.Message)), "Invalid recipe file: {0}\nError: {1}", r, ex.Message); + Logger.Error(ex, "Error while parsing recipe file '{0}'.", r); } - }); } else { diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeParser.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeParser.cs index 697d1f95a..beb1c00db 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeParser.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeParser.cs @@ -18,65 +18,59 @@ namespace Orchard.Recipes.Services { public Recipe ParseRecipe(string recipeText) { var recipe = new Recipe(); - - try { - if (string.IsNullOrEmpty(recipeText)) { - throw new Exception("Recipe is empty"); - } + if (string.IsNullOrEmpty(recipeText)) { + throw new Exception("Recipe is empty"); + } - XElement recipeTree = XElement.Parse(recipeText, LoadOptions.PreserveWhitespace); + XElement recipeTree = XElement.Parse(recipeText, LoadOptions.PreserveWhitespace); - var recipeSteps = new List(); + var recipeSteps = new List(); - foreach (var element in recipeTree.Elements()) { - // Recipe mETaDaTA - if (element.Name.LocalName == "Recipe") { - foreach (var metadataElement in element.Elements()) { - switch (metadataElement.Name.LocalName) { - case "Name": - recipe.Name = metadataElement.Value; - break; - case "Description": - recipe.Description = metadataElement.Value; - break; - case "Author": - recipe.Author = metadataElement.Value; - break; - case "WebSite": - recipe.WebSite = metadataElement.Value; - break; - case "Version": - recipe.Version = metadataElement.Value; - break; - case "IsSetupRecipe": - recipe.IsSetupRecipe = !string.IsNullOrEmpty(metadataElement.Value) ? bool.Parse(metadataElement.Value) : false; - break; - case "ExportUtc": - recipe.ExportUtc = !string.IsNullOrEmpty(metadataElement.Value) ? (DateTime?)XmlConvert.ToDateTime(metadataElement.Value, XmlDateTimeSerializationMode.Utc) : null; - break; - case "Tags": - recipe.Tags = metadataElement.Value; - break; - default: - Logger.Error("Unrecognized recipe metadata element {0} encountered. Skipping.", metadataElement.Name.LocalName); - break; - } + foreach (var element in recipeTree.Elements()) { + // Recipe metadata + if (element.Name.LocalName == "Recipe") { + foreach (var metadataElement in element.Elements()) { + switch (metadataElement.Name.LocalName) { + case "Name": + recipe.Name = metadataElement.Value; + break; + case "Description": + recipe.Description = metadataElement.Value; + break; + case "Author": + recipe.Author = metadataElement.Value; + break; + case "WebSite": + recipe.WebSite = metadataElement.Value; + break; + case "Version": + recipe.Version = metadataElement.Value; + break; + case "IsSetupRecipe": + recipe.IsSetupRecipe = !string.IsNullOrEmpty(metadataElement.Value) ? bool.Parse(metadataElement.Value) : false; + break; + case "ExportUtc": + recipe.ExportUtc = !string.IsNullOrEmpty(metadataElement.Value) ? (DateTime?)XmlConvert.ToDateTime(metadataElement.Value, XmlDateTimeSerializationMode.Utc) : null; + break; + case "Tags": + recipe.Tags = metadataElement.Value; + break; + default: + Logger.Warning("Unrecognized recipe metadata element '{0}' encountered; skipping.", metadataElement.Name.LocalName); + break; } } - // Recipe step - else { - var recipeStep = new RecipeStep { Name = element.Name.LocalName, Step = element }; - recipeSteps.Add(recipeStep); - } } - recipe.RecipeSteps = recipeSteps; - } - catch (Exception exception) { - Logger.Error(exception, "Parsing recipe failed. Recipe text was: {0}.", recipeText); - throw; + // Recipe step + else { + var recipeStep = new RecipeStep { Name = element.Name.LocalName, Step = element }; + recipeSteps.Add(recipeStep); + } } + recipe.RecipeSteps = recipeSteps; + return recipe; } } diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeScheduler.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeScheduler.cs index 8f12fe6df..9d9ff8427 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeScheduler.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeScheduler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Orchard.Environment.Configuration; using Orchard.Environment.Descriptor; using Orchard.Environment.State; +using Orchard.Logging; using Orchard.Recipes.Events; namespace Orchard.Recipes.Services { @@ -17,16 +18,21 @@ namespace Orchard.Recipes.Services { IProcessingEngine processingEngine, ShellSettings shellSettings, IShellDescriptorManager shellDescriptorManager, - Lazy recipeStepExecutor, IShellDescriptorManagerEventHandler events) { + Lazy recipeStepExecutor, + IShellDescriptorManagerEventHandler events) { _processingEngine = processingEngine; _shellSettings = shellSettings; _shellDescriptorManager = shellDescriptorManager; _recipeStepExecutor = recipeStepExecutor; _events = events; + Logger = NullLogger.Instance; } + public ILogger Logger; + public void ScheduleWork(string executionId) { var shellDescriptor = _shellDescriptorManager.GetShellDescriptor(); + Logger.Information("Scheduling execution of recipe {0}.", executionId); // TODO: this task entry may need to become appdata folder backed if it isn't already _processingEngine.AddTask( _shellSettings, @@ -36,6 +42,7 @@ namespace Orchard.Recipes.Services { } public void ExecuteWork(string executionId) { + Logger.Information("Executing next step of recipe {0}.", executionId); // todo: this callback should be guarded against concurrency by the IProcessingEngine var scheduleMore = _recipeStepExecutor.Value.ExecuteNextStep(executionId); if (scheduleMore) diff --git a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeStepQueue.cs b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeStepQueue.cs index 899d3ac9c..35e7df4d1 100644 --- a/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeStepQueue.cs +++ b/src/Orchard.Web/Modules/Orchard.Recipes/Services/RecipeStepQueue.cs @@ -23,6 +23,7 @@ namespace Orchard.Recipes.Services { public ILogger Logger { get; set; } public void Enqueue(string executionId, RecipeStep step) { + Logger.Information("Enqueuing step '{0}' for recipe {1}.", step.Name, executionId); var recipeStepElement = new XElement("RecipeStep"); recipeStepElement.Add(new XElement("Name", step.Name)); recipeStepElement.Add(step.Step); @@ -40,6 +41,7 @@ namespace Orchard.Recipes.Services { } public RecipeStep Dequeue(string executionId) { + Logger.Information("Dequeuing steps for recipe {0}.", executionId); if (!_appDataFolder.DirectoryExists(Path.Combine(_recipeQueueFolder, executionId))) { return null; } @@ -50,6 +52,7 @@ namespace Orchard.Recipes.Services { // string to xelement var stepElement = XElement.Parse(_appDataFolder.ReadFile(stepPath)); var stepName = stepElement.Element("Name").Value; + Logger.Information("Dequeuing step '{0}' for recipe {1}.", stepName, executionId); recipeStep = new RecipeStep { Name = stepName, Step = stepElement.Element(stepName)