Skip to content

Commit 747cc22

Browse files
devigowillson556
authored andcommitted
Use Layout type instead of string for Dsn and Environment (#5)
1) Used Layout type instead of string for Dsn and Environment 2) Updated NLog to 4.5.11
1 parent 1acebb8 commit 747cc22

4 files changed

Lines changed: 48 additions & 41 deletions

File tree

NLog.Targets.Sentry.UnitTests/NLog.Targets.Sentry.UnitTests.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1011
<PackageReference Include="Moq" Version="4.8.2" />
1112
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
12-
<PackageReference Include="NLog" Version="4.5.2" />
13-
<PackageReference Include="NLog.Config" Version="4.5.2" />
14-
<PackageReference Include="NLog.Schema" Version="4.5.2" />
13+
<PackageReference Include="NLog" Version="4.5.11" />
14+
<PackageReference Include="NLog.Config" Version="4.5.11" />
15+
<PackageReference Include="NLog.Schema" Version="4.5.11" />
1516
<PackageReference Include="NUnit" Version="3.10.1" />
17+
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
1618
<PackageReference Include="SharpRaven" Version="2.4.0" />
1719
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.3.0" />
1820
</ItemGroup>

NLog.Targets.Sentry.UnitTests/SentryTargetTests.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Moq;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using Moq;
25
using NLog.Config;
36
using NUnit.Framework;
47
using SharpRaven;
58
using SharpRaven.Data;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Globalization;
99

1010
namespace NLog.Targets.Sentry.UnitTests
1111
{
@@ -24,6 +24,22 @@ public void Teardown()
2424
LogManager.ThrowExceptions = false;
2525
}
2626

27+
[Test]
28+
public void TestInitialization()
29+
{
30+
var sentryTarget = new SentryTarget
31+
{
32+
Dsn = "http://25e27038b1df4930b93c96c170d95527:d87ac60bb07b4be8908845b23e914dae@test/4",
33+
Environment = "Test",
34+
Timeout = "00:00:10",
35+
};
36+
37+
var logEventInfo = LogEventInfo.CreateNullEvent();
38+
Assert.AreEqual("http://25e27038b1df4930b93c96c170d95527:d87ac60bb07b4be8908845b23e914dae@test/4", sentryTarget.Dsn.Render(logEventInfo));
39+
Assert.AreEqual("Test", sentryTarget.Environment.Render(logEventInfo));
40+
Assert.AreEqual("00:00:10", sentryTarget.Timeout);
41+
}
42+
2743
[Test]
2844
public void TestPublicConstructor()
2945
{
@@ -40,14 +56,6 @@ public void TestPublicConstructor()
4056
});
4157
}
4258

