Sonarr/NzbDrone.Common/ReportingService.cs

148 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2012-04-30 01:24:24 +00:00
using Exceptron.Driver;
using NLog;
using NzbDrone.Common.Contract;
namespace NzbDrone.Common
{
public static class ReportingService
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2012-03-01 07:03:19 +00:00
private const string SERVICE_URL = "http://services.nzbdrone.com/reporting";
2012-02-05 16:53:08 +00:00
private const string PARSE_URL = SERVICE_URL + "/ParseError";
public static RestProvider RestProvider { get; set; }
2012-04-30 01:24:24 +00:00
public static ExceptionClient ExceptronDriver { get; set; }
2012-04-22 23:14:02 +00:00
2012-04-22 23:14:02 +00:00
private static readonly HashSet<string> parserErrorCache = new HashSet<string>();
public static void ClearCache()
{
lock (parserErrorCache)
{
parserErrorCache.Clear();
}
}
public static void ReportParseError(string title)
{
try
{
2012-04-22 23:14:02 +00:00
VerifyDependencies();
lock (parserErrorCache)
{
if (parserErrorCache.Contains(title.ToLower())) return;
2012-04-22 23:14:02 +00:00
parserErrorCache.Add(title.ToLower());
}
var report = new ParseErrorReport { Title = title };
RestProvider.PostData(PARSE_URL, report);
}
catch (Exception e)
{
if (!EnvironmentProvider.IsProduction)
{
throw;
}
e.Data.Add("title", title);
2012-02-22 04:43:19 +00:00
logger.InfoException("Unable to report parse error", e);
}
}
public static string ReportException(LogEventInfo logEvent)
{
try
{
2012-04-22 23:14:02 +00:00
VerifyDependencies();
var exceptionData = new ExceptionData();
exceptionData.Exception = logEvent.Exception;
2012-05-06 16:03:23 +00:00
exceptionData.Component = logEvent.LoggerName;
exceptionData.Message = logEvent.FormattedMessage;
2012-04-22 23:14:02 +00:00
exceptionData.UserId = EnvironmentProvider.UGuid.ToString().Replace("-", string.Empty);
if (logEvent.Level <= LogLevel.Info)
{
exceptionData.Severity = ExceptionSeverity.None;
}
else if (logEvent.Level <= LogLevel.Warn)
{
exceptionData.Severity = ExceptionSeverity.Warning;
}
else if (logEvent.Level <= LogLevel.Error)
{
exceptionData.Severity = ExceptionSeverity.Error;
}
else if (logEvent.Level <= LogLevel.Fatal)
{
exceptionData.Severity = ExceptionSeverity.Fatal;
}
return ExceptronDriver.SubmitException(exceptionData).RefId;
}
catch (Exception e)
{
if (!EnvironmentProvider.IsProduction)
{
throw;
}
2012-04-30 02:42:49 +00:00
if (logEvent.LoggerName != logger.Name)//prevents a recursive loop.
{
logger.WarnException("Unable to report exception. ", e);
}
}
return null;
}
2012-04-22 23:14:02 +00:00
2012-04-30 01:24:24 +00:00
public static void SetupExceptronDriver()
{
2012-05-06 16:03:23 +00:00
ExceptronDriver = new ExceptionClient("CB230C312E5C4FF38B4FB9644B05E60G")
2012-05-01 06:16:54 +00:00
{
ApplicationVersion = new EnvironmentProvider().Version.ToString()
};
2012-06-17 21:55:28 +00:00
ExceptronDriver.ClientConfiguration.ThrowsExceptions = !EnvironmentProvider.IsProduction;
2012-04-30 01:24:24 +00:00
ExceptronDriver.Enviroment = EnvironmentProvider.IsProduction ? "Prod" : "Dev";
2012-04-22 23:14:02 +00:00
}
private static void VerifyDependencies()
{
if (RestProvider == null)
{
2012-04-22 23:14:02 +00:00
if (EnvironmentProvider.IsProduction)
{
logger.Warn("Rest provider wasn't provided. creating new one!");
RestProvider = new RestProvider(new EnvironmentProvider());
}
else
{
throw new InvalidOperationException("REST Provider wasn't configured correctly.");
}
}
2012-04-22 23:14:02 +00:00
2012-04-30 01:24:24 +00:00
if (ExceptronDriver == null)
2012-04-22 23:14:02 +00:00
{
if (EnvironmentProvider.IsProduction)
{
2012-04-30 01:24:24 +00:00
logger.Warn("Exceptron Driver wasn't provided. creating new one!");
SetupExceptronDriver();
2012-04-22 23:14:02 +00:00
}
else
{
2012-04-30 01:24:24 +00:00
throw new InvalidOperationException("Exceptron Driver wasn't configured correctly.");
2012-04-22 23:14:02 +00:00
}
}
}
}
}