From 6217c0d533cdd46fcbe9122f62b6d64eb6a4ad27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Gergely?= Date: Fri, 13 Mar 2015 13:46:06 +0100 Subject: [PATCH] Refactor logging - Add some type safety instead of magic strings, and flush after receiving error message to prevent log data loss on crashing on uncaught exceptions --- src/Pretzel.Logic/Extensions/Logger.cs | 14 ++++++++++---- src/Pretzel.Logic/Extensions/Tracing.cs | 12 +++++++++--- .../Templating/Razor/RazorSiteEngine.cs | 8 +++++--- src/Pretzel.Tests/Import/BloggerImportTests.cs | 4 ++-- .../Import/HtmlToMarkdownConverterTests.cs | 2 +- src/Pretzel.Tests/Recipe/RecipeTests.cs | 4 ++-- .../Context/SiteContextGeneratorTests.cs | 12 ++++++------ src/Pretzel/Program.cs | 6 +++--- 8 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Pretzel.Logic/Extensions/Logger.cs b/src/Pretzel.Logic/Extensions/Logger.cs index b8c712ad6..c2a6aa153 100644 --- a/src/Pretzel.Logic/Extensions/Logger.cs +++ b/src/Pretzel.Logic/Extensions/Logger.cs @@ -5,12 +5,12 @@ namespace Pretzel.Logic.Extensions { public class Logger { - private readonly List categories; + private readonly List categories; private TextWriter writer; public Logger() { - categories = new List(); + categories = new List(); } public void SetWriter(TextWriter textWriter) @@ -18,17 +18,23 @@ public void SetWriter(TextWriter textWriter) writer = textWriter; } - public void AddCategory(string category) + public void AddCategory(Tracing.Category category) { categories.Add(category); } - public void Write(string message, string category) + public void Write(string message, Tracing.Category category) { if (writer == null) return; if (categories.Contains(category)) + { writer.WriteLine(message); + if (category == Tracing.Category.Error) + { + writer.Flush(); + } + } } } } \ No newline at end of file diff --git a/src/Pretzel.Logic/Extensions/Tracing.cs b/src/Pretzel.Logic/Extensions/Tracing.cs index 4c2d181a3..8b08f2376 100644 --- a/src/Pretzel.Logic/Extensions/Tracing.cs +++ b/src/Pretzel.Logic/Extensions/Tracing.cs @@ -4,6 +4,12 @@ namespace Pretzel.Logic.Extensions { public static class Tracing { + public enum Category { + Debug, + Info, + Error + } + static Tracing() { Logger = new Logger(); @@ -13,17 +19,17 @@ static Tracing() public static void Debug(string message) { - Logger.Write(message, "debug"); + Logger.Write(message, Category.Debug); } public static void Info(string message) { - Logger.Write(message, "info"); + Logger.Write(message, Category.Info); } public static void Error(string message) { - Logger.Write(message, "error"); + Logger.Write(message, Category.Error); } } } diff --git a/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs b/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs index d36cb7864..84d7feea4 100644 --- a/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs +++ b/src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs @@ -7,6 +7,7 @@ using System.ComponentModel.Composition; using System.IO; using System.Text.RegularExpressions; +using Pretzel.Logic.Extensions; namespace Pretzel.Logic.Templating.Razor { @@ -48,10 +49,11 @@ protected override string RenderTemplate(string content, PageContext pageData) { return Engine.Razor.RunCompile(content, pageData.Page.File, typeof(PageContext), pageData); } - catch (Exception ex) + catch (Exception e) { - Tracing.Debug(ex.Message + Environment.NewLine + ex.StackTrace); - Console.WriteLine(@"Failed to render template, falling back to direct content"); + Tracing.Error(@"Failed to render template, falling back to direct content"); + Tracing.Debug(e.Message); + Tracing.Debug(e.StackTrace); return content; } } diff --git a/src/Pretzel.Tests/Import/BloggerImportTests.cs b/src/Pretzel.Tests/Import/BloggerImportTests.cs index ca3c86ae8..8e1e4daa4 100644 --- a/src/Pretzel.Tests/Import/BloggerImportTests.cs +++ b/src/Pretzel.Tests/Import/BloggerImportTests.cs @@ -116,8 +116,8 @@ public void Error_on_write_is_traced() StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("debug"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Debug); var fileSubstitute = Substitute.For(); fileSubstitute.ReadAllText(ImportFile).Returns(ImportContent); diff --git a/src/Pretzel.Tests/Import/HtmlToMarkdownConverterTests.cs b/src/Pretzel.Tests/Import/HtmlToMarkdownConverterTests.cs index c31bbb943..e06416b32 100644 --- a/src/Pretzel.Tests/Import/HtmlToMarkdownConverterTests.cs +++ b/src/Pretzel.Tests/Import/HtmlToMarkdownConverterTests.cs @@ -151,7 +151,7 @@ public void Unknown_node_is_traced_and_child_processed() StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); + Tracing.Logger.AddCategory(Tracing.Category.Info); // act string markdown = converter.Convert("hello
world"); diff --git a/src/Pretzel.Tests/Recipe/RecipeTests.cs b/src/Pretzel.Tests/Recipe/RecipeTests.cs index c96d12dd5..68ae10d42 100644 --- a/src/Pretzel.Tests/Recipe/RecipeTests.cs +++ b/src/Pretzel.Tests/Recipe/RecipeTests.cs @@ -26,8 +26,8 @@ public RecipeTests() { writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("error"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Error); } [Fact] diff --git a/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs b/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs index 7b631e91e..d5af802fc 100644 --- a/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs +++ b/src/Pretzel.Tests/Templating/Context/SiteContextGeneratorTests.cs @@ -893,8 +893,8 @@ public void page_with_false_date_is_not_processed() StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("debug"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Error); // act var siteContext = generator.BuildContext(@"C:\TestSite", @"C:\TestSite\_site", false); @@ -930,8 +930,8 @@ public void render_with_ContentTransformer_exception_should_trace_the_error() StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("debug"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Error); var contentTransformer = Substitute.For(); contentTransformer.Transform(Arg.Any()).Returns(s => { throw new Exception("foo bar"); }); @@ -1027,8 +1027,8 @@ public void file_with_2_ioexception_on_ReadAllText_is_not_processed_and_exceptio StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); Tracing.Logger.SetWriter(writer); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("debug"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Error); var generator = new SiteContextGenerator(fileSystemSubstitute, Enumerable.Empty(), new LinkHelper()); diff --git a/src/Pretzel/Program.cs b/src/Pretzel/Program.cs index 2cfd48b92..1f192dccb 100644 --- a/src/Pretzel/Program.cs +++ b/src/Pretzel/Program.cs @@ -23,14 +23,14 @@ internal class Program private static void Main(string[] args) { Tracing.Logger.SetWriter(Console.Out); - Tracing.Logger.AddCategory("info"); - Tracing.Logger.AddCategory("error"); + Tracing.Logger.AddCategory(Tracing.Category.Info); + Tracing.Logger.AddCategory(Tracing.Category.Error); var parameters = BaseParameters.Parse(args, new FileSystem()); if (parameters.Debug) { - Tracing.Logger.AddCategory("debug"); + Tracing.Logger.AddCategory(Tracing.Category.Debug); } var program = new Program();