📘 C# Blog Series – Blog 42 **“C# File Handling — Logs, Packets, Errors को File में Store करने का सबसे Perfect तरीका

📘 C# Blog Series – Blog 42

**“C# File Handling — Logs, Packets, Errors को File में Store करने का सबसे Perfect तरीका




(Real-Life Example: Daily Log Files, Packet Dumps, Crash Reports)”**

GPS Tracking Server में File Handling बहुत ज़रूरी है।
क्योंकि:

  • Packet logs

  • Error logs

  • Crash reports

  • Invalid packets

  • Debugging files

  • Daily logs

ये सब future debugging के लिए important होते हैं।

C# file handling को fast, safe, और production-ready तरीके से use करना चाहिए।
आज हम इसे आसान भाषा + GPS real examples के साथ समझेंगे।


🟦 1. File.WriteAllText() — Quick File Write

File.WriteAllText("log.txt", "Hello GPS!");

✔ Simple
✔ Small files

❌ बड़े logs के लिए धीमा
❌ Overwrite हो जाता है


🟢 2. File.AppendAllText() — Append Logs (Best for simple logs)

File.AppendAllText("log.txt", "Packet Received\n");

GPS system में basic logging के लिए perfect।


3. File.ReadAllText() — Read Entire File

string data = File.ReadAllText("log.txt");

🌟 4. StreamWriter — Large Logs के लिए Best (High Performance)

using (StreamWriter sw = new StreamWriter("packets.log", true)) { sw.WriteLine("IMEI=861...,LAT=26..."); }

✔ Fast
✔ Efficient
✔ Append mode
✔ Perfect for packet dump files


🟠 5. StreamReader — File Read with Memory Efficiency

using (StreamReader sr = new StreamReader("packets.log")) { string line; while((line = sr.ReadLine()) != null) { Console.WriteLine(line); } }

Large logs को आसानी से process कर सकते हैं।


🔵 6. BinaryWriter / BinaryReader (Advanced)

अगर आपको raw GPS packets binary में store करना हो:

using var bw = new BinaryWriter(File.Open("raw.bin", FileMode.Append)); bw.Write(bytes);

Perfect for:

  • Encrypted packets

  • Binary protocols

  • Raw TCP dumps


🟣 7. Directory Management (Create folder if not exists)

string path = "Logs/"; if(!Directory.Exists(path)) Directory.CreateDirectory(path);

Logs को folder structure में रखना बहुत जरूरी है।


🔥 8. Daily Log Files — GPS Server का सबसे important pattern

Daily log file name:

string fname = $"Logs/Log_{DateTime.Now:yyyy_MM_dd}.txt"; File.AppendAllText(fname, "Packet data...\n");

Benefits:

✔ लॉग बहुत बड़े नहीं बनते
✔ Search करना आसान
✔ हर दिन का अलग record


🧠 9. Packet Dump File (Debugging के लिए MUST)

अगर कभी device गलत packet भेजे या unknown protocol हो—
तो उसे dump file में store करो।

string dump = $"Dumps/{DateTime.Now:yyyy_MM_dd}.dump"; File.AppendAllText(dump, packet + "\n");

यह future debugging में God-level help करता है।


10. Error Logging (Production-Ready Code)

try { ... } catch(Exception ex) { File.AppendAllText("Errors/error.log", $"{DateTime.Now}: {ex}\n"); }

GPS servers में अचानक crashes बहुत कम हो जाते हैं
क्योंकि हर error track हो जाती है।


📌 11. JSON File Handling (Useful for config/settings)

Write:

File.WriteAllText("config.json", JsonSerializer.Serialize(settings));

Read:

var settings = JsonSerializer.Deserialize<Config>(File.ReadAllText("config.json"));

🧪 12. File.Exists() — Check before reading

if(File.Exists("imei.txt")) { var imei = File.ReadAllText("imei.txt"); }

No file → no crash.


🛡 13. Async File IO (High-Performance GPS Servers के लिए BEST)

await File.AppendAllTextAsync("packets.log", packet + "\n");

Async version:

✔ Background में file write
✔ Main thread free
✔ High concurrency friendly


⚠ Common Mistakes (Avoid)**

❌ File open करके बंद न करना (Dispose missing)
❌ Large logs को memory में store करना
❌ Every packet को अलग file में write करना
❌ High-frequency logs में File.WriteAllText()

Best practice:
Use StreamWriter OR AppendAllTextAsync


🔚 Conclusion

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

✔ Debugging-friendly
✔ Stable
✔ Organized
✔ Production-safe
✔ GPS server-ready
✔ Error-proof

GPS server में logs, dumps और crash files MOST important होते हैं।
सही तरीके से file handling करने से आपका backend rock-solid हो जाता है।

Post a Comment

0 Comments

Translate

Close Menu