1- using NLog . Common ;
2- using NLog . Config ;
3- using SharpRaven ;
4- using SharpRaven . Data ;
5- using System ;
1+ using System ;
62using System . Collections . Generic ;
73using System . Globalization ;
84using System . Linq ;
9-
105using 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