/// <summary> /// Initializes a new instance of the <see cref="LogLevelField"/> class. /// </summary> /// <param name="formatter">The formatter the field belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> public LogLevelField(JsonMessageFormatter formatter, string jsonKey) : base(formatter, LogMessageField.LogLevelName, jsonKey) { }
/// <summary> /// Initializes a new instance of the <see cref="TextField"/> class. /// </summary> /// <param name="formatter">The formatter the field belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> public TextField(JsonMessageFormatter formatter, string jsonKey) : base(formatter, LogMessageField.Text, jsonKey) { }
public void Format_EscapingValues(JsonMessageFormatterStyle style, LogMessageField fields) { var formatter = new JsonMessageFormatter { Style = style, Newline = "\n" }; string key = ""; var message = new LogMessage(); if (fields.HasFlag(LogMessageField.LogWriterName)) { key = "LogWriter"; formatter.AddLogWriterField(key); message.LogWriterName = sUnescapedString; } if (fields.HasFlag(LogMessageField.LogLevelName)) { key = "LogLevel"; formatter.AddLogLevelField(key); message.LogLevelName = sUnescapedString; } if (fields.HasFlag(LogMessageField.ApplicationName)) { key = "ApplicationName"; formatter.AddApplicationNameField(key); message.ApplicationName = sUnescapedString; } if (fields.HasFlag(LogMessageField.ProcessName)) { key = "ProcessName"; formatter.AddProcessNameField(key); message.ProcessName = sUnescapedString; } if (fields.HasFlag(LogMessageField.Text)) { key = "Text"; formatter.AddTextField(key); message.Text = sUnescapedString; } Assert.Equal(fields, formatter.FormattedFields); // prepare regex to match output string pattern; switch (style) { case JsonMessageFormatterStyle.Compact: pattern = "^{\"(.+)\":\"(.+)\"}$"; break; case JsonMessageFormatterStyle.OneLine: pattern = "^{ \"(.+)\" : \"(.+)\" }$"; break; case JsonMessageFormatterStyle.Beautified: default: pattern = "^{\n \"(.+)\" : \"(.+)\"\n}$"; break; } var regex = new Regex(pattern); // check whether the key has been escaped properly (without escaping the solidus) formatter.EscapeSolidus = false; string output1 = formatter.Format(message); var match1 = regex.Match(output1); Assert.True(match1.Success); Assert.Equal(key, match1.Groups[1].Value); Assert.Equal(sEscapedString_WithoutSolidus, match1.Groups[2].Value); // check whether the key has been escaped properly (with escaping the solidus) formatter.EscapeSolidus = true; string output2 = formatter.Format(message); var match2 = regex.Match(output2); Assert.True(match2.Success); Assert.Equal(key, match2.Groups[1].Value); Assert.Equal(sEscapedString_WithSolidus, match2.Groups[2].Value); }
/// <summary> /// Initializes a new instance of the <see cref="LogWriterField"/> class. /// </summary> /// <param name="formatter">The formatter the field belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> public LogWriterField(JsonMessageFormatter formatter, string jsonKey) : base(formatter, LogMessageField.LogWriterName, jsonKey) { }
/// <summary> /// Initializes a new instance of the <see cref="ApplicationNameField"/> class. /// </summary> /// <param name="formatter">The formatter the field belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> public ApplicationNameField(JsonMessageFormatter formatter, string jsonKey) : base(formatter, LogMessageField.ApplicationName, jsonKey) { }
/// <summary> /// Initializes a new instance of the <see cref="TimestampField"/> class. /// </summary> /// <param name="formatter">The formatter the field belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> /// <param name="format">Timestamp format to use.</param> public TimestampField(JsonMessageFormatter formatter, string jsonKey, string format) : base(formatter, LogMessageField.Timestamp, jsonKey) { TimestampFormat = format; }
/// <summary> /// Initializes a new instance of the <see cref="ProcessIdField"/> class. /// </summary> /// <param name="formatter">The formatter the column belongs to.</param> /// <param name="jsonKey">Key of the field in the JSON document.</param> public ProcessIdField(JsonMessageFormatter formatter, string jsonKey) : base(formatter, LogMessageField.ProcessId, jsonKey) { }