Skip to content

Commit cfd716c

Browse files
authored
RichTextBoxWordColoringRule - WholeWords + IgnoreCase as typed Layout (#129)
* FormControlTarget - Append + ReverseOrder using typed Layout * RichTextBoxWordColoringRule - WholeWords + IgnoreCase as typed Layout
1 parent c8687a8 commit cfd716c

File tree

6 files changed

+39
-53
lines changed

6 files changed

+39
-53
lines changed

NLog.Windows.Forms/Targets/FormControlTarget.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.ComponentModel;
32
using System.Windows.Forms;
43
using NLog.Common;
54
using NLog.Config;
@@ -40,7 +39,7 @@ public FormControlTarget()
4039
Append = true;
4140
}
4241

43-
private delegate void DelSendTheMessageToFormControl(Control control, string logMessage);
42+
private delegate void DelSendTheMessageToFormControl(Control control, string logMessage, bool append, bool reverseOrder);
4443

4544
/// <summary>
4645
/// Gets or sets the name of control to which NLog will log write log text.
@@ -52,8 +51,7 @@ public FormControlTarget()
5251
/// <summary>
5352
/// Gets or sets a value indicating whether log text should be appended to the text of the control instead of overwriting it. </summary>
5453
/// <docgen category='Form Options' order='10' />
55-
[DefaultValue(true)]
56-
public bool Append { get; set; }
54+
public Layout<bool> Append { get; set; }
5755

5856
/// <summary>
5957
/// Gets or sets the name of the Form on which the control is located.
@@ -64,7 +62,7 @@ public FormControlTarget()
6462
/// <summary>
6563
/// Gets or sets whether new log entry are added to the start or the end of the control
6664
/// </summary>
67-
public bool ReverseOrder { get; set; }
65+
public Layout<bool> ReverseOrder { get; set; }
6866

6967
/// <summary>
7068
/// Log message to control.
@@ -82,6 +80,7 @@ protected override void Write(LogEventInfo logEvent)
8280
{
8381
form = Form.ActiveForm;
8482
}
83+
8584
string renderedFormName = RenderLogEvent(FormName, logEvent);
8685
if (Application.OpenForms[renderedFormName] != null)
8786
{
@@ -95,19 +94,21 @@ protected override void Write(LogEventInfo logEvent)
9594
}
9695

9796
Control control = FormHelper.FindControl(RenderLogEvent(ControlName, logEvent), form);
98-
9997
if (control == null)
10098
{
10199
InternalLogger.Info("Control {0} on Form {1} not found", ControlName, FormName);
102100
return;
103101
}
102+
104103
try
105104
{
106-
control.BeginInvoke(new DelSendTheMessageToFormControl(SendTheMessageToFormControl), control, logMessage);
105+
bool append = RenderLogEvent(Append, logEvent);
106+
bool reverseOrder = RenderLogEvent(ReverseOrder, logEvent);
107+
control.BeginInvoke(new DelSendTheMessageToFormControl(SendTheMessageToFormControl), control, logMessage, append, reverseOrder);
107108
}
108109
catch (Exception ex)
109110
{
110-
InternalLogger.Warn(ex.ToString());
111+
InternalLogger.Warn(ex, "Failed to assign Control.Text");
111112

112113
if (LogManager.ThrowExceptions)
113114
{
@@ -116,13 +117,13 @@ protected override void Write(LogEventInfo logEvent)
116117
}
117118
}
118119

119-
private void SendTheMessageToFormControl(Control control, string logMessage)
120+
private void SendTheMessageToFormControl(Control control, string logMessage, bool append, bool reverseOrder)
120121
{
121122
//append of replace?
122-
if (Append)
123+
if (append)
123124
{
124125
//beginning or end?
125-
if (ReverseOrder)
126+
if (reverseOrder)
126127
control.Text = logMessage + control.Text;
127128
else
128129
control.Text += logMessage;

NLog.Windows.Forms/Targets/MessageBoxTarget.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ protected override void Write(LogEventInfo logEvent)
7070
}
7171
catch (Exception ex)
7272
{
73-
InternalLogger.Warn(ex.ToString());
73+
InternalLogger.Warn(ex, "Failed MessageBox.Show");
7474

7575
if (LogManager.ThrowExceptions)
7676
{
7777
throw;
7878
}
7979
}
80-
8180
}
8281

8382
/// <summary>

NLog.Windows.Forms/Targets/RichTextBoxRowColoringRule.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel;
2-
using System.Drawing;
1+
using System.Drawing;
32
using NLog.Conditions;
43
using NLog.Config;
54
using NLog.Layouts;
@@ -38,7 +37,6 @@ public class RichTextBoxRowColoringRule
3837
///
3938
/// </remarks>
4039
/// <docgen category="Formatting Options" order="10"/>
41-
[DefaultValue("Empty")]
4240
public Layout FontColor { get; set; }
4341

