Skip to content

Commit

Permalink
.NET Core 3.1 + NET 5 Support (#42)
Browse files Browse the repository at this point in the history
* Start .Core 3

* upgrade for rpeview 7

* install .net core 3 preview on appveyor

* WIP

* revert unneeded changes

* cleanup

* testprogram

* disable for .NET Core 3

* .NET 5

* cleanup

* test colors

* test all platforms

* update testscript

* fix MaxLinesTest

* unit tests are written with a very specific font in mind

* there can be whitespace after viewkind markup

* skip testing unsupported scenarios

* do not hardcode the font, instead hardcode test results

* use hyperlink markup instead of hidden text for links

* move to .NET Core 3.1

* Disable SupportLinks for .NET Core 3.x

* 3.1 appveyor

* Fix csproj

* sln folder

* Version 4.5-rc.1

* fix csproj #2

Co-authored-by: Tobias Käs <[email protected]>
  • Loading branch information
304NotModified and weltkante authored Mar 29, 2021
1 parent 1f16810 commit 8ba69a3
Show file tree
Hide file tree
Showing 26 changed files with 5,920 additions and 49 deletions.
9 changes: 6 additions & 3 deletions NLog.Windows.Forms.Tests/NLog.Windows.Forms.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<TargetFrameworks>net452;netcoreapp3.1;net5.0-windows</TargetFrameworks>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<AssemblyTitle>NLog.Windows.Forms.Tests</AssemblyTitle>
<Company>Microsoft</Company>
<Product>NLog.Windows.Forms.Tests</Product>
<Copyright>Copyright © Jaroslaw Kowalski, Kim Christensen, Julian Verdurmen 2015-2016</Copyright>
<OutputPath>bin\$(Configuration)\</OutputPath>
<Copyright>Copyright © Jaroslaw Kowalski, Kim Christensen, Julian Verdurmen</Copyright>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
Expand All @@ -20,6 +19,10 @@
<PropertyGroup>
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net5.0-windows' ">
<!-- Links aren't support for .NET Core 3.x, but does work for .NET 5 and .NET Framework-->
<DefineConstants>$(DefineConstants);LINKS_SUPPORTED</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.extensibility.execution" Version="2.4.1" />
Expand Down
88 changes: 80 additions & 8 deletions NLog.Windows.Forms.Tests/RichTextBoxTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,32 @@ public void SimpleRichTextBoxTargetTest()
var result = rtfText;
Assert.Contains(@"{\colortbl ;\red255\green255\blue255;\red255\green0\blue0;\red255\green165\blue0;\red0\green0\blue0;\red128\green128\blue128;\red169\green169\blue169;}", result);

#if NETCOREAPP
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\b\f0\fs18 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
#else
if (IsAppVeyor())
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\b\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\b\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
}
else
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\b\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\b\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
}
#endif

Assert.Contains(@"\cf2\highlight1\i Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par", result);
Assert.Contains(@"\cf3\ul\b0\i0 Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par", result);
Assert.Contains(@"\cf4\ulnone Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"\cf5 Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par", result);
Assert.Contains(@"\cf6\i Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par", result);
#if NETCOREAPP
Assert.Contains(@"\cf0\highlight0\i0\par", result);
#else
Assert.Contains(@"\cf0\highlight0\i0\f1\par", result);
#endif
Assert.Contains(@"}", result);

LogManager.Configuration = null;
Expand Down Expand Up @@ -115,7 +126,11 @@ public void NoColoringTest()
Assert.Contains(@"Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par", result);
Assert.Contains(@"Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par", result);
#if NETCOREAPP
Assert.Contains(@"\cf0\highlight0\par", result);
#else
Assert.Contains(@"\cf0\highlight0\f1\par", result);
#endif
Assert.Contains(@"}", result);
}
finally
Expand Down Expand Up @@ -166,7 +181,11 @@ public void CustomRowColoringTest()
Assert.Contains(@"\cf1 Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo\par", result);
Assert.Contains(@"\cf3 Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar\par", result);
#if NETCOREAPP
Assert.Contains(@"\cf0\highlight0\par", result);
#else
Assert.Contains(@"\cf0\highlight0\f1\par", result);
#endif
Assert.Contains(@"}", result);
}
finally
Expand All @@ -177,14 +196,21 @@ public void CustomRowColoringTest()

