2012-02-05 09:34:36 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2012-08-23 22:57:42 +03:00
|
|
|
using Exceptron.Client;
|
|
|
|
using Exceptron.Client.Configuration;
|
2012-02-05 09:34:36 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Contract;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public static class ReportingService
|
|
|
|
{
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
2012-03-01 10:03:19 +03:00
|
|
|
private const string SERVICE_URL = "http://services.nzbdrone.com/reporting";
|
2012-02-05 19:53:08 +03:00
|
|
|
private const string PARSE_URL = SERVICE_URL + "/ParseError";
|
2012-02-05 09:34:36 +03:00
|
|
|
|
|
|
|
public static RestProvider RestProvider { get; set; }
|
2012-08-23 22:57:42 +03:00
|
|
|
public static ExceptronClient ExceptronClient { get; set; }
|
2012-04-23 02:14:02 +03:00
|
|
|
|
2012-02-05 09:34:36 +03:00
|
|
|
|
2012-04-23 02:14:02 +03:00
|
|
|
private static readonly HashSet<string> parserErrorCache = new HashSet<string>();
|
2012-02-05 09:34:36 +03:00
|
|
|
|
|
|
|
public static void ClearCache()
|
|
|
|
{
|
|
|
|
lock (parserErrorCache)
|
|
|
|
{
|
|
|
|
parserErrorCache.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void ReportParseError(string title)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-04-23 02:14:02 +03:00
|
|
|
VerifyDependencies();
|
2012-02-05 09:34:36 +03:00
|
|
|
|
|
|
|
lock (parserErrorCache)
|
|
|
|
{
|
|
|
|
if (parserErrorCache.Contains(title.ToLower())) return;
|
2012-04-23 02:14:02 +03:00
|
|
|
|
2012-02-05 09:34:36 +03:00
|
|
|
parserErrorCache.Add(title.ToLower());
|
|
|
|
}
|
|
|
|
|
|
|
|
var report = new ParseErrorReport { Title = title };
|
|
|
|
RestProvider.PostData(PARSE_URL, report);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
if (!EnvironmentProvider.IsProduction)
|
2012-02-05 09:34:36 +03:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Data.Add("title", title);
|
2012-02-22 07:43:19 +03:00
|
|
|
logger.InfoException("Unable to report parse error", e);
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 02:30:18 +03:00
|
|
|
public static string ReportException(LogEventInfo logEvent)
|
2012-02-05 09:34:36 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-04-23 02:14:02 +03:00
|
|
|
VerifyDependencies();
|
|
|
|
|
|
|
|
var exceptionData = new ExceptionData();
|
|
|
|
|
|
|
|
exceptionData.Exception = logEvent.Exception;
|
2012-05-06 19:03:23 +03:00
|
|
|
exceptionData.Component = logEvent.LoggerName;
|
2012-04-23 05:35:55 +03:00
|
|
|
exceptionData.Message = logEvent.FormattedMessage;
|
2012-04-23 02:14:02 +03:00
|
|
|
exceptionData.UserId = EnvironmentProvider.UGuid.ToString().Replace("-", string.Empty);
|
|
|
|
|
2012-05-22 02:36:56 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-23 22:57:42 +03:00
|
|
|
return ExceptronClient.SubmitException(exceptionData).RefId;
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
if (!EnvironmentProvider.IsProduction)
|
2012-02-05 09:34:36 +03:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2012-04-30 05:42:49 +03:00
|
|
|
if (logEvent.LoggerName != logger.Name)//prevents a recursive loop.
|
|
|
|
{
|
|
|
|
logger.WarnException("Unable to report exception. ", e);
|
|
|
|
}
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
2012-04-23 02:30:18 +03:00
|
|
|
|
|
|
|
return null;
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
2012-03-02 04:57:36 +03:00
|
|
|
|
2012-04-23 02:14:02 +03:00
|
|
|
|
2012-04-30 04:24:24 +03:00
|
|
|
public static void SetupExceptronDriver()
|
2012-03-02 04:57:36 +03:00
|
|
|
{
|
2012-08-23 22:57:42 +03:00
|
|
|
var config = new ExceptronConfiguration
|
|
|
|
{
|
|
|
|
ApiKey = "CB230C312E5C4FF38B4FB9644B05E60G",
|
|
|
|
ThrowExceptions = !EnvironmentProvider.IsProduction,
|
|
|
|
};
|
|
|
|
|
|
|
|
ExceptronClient = new ExceptronClient(config)
|
2012-05-01 09:16:54 +03:00
|
|
|
{
|
|
|
|
ApplicationVersion = new EnvironmentProvider().Version.ToString()
|
|
|
|
};
|
2012-04-23 02:14:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void VerifyDependencies()
|
|
|
|
{
|
|
|
|
if (RestProvider == null)
|
2012-03-02 04:57:36 +03:00
|
|
|
{
|
2012-04-23 02:14:02 +03:00
|
|
|
if (EnvironmentProvider.IsProduction)
|
2012-03-02 04:57:36 +03:00
|
|
|
{
|
|
|
|
logger.Warn("Rest provider wasn't provided. creating new one!");
|
2012-03-07 05:59:43 +03:00
|
|
|
RestProvider = new RestProvider(new EnvironmentProvider());
|
2012-03-02 04:57:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("REST Provider wasn't configured correctly.");
|
|
|
|
}
|
|
|
|
}
|
2012-04-23 02:14:02 +03:00
|
|
|
|
2012-08-23 22:57:42 +03:00
|
|
|
if (ExceptronClient == null)
|
2012-04-23 02:14:02 +03:00
|
|
|
{
|
|
|
|
if (EnvironmentProvider.IsProduction)
|
|
|
|
{
|
2012-04-30 04:24:24 +03:00
|
|
|
logger.Warn("Exceptron Driver wasn't provided. creating new one!");
|
|
|
|
SetupExceptronDriver();
|
2012-04-23 02:14:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-30 04:24:24 +03:00
|
|
|
throw new InvalidOperationException("Exceptron Driver wasn't configured correctly.");
|
2012-04-23 02:14:02 +03:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 04:57:36 +03:00
|
|
|
}
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
|
|
|
}
|