(Real-Life Example: GPS Packet Logs, Error Logs, Alert Logs, Debug Logs)”**
Real-world systems में Logging = Life है।
GPS Tracking Server, Payment System, ERP, API Backend —
अगर logging सही नहीं की तो:
Errors trace नहीं होंगे
Packets खो जाएंगे
Server freeze का कारण पता नहीं चलेगा
Debugging impossible
Production में chaos
इस ब्लॉग में हम सीखेंगे:
✔ Basic File Logging
✔ Structured Logging
✔ Serilog
✔ NLog
✔ Log Levels
✔ Rolling Log Files
✔ GPS Server Logging Architecture
चलो सबसे आसान तरीके से शुरू करते हैं।
Logging =
System में होने वाली हर महत्वपूर्ण activity का रिकॉर्ड रखना।
Examples:
Packet received
Packet parsing error
Device connected
DB failed
Alert triggered
Server crashed
GPS server में logging सबसे important element है।
File.AppendAllText("logs.txt", $"{DateTime.Now} Packet: {pkt}\n");
सम्मानजनक काम करता है पर enterprise-grade नहीं है।
Structured means → JSON format logs:
{
"time": "2025-12-10",
"imei": "861128068064267",
"lat": 26.85,
"lon": 80.94
}
Benefits:
✔ Searchable
✔ Machine-readable
✔ Perfect for dashboards
✔ Elastic, Kibana compatible
Install:
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.File
dotnet add package Serilog.Sinks.Console
Setup:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("Logs/log_.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
Log:
Log.Information("Packet received from {IMEI}", device.IMEI);
Log.Error(ex, "Packet failed");
✔ Structured logging
✔ Fast
✔ Amazing sinks (SQL, File, Console, Seq, Elastic)
✔ Perfect for GPS debugging
Install:
dotnet add package NLog
dotnet add package NLog.Config
dotnet add package NLog.Targets
Config in nlog.config:
<targets>
<target xsi:type="File" name="logfile" fileName="Logs/log.txt" />
</targets>
<rules>
<logger name="*" minLevel="Info" writeTo="logfile" />
</rules>
Use:
logger.Info("Device connected: {IMEI}", imei);
| Level | Use |
|---|---|
| Trace | हर छोटा detail (rare) |
| Debug | Development में |
| Information | Normal logs |
| Warning | Suspicious activity |
| Error | Recoverable error |
| Fatal | Crash-level errors |
GPS server में:
Packet logs → Information
DB timeout → Warning
Parsing failure → Error
Server crash → Fatal
Logs/Packets/packet_2025_12_10.log
Each line:
2025-12-10 14:22:10 | IMEI:861128068064267 | PKT:...,LAT:26.85,...
Logs/Errors/error_2025_12_10.log
Logs/Dumps/unknown_2025_12_10.dump
Logs/Alerts/[alert-type]/...
try
{
parser.Parse(packet, device);
Log.Information("Parsed packet for {IMEI}", device.IMEI);
}
catch(Exception ex)
{
Log.Error(ex, "Packet parsing failed for {Packet}", packet);
File.AppendAllText("Dumps/unknown.dump", packet + "\n");
}
Perfect production code.
Serilog automatically async बनाता है:
.WriteTo.Async(a => a.File("Logs/log.txt"))
GPS server में यह performance को 2x बढ़ा देता है।
Serilog:
.WriteTo.File("Logs/log_.txt", rollingInterval: RollingInterval.Day)
Automatic log splitting per day.
❌ हर packet को फ़ाइल में log करना (High load → HDD overload)
✔ Only errors or filtered packets log करो
✔ Packet dump छोटे batches में रखो
C# Logging आपके system को बनाता है:
✔ Traceable
✔ Debuggable
✔ Stable
✔ Crash-proof
✔ Production-ready
✔ GPS-optimized
GPS tracking server में logs का सही structure
आपके system को industry-level strong बनाता है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav