mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-14 02:37:08 -07:00
added exceptron log target.
This commit is contained in:
parent
e37f413e19
commit
5ea794939c
26 changed files with 3113 additions and 0 deletions
106
NzbDrone.Common/Instrumentation/ExceptronTarget.cs
Normal file
106
NzbDrone.Common/Instrumentation/ExceptronTarget.cs
Normal file
|
@ -0,0 +1,106 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using Exceptron.Client;
|
||||
using Exceptron.Client.Configuration;
|
||||
using NLog;
|
||||
using NLog.Common;
|
||||
using NLog.Config;
|
||||
using NLog.Layouts;
|
||||
using NLog.Targets;
|
||||
|
||||
namespace NzbDrone.Common.Instrumentation
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="NLog"/> target for exceptron. Allows you to automatically report all
|
||||
/// exceptions logged to Nlog/>
|
||||
/// </summary>
|
||||
[Target("Exceptron")]
|
||||
public class ExceptronTarget : Target
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="ExceptronClient"/> instance that Nlog Target uses to report the exceptions.
|
||||
/// </summary>
|
||||
public IExceptronClient ExceptronClient { get; internal set; }
|
||||
|
||||
|
||||
private static ExceptronTarget _instance = new ExceptronTarget();
|
||||
|
||||
public static void Register()
|
||||
{
|
||||
var rule = new LoggingRule("*", LogLevel.Warn, _instance);
|
||||
|
||||
LogManager.Configuration.AddTarget("ExceptronTarget", _instance);
|
||||
LogManager.Configuration.LoggingRules.Add(rule);
|
||||
LogManager.ConfigurationReloaded += (sender, args) => Register();
|
||||
LogManager.ReconfigExistingLoggers();
|
||||
}
|
||||
|
||||
protected override void InitializeTarget()
|
||||
{
|
||||
var config = new ExceptronConfiguration
|
||||
{
|
||||
ApiKey = "57cb75d9eb2d457094d3f67133833eef",
|
||||
IncludeMachineName = true,
|
||||
};
|
||||
|
||||
if (EnvironmentProvider.IsProduction)
|
||||
{
|
||||
config.ApiKey = "cc4728a35aa9414f9a0baa8eed56bc67";
|
||||
}
|
||||
|
||||
ExceptronClient = new ExceptronClient(config, Environment.Version);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// String that identifies the active user
|
||||
/// </summary>
|
||||
public Layout UserId { get; set; }
|
||||
|
||||
protected override void Write(LogEventInfo logEvent)
|
||||
{
|
||||
if (logEvent == null || logEvent.Exception == null) return;
|
||||
|
||||
InternalLogger.Trace("Sending Exception to api.exceptron.com. Process Name: {0}", Process.GetCurrentProcess().ProcessName);
|
||||
|
||||
try
|
||||
{
|
||||
var exceptionData = new ExceptionData
|
||||
{
|
||||
Exception = logEvent.Exception,
|
||||
Component = logEvent.LoggerName,
|
||||
Message = logEvent.FormattedMessage,
|
||||
};
|
||||
|
||||
if (UserId != null)
|
||||
{
|
||||
exceptionData.UserId = UserId.Render(logEvent);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
ExceptronClient.SubmitException(exceptionData);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
InternalLogger.Warn("Unable to report exception. {0}", e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue