public void Two_templates_with_similar_tokens_but_different_content_should_not_be_considered_equal() { var template1 = OutputTemplate.Parse("lalala{Prop:f1}{Message}lalala"); var template2 = OutputTemplate.Parse("lalala{Prop:f2}{Message}lalala"); template2.Should().NotBe(template1); }
public string Serialize() { StateTemplate temp = new StateTemplate(); foreach (Output o in Outputs) { OutputTemplate ot = new OutputTemplate(); ot.ID = o.ID; ot.Ports = o.Ports; ot.Name = o.Name; ot.IPAddress = o.IP; foreach (Fixture f in o.GetFixtures()) { ot.Fixtures.Add(FixtureTemplateFactory.createFixtureTemplate(f)); } temp.Outputs.Add(ot); } foreach (Fixture f in FixturePool) { temp.FixturePool.Add(FixtureTemplateFactory.createFixtureTemplate(f)); } temp.ActionMessage = ActionMessage; return(JsonConvert.SerializeObject(temp)); }
private static ILog CreateConsoleLog() { return(new SynchronousConsoleLog(new ConsoleLogSettings { OutputTemplate = OutputTemplate.Parse("{Timestamp} {Level} [{threadName}] L:{sourceContext} {Message}{NewLine}{Exception}") })); }
public void Two_templates_with_equivalent_tokens_should_be_considered_equal() { var template1 = OutputTemplate.Parse("lalala{Level}{Message}lalala"); var template2 = OutputTemplate.Parse("lalala{Level}{Message}lalala"); template2.Should().Be(template1); }
public void Should_successfully_parse_from_string_with_special_properties() { var template = OutputTemplate.Parse("lalala{Level}{Message}lalala"); template.Tokens.Should().HaveCount(4); template.Tokens[0].Should().BeOfType <TextToken>().Which.Text.Should().Be("lalala"); template.Tokens[1].Should().BeOfType <LevelToken>(); template.Tokens[2].Should().BeOfType <MessageToken>(); template.Tokens[3].Should().BeOfType <TextToken>().Which.Text.Should().Be("lalala"); }
public void GlobalSetup() { var outputTemplate = OutputTemplate.Parse("{Timestamp:HH:mm:ss.fff} {Level:u5} {sourceContext:w}{Message}{NewLine}{Exception}"); var log = new ConsoleLog(new ConsoleLogSettings { OutputTemplate = outputTemplate }).WithMinimumLevel(LogLevel.Debug); LogProvider.Configure(log); }
public void Should_correctly_render_events_using_provided_template() { var template = OutputTemplate.Parse("{Level} {Message}"); var @event = new LogEvent(LogLevel.Warn, DateTimeOffset.Now, "Hello, {User}!") .WithProperty("User", "Vostok"); var result = LogEventFormatter.Format(@event, template); result.Should().Be("WARN Hello, Vostok!"); Console.Out.WriteLine(result); }
private static ILog CreateFileLog([NotNull] string logName) { return(new FileLog(new FileLogSettings { FilePath = Path.Combine("logs", $"{logName}.{{RollingSuffix}}.{DateTime.Now:HH-mm-ss}.log"), RollingStrategy = new RollingStrategyOptions { Type = RollingStrategyType.ByTime, Period = RollingPeriod.Day, MaxFiles = 7, }, FileOpenMode = FileOpenMode.Append, OutputTemplate = OutputTemplate.Parse("{Timestamp} {Level} [{threadName}] L:{sourceContext} {traceId:w}{operationContext:w}{BillingTransaction:w}{BoxEvent:w}{Message}{NewLine}{Exception}") })); }
private static ILog CreateFileLog() { return(new FileLog(new FileLogSettings { Encoding = Encoding.UTF8, FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory !, "logs", $"TotalLog.{{RollingSuffix}}.{DateTime.Now:HH-mm-ss}.log"), RollingStrategy = new RollingStrategyOptions { Type = RollingStrategyType.ByTime, Period = RollingPeriod.Day, MaxFiles = 14 }, FileOpenMode = FileOpenMode.Append, OutputTemplate = OutputTemplate.Parse("{Timestamp} {Level} [{threadName}] L:{sourceContext} {traceId:w}{operationContext:w}{Message}{NewLine}{Exception}") }));
public void TestSetup() { capturedEvents = new List <LogEvent>(); registration = Substitute.For <IMuxerRegistration>(); registration.IsValid("logs/log").Returns(true); muxer = Substitute.For <IMultiFileMuxer>(); muxer.TryAdd(Arg.Any <FilePath>(), Arg.Do <LogEventInfo>(e => capturedEvents.Add(e.Event)), Arg.Any <WeakReference>()).Returns(true); muxer.Register(Arg.Any <FilePath>(), Arg.Any <FileLogSettings>(), Arg.Any <WeakReference>()).Returns(registration); settings = new FileLogSettings { FilePath = "logs/log", OutputTemplate = OutputTemplate.Parse("{Message}"), EnableFileLogSettingsCache = false }; log = new FileLog(muxer, () => settings); }
private void ctxCreateLoadTemplate_Click(object sender, EventArgs e) { SaveFileDialog saveFile = new SaveFileDialog(); saveFile.Filter = "Text | *.txt"; if (saveFile.ShowDialog() == DialogResult.OK) { MapReferences aRef = (MapReferences)projectView.SelectedNode.Tag; if (aRef.ViableInfo.TheMap.MethodParameters.Count == 0) { Store.SaveTextOutput(OutputTemplate.CreateTemplateNoParams(aRef.ViableInfo), saveFile.FileName); } else { Store.SaveTextOutput(OutputTemplate.CreateTemplateParams(aRef.ViableInfo), saveFile.FileName); } } }
private static void InitLogger() { var logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logs", "{RollingSuffix}.log"); var fileLogSettings = new FileLogSettings { FilePath = logPath, RollingStrategy = new RollingStrategyOptions { MaxFiles = 0, Type = RollingStrategyType.Hybrid, Period = RollingPeriod.Day, MaxSize = 4 * 1073741824L, }, OutputTemplate = OutputTemplate.Parse("{Timestamp:HH:mm:ss.fff} {Level:u5} {sourceContext:w}{Message}{NewLine}{Exception}") }; var fileLog = new FileLog(fileLogSettings).WithMinimumLevel(LogLevel.Info); LogProvider.Configure(fileLog); }
private static IEnumerable <LogEvent> CaptureEvents(Action <ConsoleLog> action) { var events = new List <LogEvent>(); var muxer = Substitute.For <IConsoleLogMuxer>(); muxer.TryLog(Arg.Do <LogEvent>(e => events.Add(e)), Arg.Any <ConsoleLogSettings>()).Returns(true); var muxerProvider = Substitute.For <IConsoleLogMuxerProvider>(); muxerProvider.ObtainMuxer().Returns(muxer); var log = new ConsoleLog(muxerProvider, new ConsoleLogSettings { OutputTemplate = OutputTemplate.Parse("{Message}") }); action(log); return(events); }
public override Func <LogRecord, string> BuildLambda() { OutputTemplate = OutputTemplate ?? DefaultOutputTemplate; Formats = Formats ?? new RecordFormatStore(); Colors = Colors ?? new RecordConsoleColorStore(); PropertyFilter = PropertyFilter ?? (p => true); var consoleType = typeof(Console); var instanceExpr = Expression.Parameter(typeof(LogRecord), "instance"); var blocks = new List <Expression>(); var strVars = new List <ParameterExpression>(); if (OutputTemplate.IsEmpty()) { blocks.Add( Expression.Assign( Expression.Property(null, consoleType, nameof(Console.ForegroundColor)), Expression.Constant(ConsoleColor.White) ) ); var valExpr = Expression.Call(instanceExpr, nameof(ToString), null); blocks.Add( Expression.Call(consoleType.GetMethod(nameof(Console.Write), new[] { typeof(string) }), valExpr) ); } else { var tokens = parser.Parse(OutputTemplate); var propertyInfo = default(PropertyInfo); var filteredProps = props.Where(PropertyFilter) .ToArray(); for (int i = 0; i < tokens.Length; i++) { var t = tokens[i]; var valExpr = default(Expression); var colorExpr = default(Expression); if (t.IsLiteral) { valExpr = Expression.Constant(t.Value); } else { if (Formats.ContainsKey(t.Value)) { var callFormatExpr = Expression.Constant(Formats[t.Value]); valExpr = Expression.Invoke(callFormatExpr, instanceExpr); } else if ((propertyInfo = filteredProps.FirstOrDefault(x => x.Name.Equals(t.Value, StringComparison.OrdinalIgnoreCase))) != null) { var msgPropExpr = Expression.Property(instanceExpr, propertyInfo); valExpr = propertyInfo.PropertyType == typeof(string) ? (Expression)msgPropExpr : Expression.Call(msgPropExpr, nameof(object.ToString), null); } else { valExpr = Expression.Constant(t.Key); } if (Colors.ContainsKey(t.Value)) { var callTokenColor = Expression.Constant(Colors[t.Value]); colorExpr = Expression.Invoke(callTokenColor, instanceExpr); } } colorExpr = colorExpr ?? Expression.Constant(ConsoleColor.White); blocks.Add( Expression.Assign( Expression.Property(null, consoleType, nameof(Console.ForegroundColor)), colorExpr ) ); var valVar = Expression.Parameter(typeof(string), $"tokenVar{i}"); strVars.Add(valVar); blocks.Add( Expression.Assign(valVar, valExpr) ); blocks.Add( Expression.Call(consoleType.GetMethod(nameof(Console.Write), new[] { typeof(string) }), valVar) ); } } blocks.Add( Expression.Call(consoleType, nameof(Console.ResetColor), null) ); blocks.Add( Expression.Call(consoleType, nameof(Console.WriteLine), null) ); blocks.Add( ExpressionsHelper.BuildStringConcat(strVars.ToArray()) ); var body = Expression.Block(strVars, blocks); var lambda = Expression.Lambda <Func <LogRecord, string> >(body, instanceExpr); var serializerFunc = lambda.CompileFast(); return(serializerFunc); }
private static ConsoleLogSettings GetSettings() => new ConsoleLogSettings { OutputTemplate = OutputTemplate.Parse($"{{{WellKnownTokens.Message}}}") };
/// <summary> /// Create a new instance of <see cref="FormattingStrategy"/> /// </summary> /// <param name="template"></param> public FormattingStrategy(OutputTemplate template) { OutputTemplate = template ?? throw new ArgumentNullException(nameof(template)); }
public virtual Func <TObj, string> BuildLambda() { Formats = Formats ?? new TFormatStore(); PropertyFilter = PropertyFilter ?? (p => true); var instanceExpr = Expression.Parameter(typeof(TObj), "instance"); var strExprs = new List <Expression>(); if (OutputTemplate.IsEmpty()) { strExprs.Add( Expression.Call(instanceExpr, nameof(ToString), null) ); } else { var filteredProps = props.Where(PropertyFilter) .ToArray(); var tokens = parser.Parse(OutputTemplate); foreach (var t in tokens) { var valExpr = default(Expression); var propertyInfo = default(PropertyInfo); if (t.IsLiteral) { valExpr = Expression.Constant(t.Value); } else { if (Formats.ContainsKey(t.Value)) { var callFormatExpr = Expression.Constant(Formats[t.Value]); valExpr = Expression.Invoke(callFormatExpr, instanceExpr); } else if ((propertyInfo = filteredProps.FirstOrDefault(x => x.Name.Equals(t.Value, StringComparison.OrdinalIgnoreCase))) != null) { var propertyExpr = Expression.Property(instanceExpr, propertyInfo); valExpr = propertyInfo.PropertyType == typeof(string) ? (Expression)propertyExpr : Expression.Call(propertyExpr, nameof(ToString), null); } else { valExpr = Expression.Constant(t.Key); } } strExprs.Add(valExpr); } } var body = ExpressionWizard.Spell.StringConcat(strExprs.ToArray()); var lambda = Expression.Lambda <Func <TObj, string> >(body, instanceExpr); var serializerFunc = lambda.CompileFast(); return(serializerFunc); }