Skip to content

Commit 1f80032

Browse files
authored
Version 6.0 (#194)
1 parent 61769f9 commit 1f80032

15 files changed

+326
-285
lines changed

NLog.Windows.Forms.Tests/ApiTests.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,6 @@ public void WrapperLayoutRenderer_EnsureThreadAgnostic()
168168
}
169169
}
170170

171-
[Fact]
172-
public void RequiredConfigOptionMustBeClass()
173-
{
174-
foreach (Type type in allTypes)
175-
{
176-
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
177-
foreach (var prop in properties)
178-
{
179-
var requiredParameter = prop.GetCustomAttribute<NLog.Config.RequiredParameterAttribute>();
180-
if (requiredParameter != null)
181-
{
182-
Assert.True(prop.PropertyType.IsClass, prop.Name);
183-
}
184-
}
185-
}
186-
}
187-
188171
[Fact]
189172
public void SingleDefaultConfigOption()
190173
{

NLog.Windows.Forms.Tests/NLog.Windows.Forms.Tests.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net461;netcoreapp3.1;net6.0-windows</TargetFrameworks>
3+
<TargetFrameworks>net462;netcoreapp3.1;net6.0-windows</TargetFrameworks>
44
<UseWindowsForms>true</UseWindowsForms>
55
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
66
<AssemblyTitle>NLog.Windows.Forms.Tests</AssemblyTitle>
@@ -21,9 +21,8 @@
2121
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
2222
</PropertyGroup>
2323
<ItemGroup>
24-
<PackageReference Include="xunit" Version="2.8.0" />
25-
<PackageReference Include="xunit.extensibility.execution" Version="2.8.0" />
26-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
24+
<PackageReference Include="xunit" Version="2.8.1" />
25+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
2726
<PrivateAssets>all</PrivateAssets>
2827
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2928
</PackageReference>

NLog.Windows.Forms.Tests/RichTextBoxTargetTests.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ public void CustomRowColoringTest()
160160
ToolWindow = false,
161161
RowColoringRules =
162162
{
163+
#pragma warning disable CS0618 // Type or member is obsolete
163164
new RichTextBoxRowColoringRule("starts-with(message, 'B')", "Maroon", "Empty"),
165+
#pragma warning restore CS0618 // Type or member is obsolete
164166
}
165167
};
166168

@@ -235,8 +237,10 @@ public void CustomWordRowColoringTest()
235237
ToolWindow = false,
236238
WordColoringRules =
237239
{
240+
#pragma warning disable CS0618 // Type or member is obsolete
238241
new RichTextBoxWordColoringRule("zzz", "Red", "Empty"),
239242
new RichTextBoxWordColoringRule("aaa", "Green", "Empty"),
243+
#pragma warning restore CS0618 // Type or member is obsolete
240244
}
241245
};
242246

@@ -301,10 +305,10 @@ public void RichTextBoxTargetDefaultsTest()
301305
{
302306
var target = new RichTextBoxTarget();
303307
Assert.False(target.UseDefaultRowColoringRules);
304-
Assert.Equal(0, target.WordColoringRules.Count);
305-
Assert.Equal(0, target.RowColoringRules.Count);
306-
Assert.Null(target.FormName);
307-
Assert.Null(target.ControlName);
308+
Assert.Empty(target.WordColoringRules);
309+
Assert.Empty(target.RowColoringRules);
310+
Assert.Empty(target.FormName?.ToString());
311+
Assert.Empty(target.ControlName?.ToString());
308312
}
309313

