2012-03-01 02:36:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.Mvc;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
using MongoDB.Driver;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
using NLog;
|
2012-03-01 06:45:36 +00:00
|
|
|
|
using Ninject;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
using NzbDrone.Common.Contract;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
using NzbDrone.Services.Service.Exceptions;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
using NzbDrone.Services.Service.Repository.Reporting;
|
|
|
|
|
using Services.PetaPoco;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
using ExceptionInstance = NzbDrone.Services.Service.Repository.Reporting.ExceptionInstance;
|
|
|
|
|
using ExceptionReport = NzbDrone.Common.Contract.ExceptionReport;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Services.Service.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class ExceptionController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IDatabase _database;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
private readonly ExceptionRepository _exceptionRepository;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2012-03-01 06:45:36 +00:00
|
|
|
|
[Inject]
|
2012-03-30 01:35:35 +00:00
|
|
|
|
public ExceptionController(IDatabase database, ExceptionRepository exceptionRepository)
|
2012-03-01 02:36:39 +00:00
|
|
|
|
{
|
|
|
|
|
_database = database;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
_exceptionRepository = exceptionRepository;
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
2012-03-01 06:38:42 +00:00
|
|
|
|
|
2012-03-01 05:10:56 +00:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public EmptyResult ReportExisting(ExistingExceptionReport existingExceptionReport)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-03-01 06:38:42 +00:00
|
|
|
|
if (ExceptionHashExists(existingExceptionReport.Hash))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var exceptionInstance = new ExceptionInstance
|
|
|
|
|
{
|
|
|
|
|
ExceptionHash = existingExceptionReport.Hash,
|
|
|
|
|
IsProduction = existingExceptionReport.IsProduction,
|
|
|
|
|
LogMessage = existingExceptionReport.LogMessage,
|
2012-03-01 07:58:11 +00:00
|
|
|
|
UGuid = existingExceptionReport.UGuid,
|
2012-03-01 06:38:42 +00:00
|
|
|
|
Timestamp = DateTime.Now
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_database.Insert(exceptionInstance);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn("Invalid exception hash '{0}'", existingExceptionReport.Hash);
|
|
|
|
|
}
|
2012-03-01 05:10:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.FatalException("Error has occurred while saving exception", e);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new EmptyResult();
|
|
|
|
|
}
|
2012-03-01 06:38:42 +00:00
|
|
|
|
|
2012-03-01 02:36:39 +00:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public JsonResult ReportNew(ExceptionReport exceptionReport)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
|
2012-03-30 01:35:35 +00:00
|
|
|
|
var report = new Exceptions.ExceptionReport();
|
|
|
|
|
report.AppVersion = exceptionReport.Version;
|
|
|
|
|
report.ApplicationId = "NzbDrone";
|
2012-03-30 01:36:49 +00:00
|
|
|
|
report.ExceptionMessage = exceptionReport.ExceptionMessage;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
report.ExceptionType = exceptionReport.Type;
|
|
|
|
|
report.Location = exceptionReport.Logger;
|
|
|
|
|
report.Message = exceptionReport.LogMessage;
|
2012-03-30 01:36:49 +00:00
|
|
|
|
report.Stack = exceptionReport.Stack;
|
2012-03-30 01:35:35 +00:00
|
|
|
|
report.Uid = exceptionReport.UGuid.ToString();
|
2012-03-01 02:36:39 +00:00
|
|
|
|
|
2012-03-30 01:54:29 +00:00
|
|
|
|
_exceptionRepository.Store(report);
|
2012-03-30 01:35:35 +00:00
|
|
|
|
|
2012-03-30 01:54:29 +00:00
|
|
|
|
var exceptionHash = GetExceptionDetailId(exceptionReport);
|
2012-03-30 01:35:35 +00:00
|
|
|
|
|
2012-03-30 01:54:29 +00:00
|
|
|
|
var exceptionInstance = new ExceptionInstance
|
|
|
|
|
{
|
|
|
|
|
ExceptionHash = exceptionHash,
|
|
|
|
|
IsProduction = exceptionReport.IsProduction,
|
|
|
|
|
LogMessage = exceptionReport.LogMessage,
|
|
|
|
|
Timestamp = DateTime.Now,
|
|
|
|
|
UGuid = exceptionReport.UGuid
|
|
|
|
|
};
|
2012-03-30 01:35:35 +00:00
|
|
|
|
|
2012-03-30 01:54:29 +00:00
|
|
|
|
_database.Insert(exceptionInstance);
|
2012-03-01 02:36:39 +00:00
|
|
|
|
|
2012-03-01 05:10:56 +00:00
|
|
|
|
return new JsonResult { Data = new ExceptionReportResponse { ExceptionHash = exceptionHash } };
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2012-03-01 05:10:56 +00:00
|
|
|
|
logger.FatalException("Error has occurred while saving exception", e);
|
2012-03-01 06:38:42 +00:00
|
|
|
|
if (!exceptionReport.IsProduction)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
2012-03-01 06:38:42 +00:00
|
|
|
|
|
|
|
|
|
return new JsonResult();
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
2012-03-01 06:38:42 +00:00
|
|
|
|
|
2012-03-01 05:10:56 +00:00
|
|
|
|
private string GetExceptionDetailId(ExceptionReport exceptionReport)
|
2012-03-01 02:36:39 +00:00
|
|
|
|
{
|
2012-03-01 05:10:56 +00:00
|
|
|
|
var reportHash = Hash(String.Concat(exceptionReport.Version, exceptionReport.String, exceptionReport.Logger));
|
2012-03-01 06:38:42 +00:00
|
|
|
|
|
2012-03-01 05:10:56 +00:00
|
|
|
|
if (!ExceptionHashExists(reportHash))
|
2012-03-01 02:36:39 +00:00
|
|
|
|
{
|
|
|
|
|
var exeptionDetail = new ExceptionDetail();
|
|
|
|
|
exeptionDetail.Hash = reportHash;
|
|
|
|
|
exeptionDetail.Logger = exceptionReport.Logger;
|
|
|
|
|
exeptionDetail.String = exceptionReport.String;
|
|
|
|
|
exeptionDetail.Type = exceptionReport.Type;
|
|
|
|
|
exeptionDetail.Version = exceptionReport.Version;
|
|
|
|
|
|
2012-03-01 06:38:42 +00:00
|
|
|
|
_database.Insert(exeptionDetail);
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-01 05:10:56 +00:00
|
|
|
|
return reportHash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ExceptionHashExists(string reportHash)
|
|
|
|
|
{
|
|
|
|
|
return _database.Exists<ExceptionDetail>(reportHash);
|
2012-03-01 02:36:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Hash(string input)
|
|
|
|
|
{
|
|
|
|
|
uint mCrc = 0xffffffff;
|
|
|
|
|
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(input);
|
|
|
|
|
foreach (byte myByte in bytes)
|
|
|
|
|
{
|
|
|
|
|
mCrc ^= ((uint)(myByte) << 24);
|
|
|
|
|
for (var i = 0; i < 8; i++)
|
|
|
|
|
{
|
|
|
|
|
if ((Convert.ToUInt32(mCrc) & 0x80000000) == 0x80000000)
|
|
|
|
|
{
|
|
|
|
|
mCrc = (mCrc << 1) ^ 0x04C11DB7;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
mCrc <<= 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return String.Format("{0:x8}", mCrc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|