private static void AssertViewkind(string result)
{
#if NETCOREAPP
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs18 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
#else
if (IsAppVeyor())
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
}
else
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test\par", result);
}
#endif
}

[Fact]
Expand Down Expand Up @@ -226,21 +252,32 @@ public void CustomWordRowColoringTest()
var result = rtfText;
Assert.Contains(@"{\colortbl ;\red0\green0\blue0;\red255\green255\blue255;\red255\green0\blue0;\red0\green128\blue0;}", result);

#if NETCOREAPP
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs18 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par", result);
#else
if (IsAppVeyor())
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs17 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par", result);
}
else
{
Assert.Contains(@"\viewkind4\uc1\pard\cf1\highlight2\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par", result);
Assert.Contains(@"\viewkind4\uc1", result);
Assert.Contains(@"\pard\cf1\highlight2\f0\fs15 Fatal NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf3\f1 zzz\cf1\f0\par", result);
}
#endif

Assert.Contains(@"Error NLog.UnitTests.Targets.RichTextBoxTargetTests Foo xxx\par", result);
Assert.Contains(@"Warn NLog.UnitTests.Targets.RichTextBoxTargetTests Bar yyy\par", result);
Assert.Contains(@"Info NLog.UnitTests.Targets.RichTextBoxTargetTests Test \cf4\f1 aaa\cf1\f0\par", result);
Assert.Contains(@"Debug NLog.UnitTests.Targets.RichTextBoxTargetTests Foo \cf3\f1 zzz\cf1\f0\par", result);
Assert.Contains(@"Trace NLog.UnitTests.Targets.RichTextBoxTargetTests Bar ccc\par", result);
#if NETCOREAPP
Assert.Contains(@"\cf0\highlight0\par", result);
#else
Assert.Contains(@"\cf0\highlight0\f1\par", result);
#endif
Assert.Contains(@"}", result);
}
finally
Expand Down Expand Up @@ -299,7 +336,6 @@ public void MaxLinesTest()
{
ControlName = "Control1",
Layout = "${message}",
ShowMinimized = true,
ToolWindow = false,
AutoScroll = true,
};
Expand Down Expand Up @@ -1006,6 +1042,8 @@ public void ManualRegisterTestWithRetentionConfigReload()
}
}

#if LINKS_SUPPORTED

[Fact]
public void LinkLayoutTestDisabledLinks()
{
Expand All @@ -1029,6 +1067,7 @@ public void LinkLayoutTestDisabledLinks()
Assert.Matches(@"(\([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}\))", result); //the placeholder GUID was not replaced by was not replaced because of SupportLinks set to false
}


[Fact]
public void LinkTest()
{
Expand All @@ -1054,9 +1093,14 @@ public void LinkTest()
string resultText = target.TargetRichTextBox.Text;
Assert.DoesNotMatch(@"(\([a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}\))", resultRtf); //the placeholder GUID was replaced
Assert.Contains("descr#link", resultText); //text contains visible and invisible parts
#if NETCOREAPP
Assert.Contains(@"{\field{\*\fldinst{HYPERLINK ""descr#link", resultRtf); //RTF contains everything
#else
Assert.Contains(@"descr\v #link", resultRtf); //RTF contains everything
#endif
}


[Fact]
public void LinkTestConditional()
{
Expand Down Expand Up @@ -1084,7 +1128,11 @@ public void LinkTestConditional()
string resultRtf = ExtractRtf(target.TargetRichTextBox);
Assert.Contains("TestNoLink", resultText);
Assert.DoesNotContain("#link", resultText); //no link for first event
#if NETCOREAPP
Assert.DoesNotContain(@"{\field{\*\fldinst{HYPERLINK", resultRtf); //no link for first event
#else
Assert.DoesNotContain(@"\v #link", resultRtf); //no link for first event
#endif
}


Expand All @@ -1100,8 +1148,13 @@ public void LinkTestConditional()
{
string resultText = target.TargetRichTextBox.Text;
string resultRtf = ExtractRtf(target.TargetRichTextBox);
#if NETCOREAPP
Assert.Contains("TestWithLink HYPERLINK \"marker_text#link", resultText); //link for a second event
Assert.Contains(@"TestWithLink {{\field{\*\fldinst{HYPERLINK ""marker_text#link", resultRtf); //link for a second event
#else
Assert.Contains("TestWithLink marker_text#link", resultText); //link for a second event
Assert.Contains(@"marker_text\v #link", resultRtf); //link for a second event
#endif
}
}

Expand Down Expand Up @@ -1135,7 +1188,11 @@ public void LinkTestExcessLinksRemoved()
string resultText = target.TargetRichTextBox.Text;
string resultRtf = ExtractRtf(target.TargetRichTextBox);
Assert.Contains("#link", resultText); //some links exist
#if NETCOREAPP
Assert.Contains(@"{\field{\*\fldinst{HYPERLINK", resultRtf); //some links exist
#else
Assert.Contains(@"\v #link", resultRtf); //some links exist
#endif

Assert.True(target.LinkedEventsCount == target.MaxLines); //storing 5, not 100 events
}
Expand Down Expand Up @@ -1165,9 +1222,13 @@ public void LinkClickTest()
bool linkClickedFromHandler = false;
string linkTextFromHandler = null;
LogEventInfo logEventFromHandler = null;
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;


RichTextBoxTarget.DelLinkClicked clickHandler = (RichTextBoxTarget sender, string linkText, LogEventInfo logEvent) =>
{
cancellationTokenSource.Cancel();
//actual checks moved to main code to make exceptions caught by the test runner.
linkClickedFromHandler = true;
linkTextFromHandler = linkText;
Expand All @@ -1177,6 +1238,16 @@ public void LinkClickTest()

RichTextBoxTarget.GetTargetByControl(target.TargetRichTextBox).LinkClicked += clickHandler;

Task.Run(() =>
{
// max wait time. After that. Stop
int timeout = 1_000;
Thread.Sleep(timeout);
target.TargetForm.Close();
throw new TimeoutException("Waited to long for click");

}, cancellationToken);

//simulate clicking on a link
Task.Run(() =>
{
Expand All @@ -1190,13 +1261,14 @@ public void LinkClickTest()
}
});

//in case link does not click, this would hang up infinitely;

Application.Run(target.TargetForm);

Assert.True(linkClickedFromHandler); //check that we have actually clicked on a link, not just missed anything
Assert.True("link" == linkTextFromHandler);
Assert.True("Test" == logEventFromHandler.Message);
}
#endif


