Skip to content

Commit

Permalink
Support Layoutable Properties (#118)
Browse files Browse the repository at this point in the history
* Support Layoutable Properties

Make all properties layoutable as per issue #110.
FormName and ControlName of RichTextBoxTarget are still strings, see above referenced issue for more information.

* Initialize layouts properly, add missed properties.

* Render only once for form retrieval.

* Render only once for form retrieval.

* Compare layouts as strings.

Proper comparisons would be better, but makes the test pass.

* Use `RenderLogEvent` instead of `Layout.Render` where possible

* Can't cache compiled regex since it could change with layout rendering.

Co-authored-by: JTignor <[email protected]>
  • Loading branch information
Fr33dan and JTignor authored May 29, 2022
1 parent af4b0ca commit ec28cb9
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 59 deletions.
4 changes: 2 additions & 2 deletions NLog.Windows.Forms.Tests/RichTextBoxTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ public void ColoringRuleDefaults()
Assert.Equal(expectedRules.Length, actualRules.Count);
for (int i = 0; i < expectedRules.Length; ++i)
{
Assert.Equal(expectedRules[i].BackgroundColor, actualRules[i].BackgroundColor);
Assert.Equal(expectedRules[i].FontColor, actualRules[i].FontColor);
Assert.Equal(expectedRules[i].BackgroundColor.ToString(), actualRules[i].BackgroundColor.ToString());
Assert.Equal(expectedRules[i].FontColor.ToString(), actualRules[i].FontColor.ToString());
Assert.Equal(expectedRules[i].Condition.ToString(), actualRules[i].Condition.ToString());
Assert.Equal(expectedRules[i].Style, actualRules[i].Style);
}
Expand Down
21 changes: 8 additions & 13 deletions NLog.Windows.Forms/Targets/FormControlTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Forms;
using NLog.Common;
using NLog.Config;
using NLog.Layouts;
using NLog.Targets;

namespace NLog.Windows.Forms.Targets
Expand Down Expand Up @@ -46,7 +47,7 @@ public FormControlTarget()
/// </summary>
/// <docgen category='Form Options' order='10' />
[RequiredParameter]
public string ControlName { get; set; }
public Layout ControlName { get; set; }

/// <summary>
/// Gets or sets a value indicating whether log text should be appended to the text of the control instead of overwriting it. </summary>
Expand All @@ -58,7 +59,7 @@ public FormControlTarget()
/// Gets or sets the name of the Form on which the control is located.
/// </summary>
/// <docgen category='Form Options' order='10' />
public string FormName { get; set; }
public Layout FormName { get; set; }

/// <summary>
/// Gets or sets whether new log entry are added to the start or the end of the control
Expand All @@ -73,23 +74,18 @@ public FormControlTarget()
/// </param>
protected override void Write(LogEventInfo logEvent)
{
string logMessage = Layout.Render(logEvent);
string logMessage = RenderLogEvent(Layout, logEvent);

FindControlAndSendTheMessage(logMessage);
}

private void FindControlAndSendTheMessage(string logMessage)
{
Form form = null;

if (Form.ActiveForm != null)
{
form = Form.ActiveForm;
}

if (Application.OpenForms[FormName] != null)
string renderedFormName = RenderLogEvent(FormName, logEvent);
if (Application.OpenForms[renderedFormName] != null)
{
form = Application.OpenForms[FormName];
form = Application.OpenForms[renderedFormName];
}

if (form == null)
Expand All @@ -98,7 +94,7 @@ private void FindControlAndSendTheMessage(string logMessage)
return;
}

Control control = FormHelper.FindControl(ControlName, form);
Control control = FormHelper.FindControl(RenderLogEvent(ControlName, logEvent), form);

if (control == null)
{
Expand All @@ -118,7 +114,6 @@ private void FindControlAndSendTheMessage(string logMessage)
throw;
}
}

}

private void SendTheMessageToFormControl(Control control, string logMessage)
Expand Down
13 changes: 7 additions & 6 deletions NLog.Windows.Forms/Targets/RichTextBoxRowColoringRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using NLog.Conditions;
using NLog.Config;
using NLog.Layouts;

namespace NLog.Windows.Forms.Targets
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public class RichTextBoxRowColoringRule
/// </remarks>
/// <docgen category="Formatting Options" order="10"/>
[DefaultValue("Empty")]
public string FontColor { get; set; }
public Layout FontColor { get; set; }

/// <summary>
/// Gets or sets the background color.
Expand All @@ -51,7 +52,7 @@ public class RichTextBoxRowColoringRule
/// </remarks>
/// <docgen category="Formatting Options" order="10"/>
[DefaultValue("Empty")]
public string BackgroundColor { get; set; }
public Layout BackgroundColor { get; set; }