43-
[Test]
44-
public void TestBadDsn()
45-
{
46-
// ReSharper disable ObjectCreationAsStatement
47-
Assert.Throws<ArgumentException>(() => new SentryTarget(null) { Dsn = "http://localhost" });
48-
// ReSharper restore ObjectCreationAsStatement
49-
}
50-
5159
[Test]
5260
public void TestLoggingToSentry()
5361
{

NLog.Targets.Sentry/NLog.Targets.Sentry.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<ItemGroup>
3030
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
31-
<PackageReference Include="NLog" Version="4.5.2" />
31+
<PackageReference Include="NLog" Version="4.5.11" />
3232
<PackageReference Include="SharpRaven" Version="2.4.0" />
3333
</ItemGroup>
3434
</Project>

NLog.Targets.Sentry/SentryTarget.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using NLog.Common;
2-
using NLog.Config;
3-
using SharpRaven;
4-
using SharpRaven.Data;
5-
using System;
1+
using System;
62
using System.Collections.Generic;
73
using System.Globalization;
84
using System.Linq;
9-
105
using System.Reflection;
6+
using NLog.Common;
7+
using NLog.Config;
8+
using NLog.Layouts;
9+
using SharpRaven;
10+
using SharpRaven.Data;
1111

1212
// ReSharper disable CheckNamespace
1313

@@ -17,7 +17,6 @@ namespace NLog.Targets
1717
[Target("Sentry")]
1818
public class SentryTarget : TargetWithLayout
1919
{
20-
private Dsn dsn;
2120
private TimeSpan clientTimeout;
2221
private LogLevel minLogLevelForEvent = LogLevel.Trace;
2322
private readonly Lazy<IRavenClient> client;
@@ -94,11 +93,7 @@ static SentryTarget()
9493
/// The DSN for the Sentry host
9594
/// </summary>
9695
[RequiredParameter]
97-
public string Dsn
98-
{
99-
get { return this.dsn?.ToString(); }
100-
set { this.dsn = new Dsn(value); }
101-
}
96+
public Layout Dsn { get; set; }
10297

10398
/// <summary>
10499
/// Gets or sets the minimum log level required to trigger a Sentry event.
@@ -115,7 +110,7 @@ public string MinLogLevelForEvent
115110
/// <summary>
116111
/// Gets or sets the environment name to send with the event logs.
117112
/// </summary>
118-
public string Environment { get; set; }
113+
public Layout Environment { get; set; }
119114

120115
/// <summary>
121116
/// Gets or sets the type of version number to send with the logs.
@@ -131,8 +126,8 @@ public string VersionNumberType
131126
/// </summary>
132127
public string Timeout
133128
{
134-
get { return this.clientTimeout.ToString("c"); }
135-
set { this.clientTimeout = TimeSpan.ParseExact(value, "c", CultureInfo.InvariantCulture); }
129+
get => clientTimeout.ToString("c");
130+
set => clientTimeout = TimeSpan.ParseExact(value, "c", CultureInfo.InvariantCulture);
136131
}
137132

138133
/// <summary>
@@ -150,7 +145,7 @@ public string Timeout
150145
/// </summary>
151146
public SentryTarget()
152147
{
153-
this.client = new Lazy<IRavenClient>(this.DefaultClientFactory);
148+
this.client = new Lazy<IRavenClient>(DefaultClientFactory);
154149
}
155150

156151
/// <summary>
@@ -172,7 +167,7 @@ protected override void Write(LogEventInfo logEvent)
172167
{
173168
if (logEvent.Level >= this.minLogLevelForEvent)
174169
{
175-
if (logEvent.Exception == null && this.IgnoreEventsWithNoException)
170+
if (logEvent.Exception == null && IgnoreEventsWithNoException)
176171
{
177172
return;
178173
}
@@ -183,16 +178,16 @@ protected override void Write(LogEventInfo logEvent)
183178
eventProperties = logEvent.Properties.ToDictionary(x => x.Key.ToString(), x => x.Value?.ToString());
184179
}
185180

186-
var tags = this.SendLogEventInfoPropertiesAsTags ? eventProperties : null;
187-
var extras = this.SendLogEventInfoPropertiesAsTags ? null : eventProperties;
181+
var tags = SendLogEventInfoPropertiesAsTags ? eventProperties : null;
182+
var extras = SendLogEventInfoPropertiesAsTags ? null : eventProperties;
188183

189184
this.client.Value.Logger = logEvent.LoggerName;
190185

191186
// If the log event did not contain an exception and we're not ignoring
192187
// those kinds of events then we'll send a "Message" to Sentry
193188
if (logEvent.Exception == null)
194189
{
195-
var sentryMessage = new SentryMessage(this.Layout.Render(logEvent));
190+
var sentryMessage = new SentryMessage(Layout.Render(logEvent));
196191
var msg = new SentryEvent(sentryMessage)
197192
{
198193
Level = LoggingLevelMap[logEvent.Level],
@@ -239,7 +234,7 @@ protected override void Write(LogEventInfo logEvent)
239234
}
240235
catch (Exception ex)
241236
{
242-
this.LogException(ex);
237+
LogException(ex);
243238
throw; // Notify NLog about failure, so fallback/retry can be performed
244239
}
245240
}
@@ -269,11 +264,13 @@ protected override void Dispose(bool disposing)
269264
/// <returns>New instance of a RavenClient.</returns>
270265
private IRavenClient DefaultClientFactory()
271266
{
272-
var ravenClient = new RavenClient(this.dsn)
267+
var renderedDsn = Dsn.Render(LogEventInfo.CreateNullEvent());
268+
var renderedEnvironment = Environment?.Render(LogEventInfo.CreateNullEvent());
269+
var ravenClient = new RavenClient(renderedDsn)
273270
{
274-
ErrorOnCapture = this.LogException,
271+
ErrorOnCapture = LogException,
275272
Timeout = this.clientTimeout,
276-
Environment = this.Environment,
273+
Environment = renderedEnvironment,
277274
Release = GetVersion(),
278275
};
279276

@@ -292,7 +289,7 @@ private IRavenClient DefaultClientFactory()
292289

293290
private string GetVersion()
294291
{
295-
switch (versionNumberType)
292+
switch (this.versionNumberType)
296293
{
297294
case Targets.VersionNumberType.AssemblyVersion:
298295
return RootAssembly?.GetName().Version.ToString();

0 commit comments

Comments
 (0)