@@ -115,6 +115,7 @@ internal GoogleStackdriverAppender(LoggingServiceV2Client client,
115
115
private ILogQueue _logQ ;
116
116
private LogUploader _logUploader ;
117
117
private Task < long ? > _initIdTask ;
118
+ private bool _isActivated ;
118
119
private long _currentId = - 1 ; // Initialised here, in case Flush() is called before any log entries written.
119
120
120
121
private readonly List < Label > _resourceLabels = new List < Label > ( ) ;
@@ -192,6 +193,7 @@ public override void ActivateOptions()
192
193
_client , _scheduler , _clock ,
193
194
_logQ , logsLostWarningEntry , MaxUploadBatchSize ,
194
195
serverErrorBackoffSettings ) ;
196
+ _isActivated = true ;
195
197
}
196
198
197
199
private void ActivateLogIdAndResource ( )
@@ -383,13 +385,21 @@ private void Write(IEnumerable<LogEntry> logEntries)
383
385
/// <inheritdoc/>
384
386
protected override void Append ( LoggingEvent loggingEvent )
385
387
{
388
+ if ( ! _isActivated )
389
+ {
390
+ throw new InvalidOperationException ( $ "{ nameof ( ActivateOptions ) } () must be called before using this appender.") ;
391
+ }
386
392
var entries = new [ ] { BuildLogEntry ( loggingEvent ) } ;
387
393
Write ( entries ) ;
388
394
}
389
395
390
396
/// <inheritdoc/>
391
397
protected override void Append ( LoggingEvent [ ] loggingEvents )
392
398
{
399
+ if ( ! _isActivated )
400
+ {
401
+ throw new InvalidOperationException ( $ "{ nameof ( ActivateOptions ) } () must be called before using this appender.") ;
402
+ }
393
403
var entries = loggingEvents . Select ( x => BuildLogEntry ( x ) ) . ToList ( ) ;
394
404
Write ( entries ) ;
395
405
}
@@ -403,6 +413,10 @@ protected override void Append(LoggingEvent[] loggingEvents)
403
413
/// <returns>A task representing whether the flush completed within the timeout.</returns>
404
414
public Task < bool > FlushAsync ( TimeSpan timeout , CancellationToken cancellationToken = default ( CancellationToken ) )
405
415
{
416
+ if ( ! _isActivated )
417
+ {
418
+ throw new InvalidOperationException ( $ "{ nameof ( ActivateOptions ) } () must be called before using this appender.") ;
419
+ }
406
420
long untilId ;
407
421
lock ( _lock )
408
422
{
@@ -418,8 +432,14 @@ protected override void Append(LoggingEvent[] loggingEvents)
418
432
/// <param name="cancellationToken">The token to monitor for cancellation requests.
419
433
/// The default value is <see cref="CancellationToken.None"/>.</param>
420
434
/// <returns>Whether the flush completed within the timeout.</returns>
421
- public bool Flush ( TimeSpan timeout , CancellationToken cancellationToken = default ( CancellationToken ) ) =>
422
- Task . Run ( async ( ) => await FlushAsync ( timeout , cancellationToken ) . ConfigureAwait ( false ) ) . Result ;
435
+ public bool Flush ( TimeSpan timeout , CancellationToken cancellationToken = default ( CancellationToken ) )
436
+ {
437
+ if ( ! _isActivated )
438
+ {
439
+ throw new InvalidOperationException ( $ "{ nameof ( ActivateOptions ) } () must be called before using this appender.") ;
440
+ }
441
+ return Task . Run ( async ( ) => await FlushAsync ( timeout , cancellationToken ) . ConfigureAwait ( false ) ) . Result ;
442
+ }
423
443
424
444
/// <summary>
425
445
/// Dispose of this appender, by flushing locally buffer entries then closing the appender.
@@ -430,6 +450,11 @@ protected override void Append(LoggingEvent[] loggingEvents)
430
450
/// </remarks>
431
451
public void Dispose ( )
432
452
{
453
+ if ( ! _isActivated )
454
+ {
455
+ // Appender not activated, so Dispose is a nop.
456
+ return ;
457
+ }
433
458
bool flushSucceeded = Flush ( TimeSpan . FromSeconds ( DisposeTimeoutSeconds ) ) ;
434
459
if ( ! flushSucceeded )
435
460
{
@@ -442,6 +467,11 @@ public void Dispose()
442
467
protected override void OnClose ( )
443
468
{
444
469
base . OnClose ( ) ;
470
+ if ( ! _isActivated )
471
+ {
472
+ // Appender not activated, so Close is a nop.
473
+ return ;
474
+ }
445
475
_logUploader . Close ( ) ;
446
476
}
447
477
}
0 commit comments