/// <summary>
/// Gets or sets the font style of matched text.
Expand Down Expand Up @@ -90,8 +91,8 @@ public RichTextBoxRowColoringRule()
public RichTextBoxRowColoringRule(string condition, string fontColor, string backColor, FontStyle fontStyle)
{
this.Condition = (ConditionExpression)condition;
this.FontColor = fontColor;
this.BackgroundColor = backColor;
this.FontColor = Layout.FromString(fontColor);
this.BackgroundColor = Layout.FromString(backColor);
this.Style = fontStyle;
}

Expand All @@ -103,8 +104,8 @@ public RichTextBoxRowColoringRule(string condition, string fontColor, string bac
public RichTextBoxRowColoringRule(string condition, string fontColor, string backColor)
{
this.Condition = (ConditionExpression)condition;
this.FontColor = fontColor;
this.BackgroundColor = backColor;
this.FontColor = Layout.FromString(fontColor);
this.BackgroundColor = Layout.FromString(backColor);
this.Style = FontStyle.Regular;
}

Expand Down
12 changes: 6 additions & 6 deletions NLog.Windows.Forms/Targets/RichTextBoxTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ protected override void Write(LogEventInfo logEvent)
}
}

string logMessage = Layout.Render(logEvent);
string logMessage = RenderLogEvent(Layout, logEvent);
RichTextBoxRowColoringRule matchingRule = FindMatchingRule(logEvent);

