2013-05-24 06:23:59 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using NLog;
|
|
|
|
using NLog.Config;
|
|
|
|
using NLog.Layouts;
|
|
|
|
using NLog.Targets;
|
2013-06-28 03:04:52 +03:00
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-24 06:23:59 +03:00
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
using Logger = Loggly.Logger;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Instrumentation
|
|
|
|
{
|
|
|
|
public class LogglyTarget : TargetWithLayout
|
|
|
|
{
|
|
|
|
private Logger _logger;
|
|
|
|
|
|
|
|
public void Register(LogLevel minLevel)
|
|
|
|
{
|
|
|
|
Layout = new SimpleLayout("${callsite:className=false:fileName=false:includeSourcePath=false:methodName=true}");
|
|
|
|
|
|
|
|
var rule = new LoggingRule("*", minLevel, this);
|
|
|
|
|
|
|
|
LogManager.Configuration.AddTarget("LogglyLogger", this);
|
|
|
|
LogManager.Configuration.LoggingRules.Add(rule);
|
|
|
|
LogManager.ConfigurationReloaded += (sender, args) => Register(minLevel);
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void InitializeTarget()
|
|
|
|
{
|
|
|
|
string apiKey = string.Empty;
|
|
|
|
|
2013-06-28 03:04:52 +03:00
|
|
|
if (RuntimeInfo.IsProduction)
|
2013-05-24 06:23:59 +03:00
|
|
|
{
|
|
|
|
apiKey = "4c4ecb69-d1b9-4e2a-b54b-b0c4cc143a95";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
apiKey = "d344a321-b107-45c4-a548-77138f446510";
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger = new Logger(apiKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-24 08:35:32 +03:00
|
|
|
protected override void Write(LogEventInfo logEvent)
|
2013-05-24 06:23:59 +03:00
|
|
|
{
|
|
|
|
var dictionary = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
if (logEvent.Exception != null)
|
|
|
|
{
|
|
|
|
dictionary.Add("ex", logEvent.Exception.ToString());
|
|
|
|
dictionary.Add("extyp", logEvent.Exception.GetType().Name);
|
|
|
|
dictionary.Add("hash", logEvent.GetHash());
|
2013-05-29 03:15:42 +03:00
|
|
|
|
|
|
|
foreach (var key in logEvent.Exception.Data.Keys)
|
|
|
|
{
|
|
|
|
dictionary.Add(key.ToString(), logEvent.Exception.Data[key]);
|
|
|
|
}
|
2013-05-24 06:23:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dictionary.Add("logger", logEvent.LoggerName);
|
|
|
|
dictionary.Add("method", Layout.Render(logEvent));
|
|
|
|
dictionary.Add("level", logEvent.Level.Name);
|
|
|
|
dictionary.Add("message", logEvent.GetFormattedMessage());
|
2013-06-28 03:04:52 +03:00
|
|
|
dictionary.Add("ver", BuildInfo.Version.ToString());
|
2013-05-24 06:23:59 +03:00
|
|
|
|
2013-05-30 00:29:51 +03:00
|
|
|
_logger.Log(dictionary.ToJson());
|
2013-05-24 06:23:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|