diff --git a/Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs b/Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs index 811ea05f..ff17a8a2 100644 --- a/Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs +++ b/Morestachio.Extensions.Logging/MicrosoftLoggerAdapter.cs @@ -70,7 +70,7 @@ public void Log(string logLevel, string eventId, string message) _logger.Log(ParseLogLevel(logLevel), new EventId(_eventId, eventId), new LogEntry(message, null), null, FormatMessage); } - private string FormatMessage(LogEntry entry, Exception? arg2) + private string FormatMessage(LogEntry entry, Exception arg2) { if (_format != null) { diff --git a/Morestachio/Parser.cs b/Morestachio/Parser.cs index 2b64a236..8f398fa2 100644 --- a/Morestachio/Parser.cs +++ b/Morestachio/Parser.cs @@ -53,10 +53,7 @@ public static async MorestachioDocumentInfoPromise ParseWithOptionsAsync(ParserO var tokenzierContext = new TokenzierContext(new List(), parsingOptions.CultureInfo); var tokenizerResult = await Tokenizer.Tokenize(parsingOptions, tokenzierContext).ConfigureAwait(false); - parsingOptions.Logger?.LogDebug(LoggingFormatter.ParserEventId, "Template Parsed", new Dictionary() - { - {"Errors", tokenzierContext.Errors} - }); + parsingOptions.Logger?.LogError(LoggingFormatter.ParserEventId, $"Template Parsed. {string.Join("\r\n", tokenzierContext.Errors.Select(f => f.AsFormatted()))}"); //if there are any errors do not parse the template var documentInfo = new MorestachioDocumentInfo(parsingOptions, tokenzierContext.Errors.Any() ? null : Parse(tokenizerResult, parsingOptions), tokenzierContext.Errors); diff --git a/Morestachio/Parsing/ParserErrors/IMorestachioError.cs b/Morestachio/Parsing/ParserErrors/IMorestachioError.cs index 1a674819..68737e05 100644 --- a/Morestachio/Parsing/ParserErrors/IMorestachioError.cs +++ b/Morestachio/Parsing/ParserErrors/IMorestachioError.cs @@ -1,9 +1,6 @@ using System; using System.Text; -using System.Xml; -using System.Xml.Schema; using System.Xml.Serialization; -using Morestachio.Framework.Error; namespace Morestachio.Parsing.ParserErrors; @@ -33,140 +30,4 @@ public interface IMorestachioError : IXmlSerializable, ISerializable, IEquatable /// /// void Format(StringBuilder sb); -} - -/// -/// Base class for morestachio errors that support serialization -/// -public abstract class MorestachioErrorBase : IMorestachioError -{ - /// - /// Serialization constructor - /// - protected MorestachioErrorBase() - { - - } - - /// - /// Serialization constructor - /// - /// - /// - protected MorestachioErrorBase(CharacterLocationExtended location, string helpText = null) - { - Location = location; - HelpText = helpText; - } - - /// - /// - /// - /// - /// - protected MorestachioErrorBase(SerializationInfo info, StreamingContext c) - { - HelpText = info.GetString(nameof(HelpText)); - Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromBinary(info, c); - } - - /// - public XmlSchema GetSchema() - { - return null; - } - - /// - public virtual void ReadXml(XmlReader reader) - { - HelpText = reader.GetAttribute(nameof(HelpText)); - reader.ReadStartElement(); - Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromXml(reader); - - if (!reader.IsEmptyElement) - { - reader.ReadEndElement(); - } - } - - /// - public virtual void WriteXml(XmlWriter writer) - { - writer.WriteAttributeString(nameof(HelpText), HelpText); - writer.WriteStartElement(nameof(Location)); - ErrorSerializationHelper.WriteCharacterLocationExtendedFromXml(writer, Location); - writer.WriteEndElement(); - } - - /// - public virtual void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue(nameof(HelpText), HelpText); - info.AddValue(nameof(Location), Location); - ErrorSerializationHelper.WriteCharacterLocationExtendedToBinary(info, context, Location); - } - - /// - public CharacterLocationExtended Location { get; private set; } - - /// - public string HelpText { get; private set; } - - /// - public virtual Exception GetException() - { - return new IndexedParseException(Location, HelpText); - } - - /// - public virtual void Format(StringBuilder sb) - { - sb.Append(IndexedParseException.FormatMessage(HelpText, Location)); - } - - /// - public virtual bool Equals(IMorestachioError other) - { - if (ReferenceEquals(null, other)) - { - return false; - } - - if (ReferenceEquals(this, other)) - { - return true; - } - - return Location.Equals(other.Location) && HelpText == other.HelpText; - } - - /// - public override bool Equals(object obj) - { - if (ReferenceEquals(null, obj)) - { - return false; - } - - if (ReferenceEquals(this, obj)) - { - return true; - } - - if (obj.GetType() != this.GetType()) - { - return false; - } - - return Equals((IMorestachioError)obj); - } - - /// - public override int GetHashCode() - { - unchecked - { - return (Location.GetHashCode() * 397) ^ (HelpText != null ? HelpText.GetHashCode() : 0); - } - } } \ No newline at end of file diff --git a/Morestachio/Parsing/ParserErrors/MorestachioErrorBase.cs b/Morestachio/Parsing/ParserErrors/MorestachioErrorBase.cs new file mode 100644 index 00000000..28ae12fc --- /dev/null +++ b/Morestachio/Parsing/ParserErrors/MorestachioErrorBase.cs @@ -0,0 +1,142 @@ +using System.Text; +using System.Xml; +using System.Xml.Schema; +using Morestachio.Framework.Error; + +namespace Morestachio.Parsing.ParserErrors; + +/// +/// Base class for morestachio errors that support serialization +/// +public abstract class MorestachioErrorBase : IMorestachioError +{ + /// + /// Serialization constructor + /// + protected MorestachioErrorBase() + { + + } + + /// + /// Serialization constructor + /// + /// + /// + protected MorestachioErrorBase(CharacterLocationExtended location, string helpText = null) + { + Location = location; + HelpText = helpText; + } + + /// + /// + /// + /// + /// + protected MorestachioErrorBase(SerializationInfo info, StreamingContext c) + { + HelpText = info.GetString(nameof(HelpText)); + Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromBinary(info, c); + } + + /// + public XmlSchema GetSchema() + { + return null; + } + + /// + public virtual void ReadXml(XmlReader reader) + { + HelpText = reader.GetAttribute(nameof(HelpText)); + reader.ReadStartElement(); + Location = ErrorSerializationHelper.ReadCharacterLocationExtendedFromXml(reader); + + if (!reader.IsEmptyElement) + { + reader.ReadEndElement(); + } + } + + /// + public virtual void WriteXml(XmlWriter writer) + { + writer.WriteAttributeString(nameof(HelpText), HelpText); + writer.WriteStartElement(nameof(Location)); + ErrorSerializationHelper.WriteCharacterLocationExtendedFromXml(writer, Location); + writer.WriteEndElement(); + } + + /// + public virtual void GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue(nameof(HelpText), HelpText); + info.AddValue(nameof(Location), Location); + ErrorSerializationHelper.WriteCharacterLocationExtendedToBinary(info, context, Location); + } + + /// + public CharacterLocationExtended Location { get; private set; } + + /// + public string HelpText { get; private set; } + + /// + public virtual Exception GetException() + { + return new IndexedParseException(Location, HelpText); + } + + /// + public virtual void Format(StringBuilder sb) + { + sb.Append(IndexedParseException.FormatMessage(HelpText, Location)); + } + + /// + public virtual bool Equals(IMorestachioError other) + { + if (ReferenceEquals(null, other)) + { + return false; + } + + if (ReferenceEquals(this, other)) + { + return true; + } + + return Location.Equals(other.Location) && HelpText == other.HelpText; + } + + /// + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + + if (ReferenceEquals(this, obj)) + { + return true; + } + + if (obj.GetType() != this.GetType()) + { + return false; + } + + return Equals((IMorestachioError)obj); + } + + /// + public override int GetHashCode() + { + unchecked + { + return (Location.GetHashCode() * 397) ^ (HelpText != null ? HelpText.GetHashCode() : 0); + } + } +} \ No newline at end of file diff --git a/Morestachio/Parsing/ParserErrors/MorestachioErrorExtensions.cs b/Morestachio/Parsing/ParserErrors/MorestachioErrorExtensions.cs new file mode 100644 index 00000000..18124415 --- /dev/null +++ b/Morestachio/Parsing/ParserErrors/MorestachioErrorExtensions.cs @@ -0,0 +1,20 @@ +namespace Morestachio.Parsing.ParserErrors; + +/// +/// Defines extension methods for +/// +public static class MorestachioErrorExtensions +{ + /// + /// Uses + /// + /// + /// + public static string AsFormatted(this IMorestachioError error) + { + var sb = StringBuilderCache.Acquire(); + error.Format(sb); + + return StringBuilderCache.GetStringAndRelease(sb); + } +}