UnhandledExceptionWriter.cs 1.4 KB

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