Skip to content

Commit d58cf56

Browse files
Update samples used in delegates articles (#7028)
* Update samples used in delegates articles Contributes to dotnet/docs#45199 These samples haven't been updated since .NET 7. It's time to update them while updating the related articles. * Update csharp/events/Program.cs * Apply suggestions from code review Co-authored-by: David Pine <[email protected]> --------- Co-authored-by: David Pine <[email protected]>
1 parent 0ee6c13 commit d58cf56

File tree

7 files changed

+135
-239
lines changed

7 files changed

+135
-239
lines changed
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
using System;
2-
using System.IO;
1+
namespace DelegatesAndEvents;
32

4-
namespace DelegatesAndEvents
3+
public class FileLogger
54
{
6-
// <SnippetFileLogger>
7-
public class FileLogger
5+
private readonly string _logPath;
6+
public FileLogger(string path)
87
{
9-
private readonly string logPath;
10-
public FileLogger(string path)
11-
{
12-
logPath = path;
13-
Logger.WriteMessage += LogMessage;
14-
}
8+
_logPath = path;
9+
Logger.WriteMessage += LogMessage;
10+
}
1511

16-
public void DetachLog() => Logger.WriteMessage -= LogMessage;
17-
// make sure this can't throw.
18-
private void LogMessage(string msg)
12+
public void DetachLog() => Logger.WriteMessage -= LogMessage;
13+
// make sure this can't throw.
14+
private void LogMessage(string msg)
15+
{
16+
try
1917
{
20-
try
18+
using (var log = File.AppendText(_logPath))
2119
{
22-
using (var log = File.AppendText(logPath))
23-
{
24-
log.WriteLine(msg);
25-
log.Flush();
26-
}
27-
}
28-
catch (Exception)
29-
{
30-
// Hmm. We caught an exception while
31-
// logging. We can't really log the
32-
// problem (since it's the log that's failing).
33-
// So, while normally, catching an exception
34-
// and doing nothing isn't wise, it's really the
35-
// only reasonable option here.
20+
log.WriteLine(msg);
21+
log.Flush();
3622
}
3723
}
24+
catch (Exception)
25+
{
26+
// Hmm. We caught an exception while
27+
// logging. We can't really log the
28+
// problem (since it's the log that's failing).
29+
// So, while normally, catching an exception
30+
// and doing nothing isn't wise, it's really the
31+
// only reasonable option here.
32+
}
3833
}
39-
// </SnippetFileLogger>
4034
}
Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,27 @@
1-
using System;
1+
namespace DelegatesAndEvents;
22

3-
namespace DelegatesAndEvents
3+
public enum Severity
44
{
5-
6-
// Logger implementation two
7-
// <SnippetSeverity>
8-
public enum Severity
9-
{
10-
Verbose,
11-
Trace,
12-
Information,
13-
Warning,
14-
Error,
15-
Critical
16-
}
17-
// </SnippetSeverity>
18-
19-
// <SnippetLoggerFinal>
20-
public static class Logger
21-
{
22-
public static Action<string> WriteMessage;
23-
24-
public static Severity LogLevel { get; set; } = Severity.Warning;
25-
26-
public static void LogMessage(Severity s, string component, string msg)
27-
{
28-
if (s < LogLevel)
29-
return;
30-
31-
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
32-
WriteMessage(outputMsg);
33-
}
34-
}
35-
// </SnippetLoggerFinal>
5+
Verbose,
6+
Trace,
7+
Information,
8+
Warning,
9+
Error,
10+
Critical
3611
}
3712

38-
namespace ImplementationOne
13+
public static class Logger
3914
{
40-
// <SnippetFirstImplementation>
41-
public static class Logger
42-
{
43-
public static Action<string> WriteMessage;
15+
public static Action<string>? WriteMessage;
4416

45-
public static void LogMessage(string msg)
46-
{
47-
WriteMessage(msg);
48-
}
49-
}
50-
// </SnippetFirstImplementation>
51-
}
52-
53-
namespace ImplementationTwo
54-
{
55-
using DelegatesAndEvents;
17+
public static Severity LogLevel { get; set; } = Severity.Warning;
5618

57-
// <SnippetLoggerTwo>
58-
public static class Logger
19+
public static void LogMessage(Severity s, string component, string msg)
5920
{
60-
public static Action<string> WriteMessage;
21+
if ((s < LogLevel) || (WriteMessage is null))
22+
return;
6123

62-
public static void LogMessage(Severity s, string component, string msg)
63-
{
64-
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
65-
WriteMessage(outputMsg);
66-
}
24+
var outputMsg = $"{DateTime.Now}\t{s}\t{component}\t{msg}";
25+
WriteMessage(outputMsg);
6726
}
68-
// </SnippetLoggerTwo>
6927
}
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
using System;
1+
using DelegatesAndEvents;
22

3-
namespace DelegatesAndEvents
4-
{
5-
public class Program
6-
{
7-
public static void Main(string[] args)
8-
{
9-
// <SnippetConnectDelegate>
10-
Logger.WriteMessage += LoggingMethods.LogToConsole;
11-
// </SnippetConnectDelegate>
12-
// <SnippetFileLogger>
13-
var file = new FileLogger("log.txt");
14-
// </SnippetFileLogger>
3+
Logger.WriteMessage += LoggingMethods.LogToConsole;
4+
var file = new FileLogger("log.txt");
155

16-
Logger.LogMessage(Severity.Warning, "Console", "This is a warning message");
6+
Logger.LogMessage(Severity.Warning, "Console", "This is a warning message");
177

18-
Logger.LogMessage(Severity.Information, "Console", "Information message one");
19-
Logger.LogLevel = Severity.Information;
8+
Logger.LogMessage(Severity.Information, "Console", "Information message one");
9+
Logger.LogLevel = Severity.Information;
2010

21-
Logger.LogMessage(Severity.Information, "Console", "Information message two");
22-
}
23-
}
24-
}
11+
Logger.LogMessage(Severity.Information, "Console", "Information message two");
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<OutputType>Exe</OutputType>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
68
</PropertyGroup>
79

810
</Project>

0 commit comments

Comments
 (0)