#region mouse click smulation
Expand Down
23 changes: 21 additions & 2 deletions NLog.Windows.Forms.sln
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.645
# Visual Studio Version 16
VisualStudioVersion = 16.0.29001.49
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLog.Windows.Forms", "NLog.Windows.Forms\NLog.Windows.Forms.csproj", "{EB5C7E87-C5F4-43FC-AC71-BE0A22F5B932}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NLog.Windows.Forms.Tests", "NLog.Windows.Forms.Tests\NLog.Windows.Forms.Tests.csproj", "{77ED37BE-37DD-4C16-B027-AD6B0EDE0B4F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication", "TestApplication\TestApplication.csproj", "{03D9D89A-B972-4446-AB4A-A430851243D0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplicationCore3", "TestApplicationCore3\TestApplicationCore3.csproj", "{9FAC81DA-0137-47E9-957A-D8A9A5215338}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplicationNet5", "TestApplicationNet5\TestApplicationNet5.csproj", "{134980CC-0226-42B1-B02F-A9C81AAB0A3D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test Applications", "Test Applications", "{6AB4C95A-36D8-4F52-8F25-EF9CA98AF536}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,10 +33,23 @@ Global
{03D9D89A-B972-4446-AB4A-A430851243D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03D9D89A-B972-4446-AB4A-A430851243D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03D9D89A-B972-4446-AB4A-A430851243D0}.Release|Any CPU.Build.0 = Release|Any CPU
{9FAC81DA-0137-47E9-957A-D8A9A5215338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9FAC81DA-0137-47E9-957A-D8A9A5215338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9FAC81DA-0137-47E9-957A-D8A9A5215338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9FAC81DA-0137-47E9-957A-D8A9A5215338}.Release|Any CPU.Build.0 = Release|Any CPU
{134980CC-0226-42B1-B02F-A9C81AAB0A3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{134980CC-0226-42B1-B02F-A9C81AAB0A3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{134980CC-0226-42B1-B02F-A9C81AAB0A3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{134980CC-0226-42B1-B02F-A9C81AAB0A3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{03D9D89A-B972-4446-AB4A-A430851243D0} = {6AB4C95A-36D8-4F52-8F25-EF9CA98AF536}
{9FAC81DA-0137-47E9-957A-D8A9A5215338} = {6AB4C95A-36D8-4F52-8F25-EF9CA98AF536}
{134980CC-0226-42B1-B02F-A9C81AAB0A3D} = {6AB4C95A-36D8-4F52-8F25-EF9CA98AF536}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2DC4C500-52F1-4D06-863D-209D713D03ED}
EndGlobalSection
Expand Down
6 changes: 5 additions & 1 deletion NLog.Windows.Forms/FormHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal static Form CreateForm(string name, int width, int height, bool show, b
{
Name = name,
Text = "NLog",
Icon = GetNLogIcon()
Icon = GetNLogIcon(),
};

#if !Smartphone
Expand Down Expand Up @@ -163,6 +163,9 @@ private static Icon GetNLogIcon()
/// </remarks>
internal static void ChangeSelectionToLink(RichTextBox textBox, string text, string hyperlink)
{
#if NETCOREAPP
textBox.SelectedRtf = @"{\rtf1\ansi{\field{\*\fldinst{HYPERLINK """ + text + @"#" + hyperlink + @""" }}{\fldrslt{" + text + @"}}}}";
#else
int selectionStart = textBox.SelectionStart;

//using \v tag to hide hyperlink part of the text, and \v0 to end hiding. See http://stackoverflow.com/a/14339531/376066
Expand All @@ -171,6 +174,7 @@ internal static void ChangeSelectionToLink(RichTextBox textBox, string text, str

textBox.Select(selectionStart, text.Length + 1 + hyperlink.Length); //now select both visible and invisible part
SetSelectionStyle(textBox, CFM_LINK, CFE_LINK); //and turn into a link
#endif
}

/// <summary>
Expand Down
23 changes: 11 additions & 12 deletions NLog.Windows.Forms/NLog.Windows.Forms.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>net35</TargetFramework>
<TargetFrameworks>net35;netcoreapp3.1;net5.0-windows</TargetFrameworks>
<AssemblyTitle>NLog.Windows.Forms</AssemblyTitle>
<Company>NLog</Company>
<Product>NLog.Windows.Forms</Product>
Expand All @@ -16,26 +16,25 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<DebugType Condition=" '$(Configuration)' == 'Debug' ">Full</DebugType>
<UseWindowsForms>true</UseWindowsForms>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>NLog.snk</AssemblyOriginatorKeyFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<RepositoryUrl>https://github.com/NLog/NLog.Windows.Forms.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<PackageReleaseNotes>

- RichTextBoxTarget: added overloads for ReInitializeAllTextboxes and GetTargetByControl (#72) (@304NotModified)
- RichTextBoxTarget: better error handling for empty or unexpected NLog configs (#72) (@304NotModified)

- Update NLog to 4.7.9 (#74) (@304NotModified)
- Fixed publishing Symbol package (#46) (@304NotModified)
- Added Sourcelink (#70) (@304NotModified)
- Enable deterministic build (#73) (@304NotModified)

<PackageReleaseNotes>
Added support for .NET Core 3.1 and .NET 5 (#42) (@weltkante, @304NotModified)
</PackageReleaseNotes>
</PropertyGroup>

</PackageReleaseNotes>
<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' or '$(TargetFramework)' == 'net5.0-windows' ">
<!-- Links aren't support for .NET Core 3.x, but does work for .NET 5 and .NET Framework-->
<DefineConstants>$(DefineConstants);LINKS_SUPPORTED</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NLog" Version="4.7.9" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
Expand Down
Loading

0 comments on commit 8ba69a3

Please sign in to comment.