4442
/// <summary>
@@ -51,7 +49,6 @@ public class RichTextBoxRowColoringRule
5149
///
5250
/// </remarks>
5351
/// <docgen category="Formatting Options" order="10"/>
54-
[DefaultValue("Empty")]
5552
public Layout BackgroundColor { get; set; }
5653

5754
/// <summary>

NLog.Windows.Forms/Targets/RichTextBoxTarget.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
4-
using System.ComponentModel;
54
using System.Drawing;
65
using System.Linq;
76
using System.Text.RegularExpressions;
@@ -213,7 +212,6 @@ public RichTextBoxTarget()
213212
/// Gets or sets a value indicating whether to use default coloring rules.
214213
/// </summary>
215214
/// <docgen category='Highlighting Options' order='10' />
216-
[DefaultValue(false)]
217215
public bool UseDefaultRowColoringRules { get; set; }
218216

219217
/// <summary>
@@ -238,7 +236,6 @@ public RichTextBoxTarget()
238236
/// Tool windows have thin border, and do not show up in the task bar.
239237
/// </remarks>
240238
/// <docgen category='Form Options' order='10' />
241-
[DefaultValue(true)]
242239
public bool ToolWindow { get; set; }
243240

244241
/// <summary>
@@ -305,7 +302,6 @@ public RichTextBoxTarget()
305302
/// If set to false and the control was not found during target initialization, the target would skip events until the control is found during <see cref="ReInitializeAllTextboxes(System.Windows.Forms.Form)"/> call
306303
/// </remarks>
307304
/// <docgen category='Form Options' order='10' />
308-
[DefaultValue(true)]
309305
public bool AllowAccessoryFormCreation { get; set; }
310306

311307