310314
[Fact]
@@ -379,12 +383,14 @@ public void ColoringRuleDefaults()
379383
{
380384
var expectedRules = new[]
381385
{
386+
#pragma warning disable CS0618 // Type or member is obsolete
382387
new RichTextBoxRowColoringRule("level == LogLevel.Fatal", "White", "Red", FontStyle.Bold),
383388
new RichTextBoxRowColoringRule("level == LogLevel.Error", "Red", "Empty", FontStyle.Bold | FontStyle.Italic),
384389
new RichTextBoxRowColoringRule("level == LogLevel.Warn", "Orange", "Empty", FontStyle.Underline),
385390
new RichTextBoxRowColoringRule("level == LogLevel.Info", "Black", "Empty"),
386391
new RichTextBoxRowColoringRule("level == LogLevel.Debug", "Gray", "Empty"),
387392
new RichTextBoxRowColoringRule("level == LogLevel.Trace", "DarkGray", "Empty", FontStyle.Italic),
393+
#pragma warning restore CS0618 // Type or member is obsolete
388394
};
389395

390396
var actualRules = RichTextBoxTarget.DefaultRowColoringRules;
@@ -507,11 +513,11 @@ public void ActiveFormNegativeTest1()
507513
builder.ForLogger().WriteTo(target);
508514
}).LogFactory;
509515

510-
Assert.True(false, "Expected exception.");
516+
Assert.Fail("Expected exception.");
511517
}
512518
catch (NLogConfigurationException ex)
513519
{
514-
Assert.Equal("Rich text box control 'Control1' cannot be found on form 'MyForm1'.", ex.Message);
520+
Assert.Equal("RichTextBoxTarget(Name=RichTextBox): Rich text box control 'Control1' cannot be found on form 'MyForm1'.", ex.Message);
515521
}
516522
}
517523
}
@@ -539,11 +545,12 @@ public void ActiveFormNegativeTest2()
539545
{
540546
builder.ForLogger().WriteTo(target);
541547
}).LogFactory;
542-
Assert.True(false, "Expected exception.");
548+
549+
Assert.Fail("Expected exception.");
543550
}
544551
catch (NLogConfigurationException ex)
545552
{
546-
Assert.Equal("Rich text box control name must be specified for RichTextBoxTarget.", ex.Message);
553+
Assert.Equal("RichTextBoxTarget(Name=RichTextBox): Rich text box ControlName must be specified.", ex.Message);
547554
}
548555
}
549556
}

NLog.Windows.Forms/FormControlTarget.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Windows.Forms;
33
using NLog.Common;
4-
using NLog.Config;
54
using NLog.Layouts;
65
using NLog.Targets;
76

@@ -36,7 +35,6 @@ public sealed class FormControlTarget : TargetWithLayout
3635
/// </remarks>
3736
public FormControlTarget()
3837
{
39-
Append = true;
4038
}
4139

4240
private delegate void DelSendTheMessageToFormControl(Control control, string logMessage, bool append, bool reverseOrder);
@@ -45,58 +43,71 @@ public FormControlTarget()
4543
/// Gets or sets the name of control to which NLog will log write log text.
4644
/// </summary>
4745
/// <docgen category='Form Options' order='10' />
48-
[RequiredParameter]
49-
public Layout ControlName { get; set; }
46+
public Layout ControlName { get; set; } = Layout.Empty;
5047

5148
/// <summary>
5249
/// Gets or sets a value indicating whether log text should be appended to the text of the control instead of overwriting it. </summary>
5350
/// <docgen category='Form Options' order='10' />
54-
public Layout<bool> Append { get; set; }
51+
public Layout<bool> Append { get; set; } = true;
5552

5653
/// <summary>
5754
/// Gets or sets the name of the Form on which the control is located.
5855
/// </summary>
5956
/// <docgen category='Form Options' order='10' />
60-
public Layout FormName { get; set; }
57+
public Layout? FormName { get; set; }
6158

6259
/// <summary>
6360
/// Gets or sets whether new log entry are added to the start or the end of the control
6461
/// </summary>
65-
public Layout<bool> ReverseOrder { get; set; }
62+
public Layout<bool> ReverseOrder { get; set; } = false;
63+
64+
/// <inheritdoc />
65+
protected override void InitializeTarget()
66+
{
67+
if (ControlName is null || ReferenceEquals(ControlName, Layout.Empty))
68+
throw new NLogConfigurationException("FormControlTarget ControlName-property must be assigned.");
69+
70+
base.InitializeTarget();
71+
}
6672

