public static LogEvent CreateEvent(string sourceName, LogSeverity severity, EventType eventType, string format, object[] parameters) { var e = new LogEvent(severity, sourceName, DateTime.UtcNow) { EventType = eventType }; //add error Exception error = ExtractError(parameters); if (error != null) { e.AddProperty(KnownProperty.Error, error); } //enrich Enrich(e, L.Config.Enrichers); #if NETSTANDARD || NET46 Enrich(e, LogContext.Enrichers?.Values); #endif //message FormattedString fs = FormattedString.Parse(format, parameters); e.Message = fs; e.FormattedMessage = fs.ToString(); foreach (var namedParam in fs.NamedParameters) { e.AddProperty(namedParam.Key, namedParam.Value); } return(e); }
public void Parse_OriginalFormatIsPreserved() { const string template = "Hello, {name}!"; FormattedString fs = FormattedString.Parse(template, new object[] { "Alice" }); Assert.Equal(template, fs.Template); }
public void Format_NamedParameters_OnlyNamedCaptured() { FormattedString fs = FormattedString.Parse("named parameters are: {first} and {second}", new object[] { 1, 2 }); Assert.Equal(2, fs.NamedParameters.Count); Assert.Equal(1, fs.NamedParameters["first"]); Assert.Equal(2, fs.NamedParameters["second"]); }
/// <summary> /// Constructs and instance of this class /// </summary> public PoshConsoleLogWriter(string format) { _format = format == null ? TextFormatter.DefaultFormat : FormattedString.Parse(format, null); Settings = new PoshConsoleLogWriterSettings { AbbreviateClassNames = false }; Console.BackgroundColor = ConsoleColor.Black; }
/// <summary> /// Creates an instance of file receiver /// </summary> /// <param name="fileName">Target filename. If file does not exist it will be created.</param> /// <param name="format">format</param> public FileLogWriter(string fileName, string format) { if (fileName == null) { throw new ArgumentNullException(nameof(fileName)); } SplitPath(fileName, out _directoryName, out _fileNamePart, out _extensionPart); if (!Directory.Exists(_directoryName)) { Directory.CreateDirectory(_directoryName); } PreCreateDirectory(fileName); _format = format == null ? null : FormattedString.Parse(format, null); }
public static CldrRelativeTimeFormat Resolve(string locale, string type) { string key = locale + "/" + type; if (resolvedPatterns.TryGetValue(key, out CldrRelativeTimeFormat cached)) { return(cached); } CldrRelativeTimeFormat formatter = new CldrRelativeTimeFormat(locale, type); XElement field = xDocument.XPathSelectElement(String.Format("/root/fields[@locale = '{0}']/field[@type = '{1}']", locale, type)); if (field == null) { throw new InvalidOperationException("Unknown locale or type"); } if (CldrUtility.IsAlias(field, out string use)) { return(resolvedPatterns.GetOrAdd(key, Resolve(locale, use))); } bool hasInheritedValues = field.Attribute("inherits") != null; foreach (XElement relative in field.Elements("relative")) { int amount = Int32.Parse(relative.Attribute("type").Value); formatter.relative[amount] = FormattedString.Parse(relative.Value); } foreach (XElement relativeTime in field.Elements("relativeTime")) { Dictionary <PluralCategories, FormattedString> dict = relativeTime.Attribute("type").Value == "future" ? formatter.future : formatter.past; foreach (XElement child in relativeTime.Elements()) { PluralCategories category = IntlProviderOptions.ParseEnum <PluralCategories>(child.Attribute("count").Value); dict[category] = FormattedString.Parse(child.Value); } hasInheritedValues |= relativeTime.Attribute("inherits") != null; } if (hasInheritedValues) { CldrRelativeTimeFormat parent = CldrUtility.GetParentPatterns(locale, type, Resolve); CldrUtility.CopyPatternFromParent(formatter.relative, parent.relative); CldrUtility.CopyPatternFromParent(formatter.future, parent.future); CldrUtility.CopyPatternFromParent(formatter.past, parent.past); } return(resolvedPatterns.GetOrAdd(key, formatter)); }
public void Format_Variable_Variable(string format, object[] parameters, string output) { FormattedString fs = FormattedString.Parse(format, parameters); Assert.Equal(output, fs.ToString()); }
public BlobStorageLogWriter(IBlobStorage blobStorage, string documentId, string format) { _blobStorage = blobStorage ?? throw new ArgumentNullException(nameof(blobStorage)); _format = format == null ? null : FormattedString.Parse(format, null); _documentId = documentId ?? throw new ArgumentNullException(nameof(documentId)); }
/// <summary> /// Creates an instance with standard formatter /// </summary> public TraceLogWriter(string format) { _format = format == null ? null : FormattedString.Parse(format, null); }
public ReadOnlyDictionary <PluralCategories, NumberFormatPattern> GetUnitStyleFormat(string unit, UnitDisplayFormat format) { IDictionary <PluralCategories, string> pattern = GetUnitSubFormat(unit, format); if (pattern == null) { int pos = unit.IndexOf("-per-"); if (pos <= 0) { throw new ArgumentOutOfRangeException("unit"); } string nUnit = unit.Substring(0, pos); string dUnit = unit.Substring(pos + 5); string perUnitPattern = GetUnitSubFormat(unit, format, true)[PluralCategories.Other]; ReadOnlyDictionary <PluralCategories, string> nUnitPattern = GetUnitSubFormat(nUnit, format); if (perUnitPattern != null) { pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(perUnitPattern, v.Value)); } else { string compoundUnitPattern = GetUnitSubFormat("per", format)[PluralCategories.Other]; string dUnitPattern = GetUnitSubFormat(dUnit, format)[pluralRules.Match(1)]; pattern = nUnitPattern.ToDictionary(v => v.Key, v => String.Format(compoundUnitPattern, v.Value, dUnitPattern.Replace("{0}", "").Trim())); } } return(new ReadOnlyDictionary <PluralCategories, NumberFormatPattern>(pattern.ToDictionary(v => v.Key, v => new NumberFormatPattern(FormattedString.Parse(v.Value))))); }
/// <summary> /// Creates class instance /// </summary> public ConsoleLogWriter(string format, bool writeProperties) { _format = format == null ? null : FormattedString.Parse(format, null); _writeProperties = writeProperties; }