UnhandledExceptionWriter.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.IO;
  6. namespace MediaBrowser.Server.Startup.Common
  7. {
  8. public class UnhandledExceptionWriter
  9. {
  10. private readonly IApplicationPaths _appPaths;
  11. private readonly ILogger _logger;
  12. private readonly ILogManager _logManager;
  13. public UnhandledExceptionWriter(IApplicationPaths appPaths, ILogger logger, ILogManager logManager)
  14. {
  15. _appPaths = appPaths;
  16. _logger = logger;
  17. _logManager = logManager;
  18. }
  19. public void Log(Exception ex)
  20. {
  21. _logger.ErrorException("UnhandledException", ex);
  22. _logManager.Flush();
  23. var path = Path.Combine(_appPaths.LogDirectoryPath, "unhandled_" + Guid.NewGuid() + ".txt");
  24. Directory.CreateDirectory(Path.GetDirectoryName(path));
  25. var builder = LogHelper.GetLogMessage(ex);
  26. // Write to console just in case file logging fails
  27. Console.WriteLine("UnhandledException");
  28. Console.WriteLine(builder.ToString());
  29. File.WriteAllText(path, builder.ToString());
  30. }
  31. }
  32. }