6773
/// <summary>
6874
/// Log message to control.
6975
/// </summary>
70-
/// <param name="logEvent">
71-
/// The logging event.
72-
/// </param>
76+
/// <param name="logEvent">The logging event.</param>
7377
protected override void Write(LogEventInfo logEvent)
7478
{
7579
string logMessage = RenderLogEvent(Layout, logEvent);
7680

77-
Form form = null;
81+
Form? form = null;
7882

7983
if (Form.ActiveForm != null)
8084
{
8185
form = Form.ActiveForm;
8286
}
8387

84-
string renderedFormName = RenderLogEvent(FormName, logEvent);
85-
if (Application.OpenForms[renderedFormName] != null)
88+
string formName = RenderLogEvent(FormName, logEvent);
89+
if (Application.OpenForms[formName] != null)
8690
{
87-
form = Application.OpenForms[renderedFormName];
91+
form = Application.OpenForms[formName];
8892
}
8993

90-
if (form == null)
94+
if (form is null)
9195
{
92-
InternalLogger.Info("Form {0} not found", FormName);
96+
if (string.IsNullOrEmpty(formName))
97+
formName = FormName?.ToString() ?? string.Empty;
98+
InternalLogger.Info("Form {0} not found", formName);
9399
return;
94100
}
95101

96-
Control control = FormHelper.FindControl(RenderLogEvent(ControlName, logEvent), form);
97-
if (control == null)
102+
var controlName = RenderLogEvent(ControlName, logEvent);
103+
var control = FormHelper.FindControl(controlName, form);
104+
if (control is null)
98105
{
99-
InternalLogger.Info("Control {0} on Form {1} not found", ControlName, FormName);
106+
if (string.IsNullOrEmpty(controlName))
107+
controlName = ControlName?.ToString() ?? string.Empty;
108+
if (string.IsNullOrEmpty(formName))
109+
formName = form.Name;
110+
InternalLogger.Info("Control {0} on Form {1} not found", controlName, formName);
100111
return;
101112
}
102113

@@ -117,7 +128,7 @@ protected override void Write(LogEventInfo logEvent)
117128
}
118129
}
119130

