UnhandledExceptionWriter.cs 1.1 KB

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