UnhandledExceptionWriter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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(_fileSystem.GetDirectoryName(path));
  29. var builder = LogHelper.GetLogMessage(ex);
  30. // Write to console just in case file logging fails
  31. _console.WriteLine("UnhandledException");
  32. var logMessage = builder.ToString();
  33. _console.WriteLine(logMessage);
  34. _fileSystem.WriteAllText(path, logMessage);
  35. }
  36. }
  37. }