120-
private void SendTheMessageToFormControl(Control control, string logMessage, bool append, bool reverseOrder)
131+
private static void SendTheMessageToFormControl(Control control, string logMessage, bool append, bool reverseOrder)
121132
{
122133
//append of replace?
123134
if (append)

NLog.Windows.Forms/FormHelper.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ internal static RichTextBox CreateRichTextBox(string name, Form parentForm)
3636
/// <param name="name">Name of the control.</param>
3737
/// <param name="searchControl">Control in which we're searching for control.</param>
3838
/// <returns>A value of null if no control has been found.</returns>
39-
internal static Control FindControl(string name, Control searchControl)
39+
internal static Control? FindControl(string name, Control? searchControl)
4040
{
41-
if (searchControl.Name == name)
41+
if (searchControl is null || searchControl.Name == name)
4242
{
43-
return searchControl;
43+
return searchControl;
4444
}
4545

46-
foreach (Control childControl in searchControl.Controls)
46+
foreach (var childControl in searchControl.Controls)
4747
{
48-
Control foundControl = FindControl(name, childControl);
48+
var foundControl = FindControl(name, childControl as Control);
4949
if (foundControl != null)
5050
{
5151
return foundControl;
@@ -61,25 +61,25 @@ internal static Control FindControl(string name, Control searchControl)
6161
/// <param name="name">Name of the ToolStripItem</param>
6262
/// <param name="searchCollection">Collection of ToolStripItem we are looking for the item in.</param>
6363
/// <returns>A value of null of no item has been found.</returns>
64-
internal static ToolStripItem FindToolStripItem(string name, ToolStripItemCollection searchCollection)
64+
internal static ToolStripItem? FindToolStripItem(string name, ToolStripItemCollection searchCollection)
6565
{
66-
foreach (ToolStripItem childItem in searchCollection)
66+
foreach (ToolStripItem? childItem in searchCollection)
6767
{
68-
if(childItem.Name == name)
68+
if (childItem?.Name == name)
6969
{
7070
return childItem;
7171
}
72-
if(childItem is ToolStripDropDownItem)
73-
{
74-
ToolStripDropDownItem childDropDown = childItem as ToolStripDropDownItem;
75-
ToolStripItem foundItem = FindToolStripItem(name, childDropDown.DropDownItems);
7672

73+
if (childItem is ToolStripDropDownItem childDropDown)
74+
{
75+
var foundItem = FindToolStripItem(name, childDropDown.DropDownItems);
7776
if (foundItem != null)
7877
{
7978
return foundItem;
8079
}
8180
}
8281
}
82+
8383
return null;
8484
}
8585

@@ -92,22 +92,24 @@ internal static ToolStripItem FindToolStripItem(string name, ToolStripItemCollec
9292
/// <returns>
9393
/// A value of null if no control has been found.
9494
/// </returns>
95-
internal static TControl FindControl<TControl>(string name, Control searchControl)
95+
internal static TControl? FindControl<TControl>(string name, Control? searchControl)
9696
where TControl : Control
9797
{
98+
if (searchControl is null)
99+
return null;
100+
98101
if (searchControl.Name == name)
99102
{
100-
TControl foundControl = searchControl as TControl;
103+
var foundControl = searchControl as TControl;
101104
if (foundControl != null)
102105
{
103106
return foundControl;
104107
}
105108
}
106109

107-
foreach (Control childControl in searchControl.Controls)
110+
foreach (var childControl in searchControl.Controls)
108111
{
109-
TControl foundControl = FindControl<TControl>(name, childControl);
110-
112+
var foundControl = FindControl<TControl>(name, childControl as Control);
111113
if (foundControl != null)
112114
{
113115
return foundControl;
@@ -171,15 +173,18 @@ internal static Form CreateForm(string name, int width, int height, bool show, b
171173
return f;
172174
}
173175

174-
private static Icon GetNLogIcon()
176+
private static Icon? GetNLogIcon()
175177
{
176178
using (var stream = typeof(FormHelper).Assembly.GetManifestResourceStream("NLog.Windows.Forms.Resources.NLog.ico"))
177179
{
180+
if (stream is null)
181+
return null;
182+
178183
return new Icon(stream);
179184
}
180185
}
181186

182-
#region Link support
187+
#region Link support
183188
/// <summary>
184189
/// Replaces currently selected text in the RTB control with a link
185190
/// </summary>
@@ -191,9 +196,7 @@ private static Icon GetNLogIcon()
191196
/// </remarks>
192197
internal static void ChangeSelectionToLink(RichTextBox textBox, string text, string hyperlink)
193198
{
194-
#if NETCOREAPP
195-
textBox.SelectedRtf = @"{\rtf1\ansi{\field{\*\fldinst{HYPERLINK """ + text + @"#" + hyperlink + @""" }}{\fldrslt{" + text + @"}}}}";
196-
#else
199+
#if NETFRAMEWORK
197200
int selectionStart = textBox.SelectionStart;
198201

199202
//using \v tag to hide hyperlink part of the text, and \v0 to end hiding. See http://stackoverflow.com/a/14339531/376066
@@ -202,9 +205,12 @@ internal static void ChangeSelectionToLink(RichTextBox textBox, string text, str
202205

203206
textBox.Select(selectionStart, text.Length + 1 + hyperlink.Length); //now select both visible and invisible part
204207
SetSelectionStyle(textBox, CFM_LINK, CFE_LINK); //and turn into a link
208+
#else
209+
textBox.SelectedRtf = @"{\rtf1\ansi{\field{\*\fldinst{HYPERLINK """ + text + @"#" + hyperlink + @""" }}{\fldrslt{" + text + @"}}}}";
205210
#endif
206211
}
207212

213+
#if NETFRAMEWORK
208214
/// <summary>
209215
/// Sets selection style for RichTextBox
210216
/// https://msdn.microsoft.com/en-us/library/windows/desktop/bb787883(v=vs.85).aspx
@@ -269,6 +275,7 @@ private struct CHARFORMAT2_STRUCT
269275

270276
[DllImport("user32.dll", CharSet = CharSet.Auto)]
271277
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
278+
#endif
272279
#endregion
273280
}
274281
}

0 commit comments

Comments
 (0)