From 0b8c41edbbb4e75128b2b377780735a43a4cfff0 Mon Sep 17 00:00:00 2001 From: Code Maze Date: Sun, 7 Nov 2021 17:49:26 +0100 Subject: [PATCH 1/3] CM-46: Delegates in C# Inital Commit --- .../DelegatesInCsharp/DelegatesInCsharp.sln | 31 ++++++ .../DelegatesInCsharp.csproj | 8 ++ .../DelegatesInCsharp/Program.cs | 53 +++++++++++ .../DelegatesInCsharp/Tests/Tests.cs | 94 +++++++++++++++++++ .../DelegatesInCsharp/Tests/Tests.csproj | 16 ++++ 5 files changed, 202 insertions(+) create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs create mode 100644 csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs create mode 100644 csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln new file mode 100644 index 0000000000..ea1653648e --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31702.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DelegatesInCsharp", "DelegatesInCsharp\DelegatesInCsharp.csproj", "{B7E7E843-3109-4BFF-90DD-B979CA1853B0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Release|Any CPU.Build.0 = Release|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {84A17409-6852-4689-A48E-AE3682E9D105} + EndGlobalSection +EndGlobal diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj new file mode 100644 index 0000000000..1d2d39a9ef --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs new file mode 100644 index 0000000000..f0a2d10334 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace DelegatesInCsharp +{ + delegate void PrintMessage(string text); + delegate T Print(T param1); + + class Program + { + public static void WriteText(string text) { Console.WriteLine($"Text: {text}"); } + public static void ReverseWriteText(string text) { Console.WriteLine($"Text in reverse: {Reverse(text)}"); } + public static string ReverseText(string text) { return Reverse(text); } + + private static string Reverse(string s) + { + char[] charArray = s.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + static void Main(string[] args) + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseWriteText); + // with + sign + PrintMessage multicastDelegate = delegate1 + delegate2; + + // with =, +=, and -= + multicastDelegate = delegate1; + multicastDelegate += delegate2; + + multicastDelegate.Invoke("Go ahead, make my day."); + multicastDelegate("You're gonna need a bigger boat."); + + Print delegate3 = new Print(ReverseText); + Console.WriteLine(delegate3("I'll be back.")); + + Action executeReverseWrite = ReverseWriteText; + executeReverseWrite("You're gonna need a bigger boat."); + + Func executeReverse = ReverseText; + Console.WriteLine(executeReverse("You're gonna need a bigger boat.")); + + // comment out other stuff + Action executeReverseWriteAction = ReverseWriteText; + executeReverseWriteAction("Are you not entertained?"); + Func executeReverseFunc = ReverseText; + Console.WriteLine(executeReverse("Are you not entertained?")); + } + } +} diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs new file mode 100644 index 0000000000..447a2a2f01 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs @@ -0,0 +1,94 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Tests +{ + delegate string PrintMessage(string text); + delegate T Print(T param1); + + [TestClass] + public class Tests + { + public static string WriteText(string text) { return $"Text:{text}"; } + public static string ReverseText(string text) { return Reverse(text); } + public static void ReverseWriteText(string text) { Console.WriteLine(Reverse(text)); } + + private static string Reverse(string s) + { + char[] charArray = s.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + [TestMethod] + public void whenStringIsSent_DelegateExecutesTheReferencedMethod() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + var result = delegate1("You're gonna need a bigger boat."); + Assert.AreEqual("Text:You're gonna need a bigger boat.", result); + } + + [TestMethod] + public void whenStringIsSent_DelegateReturnsTheReversedString() + { + PrintMessage delegate1 = new PrintMessage(ReverseText); + var result = delegate1("You're gonna need a bigger boat."); + Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); + } + + [TestMethod] + public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateInvocationListContainsTwoMethods() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseText); + PrintMessage multicastDelegate = delegate1 + delegate2; + + var invocationList = multicastDelegate.GetInvocationList(); + + Assert.AreEqual(invocationList.Length, 2); + Assert.AreEqual(invocationList[0].Method.Name, "WriteText"); + Assert.AreEqual(invocationList[1].Method.Name, "ReverseText"); + } + + [TestMethod] + public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_DelegateInvocationListContainsTwoMethods() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseText); + PrintMessage multicastDelegate = delegate1; + multicastDelegate += delegate2; + + var invocationList = multicastDelegate.GetInvocationList(); + + Assert.AreEqual(invocationList.Length, 2); + Assert.AreEqual(invocationList[0].Method.Name, "WriteText"); + Assert.AreEqual(invocationList[1].Method.Name, "ReverseText"); + } + + [TestMethod] + public void whenGenericDelegate_DelegateExecutesTheReferencedMethod() + { + Print delegate1 = new Print(ReverseText); + + var result = delegate1("You're gonna need a bigger boat."); + + Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); + } + + [TestMethod] + public void whenActionDelegate_DelegateInvocationListNotEmpty() + { + Action executeReverseWriteAction = ReverseWriteText; + var invocationList = executeReverseWriteAction.GetInvocationList(); + Assert.AreEqual(invocationList.Length, 1); + } + + [TestMethod] + public void whenFuncDelegate_DelegateInvocationListNotEmpty() + { + Func executeReverseWriteAction = ReverseText; + var invocationList = executeReverseWriteAction.GetInvocationList(); + Assert.AreEqual(invocationList.Length, 1); + } + } +} diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj new file mode 100644 index 0000000000..4203089904 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + + false + + + + + + + + + + From ae942072a27e6e353bd9ccbd91e3bcc8ce77cc89 Mon Sep 17 00:00:00 2001 From: Code Maze Date: Mon, 8 Nov 2021 15:11:03 +0100 Subject: [PATCH 2/3] Cleaning --- .../DelegatesInCsharp/Program.cs | 26 +++++++------------ .../DelegatesInCsharp/Tests/Tests.cs | 18 ++++++------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs index f0a2d10334..f928217476 100644 --- a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; namespace DelegatesInCsharp { @@ -9,9 +7,9 @@ namespace DelegatesInCsharp class Program { - public static void WriteText(string text) { Console.WriteLine($"Text: {text}"); } - public static void ReverseWriteText(string text) { Console.WriteLine($"Text in reverse: {Reverse(text)}"); } - public static string ReverseText(string text) { return Reverse(text); } + public static void WriteText(string text) => Console.WriteLine($"Text: {text}"); + public static void ReverseWriteText(string text) => Console.WriteLine($"Text in reverse: {Reverse(text)}"); + public static string ReverseText(string text) => Reverse(text); private static string Reverse(string s) { @@ -22,10 +20,10 @@ private static string Reverse(string s) static void Main(string[] args) { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseWriteText); + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseWriteText); // with + sign - PrintMessage multicastDelegate = delegate1 + delegate2; + var multicastDelegate = delegate1 + delegate2; // with =, +=, and -= multicastDelegate = delegate1; @@ -34,20 +32,14 @@ static void Main(string[] args) multicastDelegate.Invoke("Go ahead, make my day."); multicastDelegate("You're gonna need a bigger boat."); - Print delegate3 = new Print(ReverseText); + var delegate3 = new Print(ReverseText); Console.WriteLine(delegate3("I'll be back.")); - Action executeReverseWrite = ReverseWriteText; - executeReverseWrite("You're gonna need a bigger boat."); - - Func executeReverse = ReverseText; - Console.WriteLine(executeReverse("You're gonna need a bigger boat.")); - // comment out other stuff Action executeReverseWriteAction = ReverseWriteText; executeReverseWriteAction("Are you not entertained?"); - Func executeReverseFunc = ReverseText; - Console.WriteLine(executeReverse("Are you not entertained?")); + Func executeReverseFunc = ReverseText; + Console.WriteLine(executeReverseFunc("Are you not entertained?")); } } } diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs index 447a2a2f01..ce693d6792 100644 --- a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs @@ -23,7 +23,7 @@ private static string Reverse(string s) [TestMethod] public void whenStringIsSent_DelegateExecutesTheReferencedMethod() { - PrintMessage delegate1 = new PrintMessage(WriteText); + var delegate1 = new PrintMessage(WriteText); var result = delegate1("You're gonna need a bigger boat."); Assert.AreEqual("Text:You're gonna need a bigger boat.", result); } @@ -31,7 +31,7 @@ public void whenStringIsSent_DelegateExecutesTheReferencedMethod() [TestMethod] public void whenStringIsSent_DelegateReturnsTheReversedString() { - PrintMessage delegate1 = new PrintMessage(ReverseText); + var delegate1 = new PrintMessage(ReverseText); var result = delegate1("You're gonna need a bigger boat."); Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); } @@ -39,9 +39,9 @@ public void whenStringIsSent_DelegateReturnsTheReversedString() [TestMethod] public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateInvocationListContainsTwoMethods() { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseText); - PrintMessage multicastDelegate = delegate1 + delegate2; + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseText); + var multicastDelegate = delegate1 + delegate2; var invocationList = multicastDelegate.GetInvocationList(); @@ -53,9 +53,9 @@ public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateIn [TestMethod] public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_DelegateInvocationListContainsTwoMethods() { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseText); - PrintMessage multicastDelegate = delegate1; + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseText); + var multicastDelegate = delegate1; multicastDelegate += delegate2; var invocationList = multicastDelegate.GetInvocationList(); @@ -68,7 +68,7 @@ public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_Delegate [TestMethod] public void whenGenericDelegate_DelegateExecutesTheReferencedMethod() { - Print delegate1 = new Print(ReverseText); + var delegate1 = new Print(ReverseText); var result = delegate1("You're gonna need a bigger boat."); From cce8c859d3f04c082bea10313cd08e0007c52482 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Mon, 10 Aug 2026 02:25:46 +0200 Subject: [PATCH 3/3] serilog-log-levels: update for .NET 10 rewrite Retarget sample and tests to net10.0, bump Serilog to 4.4.0 and Serilog.AspNetCore to 10.0.0, and add a LoggingLevelSwitch example with a runtime admin endpoint to flip the minimum level without a redeploy. --- .../LogLevelsWithSerilog.csproj | 12 ++++++------ .../LogLevelsWithSerilog/Program.cs | 15 ++++++++++++++- .../Tests/LogLevelsWithSerilogTests.csproj | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/LogLevelsWithSerilog.csproj b/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/LogLevelsWithSerilog.csproj index 7d83ebc86e..088667a419 100644 --- a/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/LogLevelsWithSerilog.csproj +++ b/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/LogLevelsWithSerilog.csproj @@ -1,18 +1,18 @@ - net7.0 + net10.0 enable enable - - - - + + + + + - diff --git a/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/Program.cs b/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/Program.cs index c56fca9be6..01d30a44c6 100644 --- a/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/Program.cs +++ b/dotnet-logging/LogLevelsWithSerilog/LogLevelsWithSerilog/Program.cs @@ -1,4 +1,5 @@ using Serilog; +using Serilog.Core; using Serilog.Events; var configuration = new ConfigurationBuilder() @@ -10,13 +11,17 @@ Log.Information("Web Host started"); var builder = WebApplication.CreateBuilder(args); +// Wrapping the minimum level in a LoggingLevelSwitch lets us change it at +// runtime -- e.g. from an admin endpoint -- with no redeploy and no restart. +var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Information); + builder.Services.AddSerilog(options => { //we can configure serilog from configuration options.ReadFrom.Configuration(configuration); //or we can configure serilog via fluent api - options.MinimumLevel.Information() + options.MinimumLevel.ControlledBy(levelSwitch) .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("System", LogEventLevel.Warning) .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information, @@ -44,4 +49,12 @@ name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); +// e.g. POST /admin/log-level/Debug -- flips the minimum level on every sink +// immediately, with no restart. +app.MapPost("/admin/log-level/{level}", (LogEventLevel level) => +{ + levelSwitch.MinimumLevel = level; + return Results.Ok($"Minimum log level set to {level}."); +}); + app.Run(); \ No newline at end of file diff --git a/dotnet-logging/LogLevelsWithSerilog/Tests/LogLevelsWithSerilogTests.csproj b/dotnet-logging/LogLevelsWithSerilog/Tests/LogLevelsWithSerilogTests.csproj index 7c6328c181..4c219929d0 100644 --- a/dotnet-logging/LogLevelsWithSerilog/Tests/LogLevelsWithSerilogTests.csproj +++ b/dotnet-logging/LogLevelsWithSerilog/Tests/LogLevelsWithSerilogTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable