@@ -13,25 +13,42 @@ namespace Sentry;
1313[ Experimental ( DiagnosticId . ExperimentalFeature ) ]
1414public sealed class SentryStructuredLogger
1515{
16+ private readonly IHub _hub ;
1617 private readonly ISystemClock _clock ;
1718
18- internal SentryStructuredLogger ( )
19- : this ( SystemClock . Clock )
19+ private readonly SentryOptions ? _options ;
20+ private readonly IInternalScopeManager ? _scopeManager ;
21+
22+ internal SentryStructuredLogger ( IHub hub )
23+ : this ( hub , SystemClock . Clock )
24+ {
25+ }
26+
27+ internal SentryStructuredLogger ( IHub hub , ISystemClock clock )
28+ : this ( hub , ( hub as Hub ) ? . ScopeManager , hub . GetSentryOptions ( ) , clock )
2029 {
2130 }
2231
23- internal SentryStructuredLogger ( ISystemClock clock )
32+ internal SentryStructuredLogger ( IHub hub , IInternalScopeManager ? scopeManager , SentryOptions ? options , ISystemClock clock )
2433 {
34+ _hub = hub ;
2535 _clock = clock ;
36+
37+ _options = options ;
38+ _scopeManager = scopeManager ;
39+ IsEnabled = options is { EnableLogs : true } ;
2640 }
2741
42+ [ MemberNotNullWhen ( true , nameof ( _options ) ) ]
43+ private bool IsEnabled { get ; }
44+
2845 /// <summary>
2946 /// Creates and sends a structured log to Sentry, with severity <see cref="SentryLogLevel.Trace"/>, when enabled and sampled.
3047 /// <para>This API is experimental and it may change in the future.</para>
3148 /// </summary>
3249 public void LogTrace ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
3350 {
34- if ( IsEnabled ( ) )
51+ if ( IsEnabled )
3552 {
3653 CaptureLog ( SentryLogLevel . Trace , template , parameters , configureLog ) ;
3754 }
@@ -44,7 +61,7 @@ public void LogTrace(string template, object[]? parameters = null, Action<Sentry
4461 [ Experimental ( DiagnosticId . ExperimentalFeature ) ]
4562 public void LogDebug ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
4663 {
47- if ( IsEnabled ( ) )
64+ if ( IsEnabled )
4865 {
4966 CaptureLog ( SentryLogLevel . Debug , template , parameters , configureLog ) ;
5067 }
@@ -57,7 +74,7 @@ public void LogDebug(string template, object[]? parameters = null, Action<Sentry
5774 [ Experimental ( DiagnosticId . ExperimentalFeature ) ]
5875 public void LogInfo ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
5976 {
60- if ( IsEnabled ( ) )
77+ if ( IsEnabled )
6178 {
6279 CaptureLog ( SentryLogLevel . Info , template , parameters , configureLog ) ;
6380 }
@@ -70,7 +87,7 @@ public void LogInfo(string template, object[]? parameters = null, Action<SentryL
7087 [ Experimental ( DiagnosticId . ExperimentalFeature ) ]
7188 public void LogWarning ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
7289 {
73- if ( IsEnabled ( ) )
90+ if ( IsEnabled )
7491 {
7592 CaptureLog ( SentryLogLevel . Warning , template , parameters , configureLog ) ;
7693 }
@@ -83,7 +100,7 @@ public void LogWarning(string template, object[]? parameters = null, Action<Sent
83100 [ Experimental ( DiagnosticId . ExperimentalFeature ) ]
84101 public void LogError ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
85102 {
86- if ( IsEnabled ( ) )
103+ if ( IsEnabled )
87104 {
88105 CaptureLog ( SentryLogLevel . Error , template , parameters , configureLog ) ;
89106 }
@@ -96,57 +113,37 @@ public void LogError(string template, object[]? parameters = null, Action<Sentry
96113 [ Experimental ( DiagnosticId . ExperimentalFeature ) ]
97114 public void LogFatal ( string template , object [ ] ? parameters = null , Action < SentryLog > ? configureLog = null )
98115 {
99- if ( IsEnabled ( ) )
116+ if ( IsEnabled )
100117 {
101118 CaptureLog ( SentryLogLevel . Fatal , template , parameters , configureLog ) ;
102119 }
103120 }
104121
105- private bool IsEnabled ( )
106- {
107- var hub = SentrySdk . CurrentHub ;
108-
109- if ( hub . GetSentryOptions ( ) is { } options )
110- {
111- return options . EnableLogs ;
112- }
113-
114- return false ;
115- }
116-
117122 private void CaptureLog ( SentryLogLevel level , string template , object [ ] ? parameters , Action < SentryLog > ? configureLog )
118123 {
119- var timestamp = _clock . GetUtcNow ( ) ;
120-
121- var hub = SentrySdk . CurrentHub ;
122-
123- if ( hub . GetSentryOptions ( ) is not { EnableLogs : true } options )
124- {
125- return ;
126- }
124+ Debug . Assert ( _options is not null ) ;
127125
128- var scopeManager = ( hub as Hub ) ? . ScopeManager ;
126+ var timestamp = _clock . GetUtcNow ( ) ;
129127
130- if ( ! TryGetTraceId ( hub , scopeManager , out var traceId ) )
128+ if ( ! TryGetTraceId ( _hub , _scopeManager , out var traceId ) )
131129 {
132- options . DiagnosticLogger ? . LogWarning ( "TraceId not found" ) ;
130+ _options . DiagnosticLogger ? . LogWarning ( "TraceId not found" ) ;
133131 }
134132
135- _ = TryGetParentSpanId ( hub , scopeManager , out var parentSpanId ) ;
133+ _ = TryGetParentSpanId ( _hub , _scopeManager , out var parentSpanId ) ;
136134
137- var message = string . Format ( template , parameters ?? [ ] ) ;
135+ var message = string . Format ( CultureInfo . InvariantCulture , template , parameters ?? [ ] ) ;
138136 SentryLog log = new ( timestamp , traceId , level , message )
139137 {
140138 Template = template ,
141139 Parameters = ImmutableArray . Create ( parameters ) ,
140+ ParentSpanId = parentSpanId ,
142141 } ;
143- log . SetAttributes ( options , parentSpanId ) ;
142+ log . SetAttributes ( _options ) ;
144143
145- SentryLog ? configuredLog ;
146144 try
147145 {
148146 configureLog ? . Invoke ( log ) ;
149- configuredLog = options . BeforeSendLogInternal ? . Invoke ( log ) ;
150147 }
151148 catch ( Exception e )
152149 {
@@ -156,11 +153,27 @@ private void CaptureLog(SentryLogLevel level, string template, object[]? paramet
156153 return ;
157154 }
158155
156+ var configuredLog = log ;
157+ if ( _options . BeforeSendLogInternal is { } beforeSendLog )
158+ {
159+ try
160+ {
161+ configuredLog = beforeSendLog . Invoke ( log ) ;
162+ }
163+ catch ( Exception e )
164+ {
165+ //TODO: change to Diagnostic Logger (if enabled)
166+ // see https://github.com/getsentry/sentry-dotnet/issues/4132
167+ Console . WriteLine ( e ) ;
168+ return ;
169+ }
170+ }
171+
159172 if ( configuredLog is not null )
160173 {
161174 //TODO: enqueue in Batch-Processor / Background-Worker
162175 // see https://github.com/getsentry/sentry-dotnet/issues/4132
163- _ = hub . CaptureEnvelope ( Envelope . FromLog ( configuredLog ) ) ;
176+ _ = _hub . CaptureEnvelope ( Envelope . FromLog ( configuredLog ) ) ;
164177 }
165178 }
166179
0 commit comments