📘 C# Blog Series – Blog 44 **“C# Logging — NLog, Serilog, File Logging, Structured Logs कैसे Use करते हैं?

📘 C# Blog Series – Blog 44

**“C# Logging — NLog, Serilog, File Logging, Structured Logs कैसे Use करते हैं?






(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

चलो सबसे आसान तरीके से शुरू करते हैं।


🟦 1. Logging क्या है? (Simple Definition)

Logging =
System में होने वाली हर महत्वपूर्ण activity का रिकॉर्ड रखना।

Examples:

  • Packet received

  • Packet parsing error

  • Device connected

  • DB failed

  • Alert triggered

  • Server crashed

GPS server में logging सबसे important element है।


🟢 2. Basic File Logging (Fast + Simple)

File.AppendAllText("logs.txt", $"{DateTime.Now} Packet: {pkt}\n");

सम्मानजनक काम करता है पर enterprise-grade नहीं है।


3. Structured Logging (Production Standard)

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


🌟 4. Introducing Serilog (BEST Logging Library)

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");

Why Serilog?

✔ Structured logging
✔ Fast
✔ Amazing sinks (SQL, File, Console, Seq, Elastic)
✔ Perfect for GPS debugging


🟠 5. NLog — दूसरी सबसे famous logging library

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);

🔵 6. Log Levels (Very Important)

LevelUse
Traceहर छोटा detail (rare)
DebugDevelopment में
InformationNormal logs
WarningSuspicious activity
ErrorRecoverable error
FatalCrash-level errors

GPS server में:

  • Packet logs → Information

  • DB timeout → Warning

  • Parsing failure → Error

  • Server crash → Fatal


🟣 7. Real-Life GPS Logging Architecture

1. Packet Logs

Logs/Packets/packet_2025_12_10.log

Each line:

2025-12-10 14:22:10 | IMEI:861128068064267 | PKT:...,LAT:26.85,...

2. Error Logs

Logs/Errors/error_2025_12_10.log

3. Unknown Packet dumps

Logs/Dumps/unknown_2025_12_10.dump

4. Alert Logs

Logs/Alerts/[alert-type]/...

🔥 8. Real Code: Logging inside GPS Packet Processor

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.


🧠 9. Async Logging (Best for high-load servers)

Serilog automatically async बनाता है:

.WriteTo.Async(a => a.File("Logs/log.txt"))

GPS server में यह performance को 2x बढ़ा देता है।


📌 10. Rolling Logs (Daily logs auto-create)

Serilog:

.WriteTo.File("Logs/log_.txt", rollingInterval: RollingInterval.Day)

Automatic log splitting per day.


🛡 11. Avoid Overlogging (Common Mistake)

❌ हर packet को फ़ाइल में log करना (High load → HDD overload)
✔ Only errors or filtered packets log करो
✔ Packet dump छोटे batches में रखो


🔚 Conclusion

C# Logging आपके system को बनाता है:

✔ Traceable
✔ Debuggable
✔ Stable
✔ Crash-proof
✔ Production-ready
✔ GPS-optimized

GPS tracking server में logs का सही structure
आपके system को industry-level strong बनाता है।

Post a Comment

0 Comments

Translate

Close Menu