ServiceUtils.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using DeepCore;
  2. using DeepCore.IO;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace DeepCrystal.RPC
  9. {
  10. public static class ServiceUtils
  11. {
  12. public static void LogState(this IService service)
  13. {
  14. using (var sb = StringBuilderObjectPool.AllocAutoRelease())
  15. {
  16. if (service.GetState(sb))
  17. {
  18. var info = sb.ToString();
  19. Task.Run(() =>
  20. {
  21. var type = service.GetType();
  22. try
  23. {
  24. lock (type)
  25. {
  26. var name = CFiles.ReplaceSpecialChars(service.SelfAddress.FullPath);
  27. var file = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "state" + Path.DirectorySeparatorChar + name + ".txt";
  28. CFiles.CreateFile(new FileInfo(file));
  29. File.WriteAllText(file, info, CUtils.UTF8);
  30. }
  31. }
  32. catch { }
  33. });
  34. }
  35. }
  36. }
  37. }
  38. }