@@ -315,7 +311,6 @@ public RichTextBoxTarget()
315311
/// <remarks>
316312
/// </remarks>
317313
/// <docgen category='Form Options' order='10' />
318-
[DefaultValue(RichTextBoxTargetMessageRetentionStrategy.None)]
319314
public RichTextBoxTargetMessageRetentionStrategy MessageRetention
320315
{
321316
get { return messageRetention; }
@@ -364,13 +359,10 @@ public RichTextBoxTargetMessageRetentionStrategy MessageRetention
364359
/// </summary>
365360
private volatile Queue<MessageInfo> messageQueue;
366361

367-
368-
369362
/// <summary>
370363
/// If set to true, using "rtb-link" renderer (<see cref="RichTextBoxLinkLayoutRenderer"/>) would create clickable links in the control.
371364
/// <seealso cref="LinkClicked"/>
372365
/// </summary>
373-
[DefaultValue(false)]
374366
public bool SupportLinks
375367
{
376368
get { return supportLinks; }
@@ -711,7 +703,7 @@ private void DetachFromControl()
711703
}
712704
catch (Exception ex)
713705
{
714-
InternalLogger.Warn(ex.ToString());
706+
InternalLogger.Warn(ex, "Failed DetachFromControl");
715707

716708
if (LogManager.ThrowExceptions)
717709
{
@@ -811,7 +803,7 @@ private bool DoSendMessageToTextbox(string logMessage, RichTextBoxRowColoringRul
811803
}
812804
catch (Exception ex)
813805
{
814-
InternalLogger.Warn(ex.ToString());
806+
InternalLogger.Warn(ex, "Failed to append RichTextBox");
815807

816808
if (LogManager.ThrowExceptions)
817809
{
@@ -879,7 +871,12 @@ private void SendTheMessageToRichTextBox(string logMessage, RichTextBoxRowColori
879871
// find word to color
880872
foreach (RichTextBoxWordColoringRule wordRule in WordColoringRules)
881873
{
882-
MatchCollection matches = wordRule.CompileRegex(logEvent).Matches(textBox.Text, startIndex);
874+
var wordRulePattern = RenderLogEvent(wordRule.Regex, logEvent);
875+
var wordRuleText = RenderLogEvent(wordRule.Text, logEvent);
876+
var wordRuleWholeWords = RenderLogEvent(wordRule.WholeWords, logEvent);
877+
var wordRuleIgnoreCase = RenderLogEvent(wordRule.IgnoreCase, logEvent);
878+
879+
MatchCollection matches = wordRule.ResolveRegEx(wordRulePattern, wordRuleText, wordRuleWholeWords, wordRuleIgnoreCase).Matches(textBox.Text, startIndex);
883880
foreach (Match match in matches)
884881
{
885882
textBox.SelectionStart = match.Index;
@@ -993,7 +990,7 @@ private void StoreMessage(string logMessage, RichTextBoxRowColoringRule rule, Lo
993990
}
994991
}
995992

996-
private class MessageInfo
993+
private sealed class MessageInfo
997994
{
998995
internal string Message { get; private set; }
999996
internal RichTextBoxRowColoringRule Rule { get; private set; }

NLog.Windows.Forms/Targets/RichTextBoxWordColoringRule.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel;
2-
using System.Drawing;
1+
using System.Drawing;
32
using System.Text.RegularExpressions;
43
using NLog.Config;
54
using NLog.Layouts;
@@ -32,16 +31,14 @@ public class RichTextBoxWordColoringRule
3231
///
3332
/// </summary>
3433
/// <docgen category="Rule Matching Options" order="10"/>
35-
[DefaultValue(false)]
36-
public bool WholeWords { get; set; }
34+
public Layout<bool> WholeWords { get; set; }
3735

3836
/// <summary>
3937
/// Gets or sets a value indicating whether to ignore case when comparing texts.
4038
///
4139
/// </summary>
4240
/// <docgen category="Rule Matching Options" order="10"/>
43-
[DefaultValue(false)]
44-
public bool IgnoreCase { get; set; }
41+
public Layout<bool> IgnoreCase { get; set; }
4542

4643
/// <summary>
4744
/// Gets or sets the font style of matched text.
@@ -53,22 +50,21 @@ public class RichTextBoxWordColoringRule
5350

5451
/// <summary>
5552
/// Gets the compiled regular expression that matches either Text or Regex property.
56-
///
5753
/// </summary>
58-
public Regex CompileRegex(LogEventInfo logEvent)
54+
internal Regex ResolveRegEx(string pattern, string text, bool wholeWords, bool ignoreCase)
5955
{
60-
string pattern = this.Regex == null? null: this.Regex.Render(logEvent);
61-
if (pattern == null && this.Text != null)
56+
if (string.IsNullOrEmpty(pattern) && text != null)
6257
{
63-
pattern = System.Text.RegularExpressions.Regex.Escape(this.Text.Render(logEvent));
64-
if (this.WholeWords)
58+
pattern = System.Text.RegularExpressions.Regex.Escape(text);
59+
if (wholeWords)
6560
pattern = "\b" + pattern + "\b";
6661
}
67-
RegexOptions options = RegexOptions.Compiled;
68-
if (this.IgnoreCase)
62+
63+
RegexOptions options = RegexOptions.None;
64+
if (ignoreCase)
6965
options |= RegexOptions.IgnoreCase;
70-
71-
return new Regex(pattern, options);
66+
67+
return new Regex(pattern, options); // RegEx-Cache
7268
}
7369

7470
/// <summary>
@@ -77,7 +73,6 @@ public Regex CompileRegex(LogEventInfo logEvent)
7773
///
7874
/// </summary>
7975
/// <docgen category="Formatting Options" order="10"/>
80-
[DefaultValue("Empty")]
8176
public Layout FontColor { get; set; }
8277

8378
/// <summary>
@@ -86,7 +81,6 @@ public Regex CompileRegex(LogEventInfo logEvent)
8681
///
8782
/// </summary>
8883
/// <docgen category="Formatting Options" order="10"/>
89-
[DefaultValue("Empty")]
9084
public Layout BackgroundColor { get; set; }
9185

9286
/// <summary>

NLog.Windows.Forms/Targets/ToolStripItemTarget.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,14 @@ protected override void Write(LogEventInfo logEvent)
9191

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

94-
if (control == null || !(control is ToolStrip))
94+
ToolStrip toolStrip = control as ToolStrip;
95+
if (toolStrip == null)
9596
{
9697
InternalLogger.Info("ToolStrip {0} on Form {1} not found", ToolStripName, FormName);
9798
return;
9899
}
99100

100-
ToolStrip toolStrip = control as ToolStrip;
101-
102101
ToolStripItem item = FormHelper.FindToolStripItem(RenderLogEvent(ItemName, logEvent), toolStrip.Items);
103-
104102
if (item == null)
105103
{
106104
InternalLogger.Info("ToolStripItem {0} on ToolStrip {1} not found", ItemName, ToolStripName);
@@ -110,18 +108,18 @@ protected override void Write(LogEventInfo logEvent)
110108
try
111109
{
112110
control.BeginInvoke(new DelSendTheMessageToFormControl(SendTheMessageToFormControl), item, logMessage);
113-
114111
}
115112
catch (Exception ex)
116113
{
117-
InternalLogger.Warn(ex.ToString());
114+
InternalLogger.Warn(ex, "Failed to assign Control.Text");
118115

119116
if (LogManager.ThrowExceptions)
120117
{
121118
throw;
122119
}
123120
}
124121
}
122+
125123
private void SendTheMessageToFormControl(ToolStripItem item, string logMessage)
126124
{
127125
item.Text = logMessage;

0 commit comments

Comments
 (0)