-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationLibrary.cs
More file actions
132 lines (110 loc) · 3.62 KB
/
NotificationLibrary.cs
File metadata and controls
132 lines (110 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
namespace DB_Helper
{
internal class NotificationLibrary
{
// Basis-Interface für Benachrichtigungen
public interface INotification
{
void ShowMessage(string message);
void ShowError(string error);
void ShowWarning(string warning);
}
// Logger-Implementierung
/*
Logger verwenden
var logger = new LogNotification("app.log");
logger.ShowMessage("Operation erfolgreich");
logger.ShowError("Ein Fehler ist aufgetreten");
*/
public class LogNotification : INotification
{
private readonly string logPath;
public LogNotification(string path = "log.txt")
{
logPath = path;
}
public void ShowMessage(string message)
{
LogToFile($"INFO: {message}");
}
public void ShowError(string error)
{
LogToFile($"ERROR: {error}");
}
public void ShowWarning(string warning)
{
LogToFile($"WARNING: {warning}");
}
private void LogToFile(string content)
{
try
{
System.IO.File.AppendAllText(logPath, $"{DateTime.Now}: {content}\n");
}
catch (Exception ex)
{
Console.WriteLine($"Logging failed: {ex.Message}");
}
}
}
// Event-basierte Implementierung
/*
Events verwenden
var events = new EventNotification();
events.OnError += (sender, message) =>
{
// Hier kann man auf Fehler reagieren
};
events.ShowError("Fehlermeldung");
*/
public class EventNotification : INotification
{
public event EventHandler<string> OnMessage;
public event EventHandler<string> OnError;
public event EventHandler<string> OnWarning;
public void ShowMessage(string message)
{
OnMessage?.Invoke(this, message);
}
public void ShowError(string error)
{
OnError?.Invoke(this, error);
}
public void ShowWarning(string warning)
{
OnWarning?.Invoke(this, warning);
}
}
// Konsolen-basierte Implementierung
/*
Konsole verwenden
var console = new ConsoleNotification();
console.ShowWarning("Warnung!");
*/
public class ConsoleNotification : INotification
{
public void ShowMessage(string message)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"INFO: {message}");
Console.ForegroundColor = originalColor;
}
public void ShowError(string error)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"ERROR: {error}");
Console.ForegroundColor = originalColor;
}
public void ShowWarning(string warning)
{
var originalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"WARNING: {warning}");
Console.ForegroundColor = originalColor;
}
}
}
}