Skip to content

Commit

Permalink
Issue #4. Allow for LogEventInfo Properties to be passed as Tags inst…
Browse files Browse the repository at this point in the history
…ead of Extra
  • Loading branch information
Ryan Anthony committed Mar 2, 2016
1 parent f0bb65f commit 53001d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
52 changes: 52 additions & 0 deletions NLog.Targets.Sentry.UnitTests/SentryTargetTests.cs
Expand Up @@ -85,5 +85,57 @@ public void TestLoggingToSentry()
Assert.IsTrue(lTags == null);
Assert.IsTrue(lErrorLevel == ErrorLevel.Error);
}




[Test]
public void TestLoggingToSentry_SendLogEventInfoPropertiesAsTags()
{
var sentryClient = new Mock<IRavenClient>();
ErrorLevel lErrorLevel = ErrorLevel.Debug;
IDictionary<string, string> lTags = null;
Exception lException = null;

sentryClient
.Setup(x => x.CaptureException(It.IsAny<Exception>(), It.IsAny<SentryMessage>(), It.IsAny<ErrorLevel>(), It.IsAny<IDictionary<string, string>>(), It.IsAny<object>()))
.Callback((Exception exception, SentryMessage msg, ErrorLevel lvl, IDictionary<string, string> d, object extra) =>
{
lException = exception;
lErrorLevel = lvl;
lTags = d;
})
.Returns("Done");

// Setup NLog
var sentryTarget = new SentryTarget(sentryClient.Object)
{
Dsn = "http://25e27038b1df4930b93c96c170d95527:d87ac60bb07b4be8908845b23e914dae@test/4",
SendLogEventInfoPropertiesAsTags = true,
};
var configuration = new LoggingConfiguration();
configuration.AddTarget("NLogSentry", sentryTarget);
configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, sentryTarget));
LogManager.Configuration = configuration;

var tag1Value = "abcde";

try
{
throw new Exception("Oh No!");
}
catch (Exception e)
{
var logger = LogManager.GetCurrentClassLogger();

var logEventInfo = LogEventInfo.Create(LogLevel.Error, "default", "Error Message", e);
logEventInfo.Properties.Add("tag1", tag1Value);
logger.Log(logEventInfo);
}

Assert.IsTrue(lException.Message == "Oh No!");
Assert.IsTrue(lTags != null);
Assert.IsTrue(lErrorLevel == ErrorLevel.Error);
}
}
}
18 changes: 15 additions & 3 deletions NLog.Targets.Sentry/SentryTarget.cs
Expand Up @@ -44,6 +44,11 @@ public string Dsn
/// </summary>
public bool IgnoreEventsWithNoException { get; set; }

/// <summary>
/// Determines whether event properites will be sent to sentry as Tags or not
/// </summary>
public bool SendLogEventInfoPropertiesAsTags { get; set; }

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -69,20 +74,27 @@ protected override void Write(LogEventInfo logEvent)
{
try
{
var extras = logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString());
var tags = SendLogEventInfoPropertiesAsTags
? logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString())
: null;

var extras = SendLogEventInfoPropertiesAsTags
? null
: logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value.ToString());

client.Value.Logger = logEvent.LoggerName;

// If the log event did not contain an exception and we're not ignoring
// those kinds of events then we'll send a "Message" to Sentry
if (logEvent.Exception == null && !IgnoreEventsWithNoException)
{
var sentryMessage = new SentryMessage(Layout.Render(logEvent));
client.Value.CaptureMessage(sentryMessage, LoggingLevelMap[logEvent.Level], extra: extras);
client.Value.CaptureMessage(sentryMessage, LoggingLevelMap[logEvent.Level], extra: extras, tags: tags);
}
else if (logEvent.Exception != null)
{
var sentryMessage = new SentryMessage(logEvent.FormattedMessage);
client.Value.CaptureException(logEvent.Exception, extra: extras, level: LoggingLevelMap[logEvent.Level], message: sentryMessage);
client.Value.CaptureException(logEvent.Exception, extra: extras, level: LoggingLevelMap[logEvent.Level], message: sentryMessage, tags: tags);
}
}
catch (Exception e)
Expand Down

0 comments on commit 53001d1

Please sign in to comment.