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

46 lines
1.5 KiB
C#
Raw Normal View History

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