Skip to content

Commit 5668a7b

Browse files
author
Om-Suryawanshi
committed
updates
1 parent b2c65b4 commit 5668a7b

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
bin
22
obj
3-
.venv
3+
.venv
4+
logs
5+
log.txt

Services/LogService.cs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,43 @@ public class LogService : ILogService
44
{
55
private readonly List<LogEntry> _logs = new();
66
private readonly object _lock = new();
7+
private const string LogFilePath = "logs/log.txt";
8+
9+
public LogService()
10+
{
11+
Directory.CreateDirectory("logs");
12+
13+
// Load existing logs from file at startup
14+
if (File.Exists(LogFilePath))
15+
{
16+
var lines = File.ReadAllLines(LogFilePath);
17+
foreach (var line in lines)
18+
{
19+
var entry = ParseLogLine(line);
20+
if (entry != null)
21+
_logs.Add(entry);
22+
}
23+
}
24+
}
725

826
public void Log(string message, string level = "INFO", string? context = null)
927
{
10-
var entry = new LogEntry { Message = message, Level = level, Context = context };
28+
var entry = new LogEntry
29+
{
30+
Message = message,
31+
Level = level,
32+
Context = context,
33+
Timestamp = DateTime.UtcNow
34+
};
35+
1136
lock (_lock)
1237
{
1338
_logs.Add(entry);
14-
if (_logs.Count > 1000) _logs.RemoveAt(0); // keep memory usage in check
15-
}
39+
if (_logs.Count > 10000) _logs.RemoveAt(0); // optional trim
1640

17-
// Optional: Also write to file
18-
File.AppendAllText("logs/log.txt", $"{entry.Timestamp:o} [{level}] ({context}) {message}{Environment.NewLine}");
41+
File.AppendAllText(LogFilePath,
42+
$"{entry.Timestamp:o} [{entry.Level}] ({entry.Context}) {entry.Message}{Environment.NewLine}");
43+
}
1944
}
2045

2146
public IEnumerable<LogEntry> GetLogs()
@@ -25,5 +50,39 @@ public IEnumerable<LogEntry> GetLogs()
2550
return _logs.ToList();
2651
}
2752
}
53+
54+
private LogEntry? ParseLogLine(string line)
55+
{
56+
try
57+
{
58+
// Format: 2025-06-02T12:34:56.7890000Z [INFO] (Context) Message
59+
var timestampEnd = line.IndexOf('[') - 1;
60+
var timestamp = DateTime.Parse(line.Substring(0, timestampEnd).Trim());
61+
62+
var levelStart = line.IndexOf('[') + 1;
63+
var levelEnd = line.IndexOf(']');
64+
var level = line.Substring(levelStart, levelEnd - levelStart);
65+
66+
var contextStart = line.IndexOf('(', levelEnd) + 1;
67+
var contextEnd = line.IndexOf(')', contextStart);
68+
var context = line.Substring(contextStart, contextEnd - contextStart);
69+
70+
var messageStart = line.IndexOf(')', contextEnd) + 2;
71+
var message = line.Substring(messageStart);
72+
73+
return new LogEntry
74+
{
75+
Timestamp = timestamp,
76+
Level = level,
77+
Context = context,
78+
Message = message
79+
};
80+
}
81+
catch
82+
{
83+
// Ignore malformed lines
84+
return null;
85+
}
86+
}
2887
}
2988
}

Services/TcpListenerService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5959
{
6060
_tcpListener.Start();
6161
_logger.LogInformation($"✅ TCP Server started on {_localIp}:{Port}");
62-
_logService.Log($"TCP Listener started", "INFO", "{TcpListenerService}");
62+
_logService.Log($"TCP Listener started", "INFO", "TcpListenerService");
6363

6464

6565
// Background task for saving data

logs/log.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@
4949
2025-06-02T11:52:55.8212770Z [INFO] (🟡 TCP server is shutting down due to cancellation request.) TCP Listener Cancellation requested
5050
2025-06-02T11:52:55.8224596Z [INFO] (🔌 TCP listener stopped.) TCP Listener stopped
5151
2025-06-02T11:52:59.8444850Z [INFO] ({TcpListenerService}) TCP Listener started
52+
2025-06-02T12:03:59.3353722Z [INFO] (🟡 TCP server is shutting down due to cancellation request.) TCP Listener Cancellation requested
53+
2025-06-02T12:03:59.3362736Z [INFO] (🔌 TCP listener stopped.) TCP Listener stopped
54+
2025-06-02T12:04:06.6213739Z [INFO] ({TcpListenerService}) TCP Listener started
55+
2025-06-02T12:04:55.6516427Z [INFO] (🟡 TCP server is shutting down due to cancellation request.) TCP Listener Cancellation requested
56+
2025-06-02T12:04:55.6537237Z [INFO] (🔌 TCP listener stopped.) TCP Listener stopped
57+
2025-06-02T12:05:03.7978952Z [INFO] (TcpListenerService) TCP Listener started
58+
2025-06-02T12:18:11.7707276Z [INFO] (🟡 TCP server is shutting down due to cancellation request.) TCP Listener Cancellation requested
59+
2025-06-02T12:18:11.7720018Z [INFO] (🔌 TCP listener stopped.) TCP Listener stopped

0 commit comments

Comments
 (0)