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

44 lines
1.4 KiB
C#
Raw Normal View History

2014-11-23 23:36:40 +01:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
2017-02-20 21:50:58 +01:00
using MediaBrowser.Model.IO;
2014-11-23 23:36:40 +01:00
2017-02-20 21:50:58 +01:00
namespace Emby.Server.Implementations.Logging
2014-11-23 23:36:40 +01:00
{
public class UnhandledExceptionWriter
{
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
private readonly ILogManager _logManager;
2017-02-20 21:50:58 +01:00
private readonly IFileSystem _fileSystem;
private readonly IConsoleLogger _console;
2014-11-23 23:36:40 +01:00
2017-02-20 21:50:58 +01:00
public UnhandledExceptionWriter(IApplicationPaths appPaths, ILogger logger, ILogManager logManager, IFileSystem fileSystem, IConsoleLogger console)
2014-11-23 23:36:40 +01:00
{
_appPaths = appPaths;
_logger = logger;
_logManager = logManager;
2017-02-20 21:50:58 +01:00
_fileSystem = fileSystem;
_console = console;
2014-11-23 23:36:40 +01: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 21:50:58 +01:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
2014-11-23 23:36:40 +01:00
var builder = LogHelper.GetLogMessage(ex);
// Write to console just in case file logging fails
2017-02-20 21:50:58 +01:00
_console.WriteLine("UnhandledException");
_console.WriteLine(builder.ToString());
_fileSystem.WriteAllText(path, builder.ToString());
2014-11-23 23:36:40 +01:00
}
}
}