bool messageSent = DoSendMessageToTextbox(logMessage, matchingRule, logEvent);
Expand Down Expand Up @@ -870,22 +870,22 @@ private void SendTheMessageToRichTextBox(string logMessage, RichTextBoxRowColori

int startIndex = textBox.TextLength;
textBox.SelectionStart = startIndex;
textBox.SelectionBackColor = GetColorFromString(rule.BackgroundColor, textBox.BackColor);
textBox.SelectionColor = GetColorFromString(rule.FontColor, textBox.ForeColor);
textBox.SelectionBackColor = GetColorFromString(RenderLogEvent(rule.BackgroundColor, logEvent), textBox.BackColor);
textBox.SelectionColor = GetColorFromString(RenderLogEvent(rule.FontColor, logEvent), textBox.ForeColor);
textBox.SelectionFont = new Font(textBox.SelectionFont, textBox.SelectionFont.Style ^ rule.Style);
textBox.AppendText(logMessage + "\n");
textBox.SelectionLength = textBox.TextLength - textBox.SelectionStart;

// find word to color
foreach (RichTextBoxWordColoringRule wordRule in WordColoringRules)
{
MatchCollection matches = wordRule.CompiledRegex.Matches(textBox.Text, startIndex);
MatchCollection matches = wordRule.CompileRegex(logEvent).Matches(textBox.Text, startIndex);
foreach (Match match in matches)
{
textBox.SelectionStart = match.Index;
textBox.SelectionLength = match.Length;
textBox.SelectionBackColor = GetColorFromString(wordRule.BackgroundColor, textBox.BackColor);
textBox.SelectionColor = GetColorFromString(wordRule.FontColor, textBox.ForeColor);
textBox.SelectionBackColor = GetColorFromString(RenderLogEvent(wordRule.BackgroundColor, logEvent), textBox.BackColor);
textBox.SelectionColor = GetColorFromString(RenderLogEvent(wordRule.FontColor, logEvent), textBox.ForeColor);
textBox.SelectionFont = new Font(textBox.SelectionFont, textBox.SelectionFont.Style ^ wordRule.Style);
}
}
Expand Down
47 changes: 20 additions & 27 deletions NLog.Windows.Forms/Targets/RichTextBoxWordColoringRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using System.Text.RegularExpressions;
using NLog.Config;
using NLog.Layouts;

namespace NLog.Windows.Forms.Targets
{
Expand All @@ -12,21 +13,19 @@ namespace NLog.Windows.Forms.Targets
[NLogConfigurationItem]
public class RichTextBoxWordColoringRule
{
private Regex compiledRegex;

/// <summary>
/// Gets or sets the regular expression to be matched. You must specify either <c>text</c> or <c>regex</c>.
///
/// </summary>
/// <docgen category="Rule Matching Options" order="10"/>
public string Regex { get; set; }
public Layout Regex { get; set; }

/// <summary>
/// Gets or sets the text to be matched. You must specify either <c>text</c> or <c>regex</c>.
///
/// </summary>
/// <docgen category="Rule Matching Options" order="10"/>
public string Text { get; set; }
public Layout Text { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to match whole words only.
Expand Down Expand Up @@ -56,26 +55,20 @@ public class RichTextBoxWordColoringRule
/// Gets the compiled regular expression that matches either Text or Regex property.
///
/// </summary>
public Regex CompiledRegex
public Regex CompileRegex(LogEventInfo logEvent)
{
get
string pattern = this.Regex == null? null: this.Regex.Render(logEvent);
if (pattern == null && this.Text != null)
{
if (this.compiledRegex == null)
{
string pattern = this.Regex;
if (pattern == null && this.Text != null)
{
pattern = System.Text.RegularExpressions.Regex.Escape(this.Text);
if (this.WholeWords)
pattern = "\b" + pattern + "\b";
}
RegexOptions options = RegexOptions.Compiled;
if (this.IgnoreCase)
options |= RegexOptions.IgnoreCase;
this.compiledRegex = new Regex(pattern, options);
}
return this.compiledRegex;
pattern = System.Text.RegularExpressions.Regex.Escape(this.Text.Render(logEvent));
if (this.WholeWords)
pattern = "\b" + pattern + "\b";
}
RegexOptions options = RegexOptions.Compiled;
if (this.IgnoreCase)
options |= RegexOptions.IgnoreCase;

return new Regex(pattern, options);
}

/// <summary>
Expand All @@ -85,7 +78,7 @@ public Regex CompiledRegex
/// </summary>
/// <docgen category="Formatting Options" order="10"/>
[DefaultValue("Empty")]
public string FontColor { get; set; }
public Layout FontColor { get; set; }

/// <summary>
/// Gets or sets the background color.
Expand All @@ -94,7 +87,7 @@ public Regex CompiledRegex
/// </summary>
/// <docgen category="Formatting Options" order="10"/>
[DefaultValue("Empty")]
public string BackgroundColor { get; set; }
public Layout BackgroundColor { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="T:NLog.Targets.RichTextBoxWordColoringRule"/> class.
Expand All @@ -114,8 +107,8 @@ public RichTextBoxWordColoringRule()
public RichTextBoxWordColoringRule(string text, string fontColor, string backgroundColor)
{
this.Text = text;
this.FontColor = fontColor;
this.BackgroundColor = backgroundColor;
this.FontColor = Layout.FromString(fontColor);
this.BackgroundColor = Layout.FromString(backgroundColor);
}

/// <summary>
Expand All @@ -126,8 +119,8 @@ public RichTextBoxWordColoringRule(string text, string fontColor, string backgro
public RichTextBoxWordColoringRule(string text, string textColor, string backgroundColor, FontStyle fontStyle)
{
this.Text = text;
this.FontColor = textColor;
this.BackgroundColor = backgroundColor;
this.FontColor = Layout.FromString(textColor);
this.BackgroundColor = Layout.FromString(backgroundColor);
this.Style = fontStyle;
}
}
Expand Down
11 changes: 6 additions & 5 deletions NLog.Windows.Forms/Targets/ToolStripItemTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ToolStripItemTarget()
/// </param>
protected override void Write(LogEventInfo logEvent)
{
string logMessage = Layout.Render(logEvent);
string logMessage = RenderLogEvent(Layout, logEvent);

Form form = null;

Expand All @@ -77,9 +77,10 @@ protected override void Write(LogEventInfo logEvent)
form = Form.ActiveForm;
}

if (Application.OpenForms[FormName.Render(logEvent)] != null)
string renderedFormName = RenderLogEvent(FormName, logEvent);
if (Application.OpenForms[renderedFormName] != null)
{
form = Application.OpenForms[FormName.Render(logEvent)];
form = Application.OpenForms[renderedFormName];
}

if (form == null)
Expand All @@ -88,7 +89,7 @@ protected override void Write(LogEventInfo logEvent)
return;
}

Control control = FormHelper.FindControl(ToolStripName.Render(logEvent), form);
Control control = FormHelper.FindControl(RenderLogEvent(ToolStripName, logEvent), form);

if (control == null || !(control is ToolStrip))
{
Expand All @@ -98,7 +99,7 @@ protected override void Write(LogEventInfo logEvent)

ToolStrip toolStrip = control as ToolStrip;

ToolStripItem item = FormHelper.FindToolStripItem(ItemName.Render(logEvent), toolStrip.Items);
ToolStripItem item = FormHelper.FindToolStripItem(RenderLogEvent(ItemName, logEvent), toolStrip.Items);

if (item == null)
{
Expand Down

0 comments on commit ec28cb9

Please sign in to comment.