jellyfin/Emby.Server.Implementations/Logging/UnhandledExceptionWriter.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2014-11-23 15:36:40 -07:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
2017-02-20 13:50:58 -07:00
using MediaBrowser.Model.IO;
2014-11-23 15:36:40 -07:00
2017-02-20 13:50:58 -07:00
namespace Emby.Server.Implementations.Logging
2014-11-23 15:36:40 -07:00
{
public class UnhandledExceptionWriter
{
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
private readonly ILogManager _logManager;
2017-02-20 13:50:58 -07:00
private readonly IFileSystem _fileSystem;
private readonly IConsoleLogger _console;
2014-11-23 15:36:40 -07:00
2017-02-20 13:50:58 -07:00
public UnhandledExceptionWriter(IApplicationPaths appPaths, ILogger logger, ILogManager logManager, IFileSystem fileSystem, IConsoleLogger console)
2014-11-23 15:36:40 -07:00
{
_appPaths = appPaths;
_logger = logger;
_logManager = logManager;
2017-02-20 13:50:58 -07:00
_fileSystem = fileSystem;
_console = console;
2014-11-23 15:36:40 -07:00
}
public void Log(Exception ex)
{
_logger.ErrorException("UnhandledException", ex);
_logManager.Flush();
var path = Path.Combine(_appPaths.LogDirectoryPath, "unhandled_" + Guid.NewGuid() + ".txt");
2017-02-20 13:50:58 -07:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
2014-11-23 15:36:40 -07:00
var builder = LogHelper.GetLogMessage(ex);
// Write to console just in case file logging fails
2017-02-20 13:50:58 -07:00
_console.WriteLine("UnhandledException");
var logMessage = builder.ToString();
_console.WriteLine(logMessage);
_fileSystem.WriteAllText(path, logMessage);
2014-11-23 15:36:40 -07:00
}
}
}