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-02-20 20:50:58 +00:00
|
|
|
|
_fileSystem.CreateDirectory(Path.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");
|
|
|
|
|
|
2017-04-27 18:11:56 +00:00
|
|
|
|
var logMessage = builder.ToString();
|
|
|
|
|
_console.WriteLine(logMessage);
|
|
|
|
|
|
|
|
|
|
_fileSystem.WriteAllText(path, logMessage);
|
2014-11-23 22:36:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|