Esempio n. 1
0
        private async Task RunUploader(CancellationToken cancellationToken)
        {
            TimeSpan errorDelay = _serverErrorBackoffSettings.Delay;

            while (true)
            {
                List <LogEntryExtra> entries;
                // Wait/loop until there are some log entries that need uploading.
                while (true)
                {
                    DateTimeRange logEntriesLost;
                    lock (_lockObj)
                    {
                        logEntriesLost  = _logEntriesLost;
                        _logEntriesLost = null;
                    }
                    entries = await _logQ.PeekAsync(_maxUploadBatchSize - (logEntriesLost != null ? 1 : 0), cancellationToken);

                    if (logEntriesLost != null)
                    {
                        var lostEntryExtra = new LogEntryExtra(-1, MakeLostEntry(logEntriesLost));
                        entries = (new[] { lostEntryExtra }).Concat(entries ?? Enumerable.Empty <LogEntryExtra>()).ToList();
                    }
                    if (entries != null && entries.Count() > 0)
                    {
                        // There are log entries that need uploading, so do that now.
                        break;
                    }
                    await _uploadReadyEvent.WaitAsync(cancellationToken);
                }
                // Upload entries to the Cloud Logging server
                try
                {
                    await _client.WriteLogEntriesAsync(null, null, s_emptyLabels, entries.Select(x => x.Entry), cancellationToken);

                    await _logQ.RemoveUntilAsync(entries.Last().Id, cancellationToken);

                    lock (_lockObj)
                    {
                        _maxConfirmedSentId = entries.Last().Id;
                    }
                    _uploadCompleteEvent.Set();
                    errorDelay = _serverErrorBackoffSettings.Delay;
                }
                catch (Exception e)
                {
                    // Always retry, regardless of error, as there's nothing much else to be done.
                    await _scheduler.Delay(errorDelay, cancellationToken);

                    errorDelay = new TimeSpan((long)(errorDelay.Ticks * _serverErrorBackoffSettings.DelayMultiplier));
                    if (errorDelay > _serverErrorBackoffSettings.MaxDelay)
                    {
                        errorDelay = _serverErrorBackoffSettings.MaxDelay;
                    }
                    // Use log4net internal logging to warn user of upload error.
                    // Internal logging can be enabled as described in the "Troubleshooting" section of the log4net FAQ.
                    log4net.Util.LogLog.Warn(typeof(LogUploader), "Error uploading log messages to server.", e);
                }
            }
        }
Esempio n. 2
0
        public void CreateNewLogging(string message, string logId, string type, IDictionary <string, string> entryLabels, LogSeverity severity = LogSeverity.Info)
        {
            // Prepare new log entry.
            LogEntry     logEntry       = new LogEntry();
            LogName      logName        = new LogName(projectId, logId);
            LogNameOneof logNameToWrite = LogNameOneof.From(logName);

            logEntry.LogName  = logName.ToString();
            logEntry.Severity = severity;

            // Create log entry message.
            string messageId     = DateTime.Now.Millisecond.ToString();
            string entrySeverity = logEntry.Severity.ToString().ToUpper();

            logEntry.TextPayload =
                $"{messageId} {entrySeverity}.Logging - {message}";

            MonitoredResource resource = new MonitoredResource
            {
                Type = type
            };

            // Add log entry to collection for writing. Multiple log entries can be added.
            IEnumerable <LogEntry> logEntries = new LogEntry[] { logEntry };

            // Write new log entry.
            client.WriteLogEntriesAsync(logNameToWrite, resource, entryLabels, logEntries);
        }
Esempio n. 3
0
 /// <inheritdoc />
 public void Receive(IEnumerable <LogEntry> logs)
 {
     if (!logs.Any())
     {
         return;
     }
     _client.WriteLogEntriesAsync(null, null, EmptyDictionary, logs);
 }
Esempio n. 4
0
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            var logEntries = new List <LogEntry>();

            foreach (var e in events)
            {
                var entry = new LogEntry
                {
                    LogName   = _logName,
                    Severity  = TranslateSeverity(e.Level),
                    Timestamp = Timestamp.FromDateTimeOffset(e.Timestamp)
                };

                if (_sinkOptions.UseJsonOutput)
                {
                    var jsonStruct = new Struct();

                    if (_errorReportingEnabled && entry.Severity == LogSeverity.Error && e.Exception != null)
                    {
                        jsonStruct.Fields.Add("message", Value.ForString(RenderEventMessage(e) + "\n" + RenderException(e.Exception)));

                        var serviceContextStruct = new Struct();
                        jsonStruct.Fields.Add("serviceContext", Value.ForStruct(serviceContextStruct));

                        serviceContextStruct.Fields.Add("service", Value.ForString(_sinkOptions.ErrorReportingServiceName));
                        serviceContextStruct.Fields.Add("version", Value.ForString(_sinkOptions.ErrorReportingServiceVersion));
                    }
                    else
                    {
                        jsonStruct.Fields.Add("message", Value.ForString(RenderEventMessage(e)));
                    }

                    var propertiesStruct = new Struct();
                    jsonStruct.Fields.Add("properties", Value.ForStruct(propertiesStruct));

                    foreach (var property in e.Properties)
                    {
                        WritePropertyAsJson(entry, propertiesStruct, property.Key, property.Value);
                    }

                    entry.JsonPayload = jsonStruct;
                }
                else
                {
                    entry.TextPayload = RenderEventMessage(e);

                    foreach (var property in e.Properties)
                    {
                        WritePropertyAsLabel(entry, property.Key, property.Value);
                    }
                }

                logEntries.Add(entry);
            }

            await _client.WriteLogEntriesAsync(_logNameToWrite, _resource, _sinkOptions.Labels, logEntries);
        }
Esempio n. 5
0
 /// <inheritdoc />
 public Task ReceiveAsync(
     IEnumerable <LogEntry> logs, CancellationToken cancellationToken = default)
 {
     GaxPreconditions.CheckNotNull(logs, nameof(logs));
     if (!logs.Any())
     {
         return(CommonUtils.CompletedTask);
     }
     return(_client.WriteLogEntriesAsync((LogName)null, null, null, logs, cancellationToken));
 }
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            using var writer = new StringWriter();
            var entries = events.Select(e => CreateLogEntry(e, writer)).ToList();

            if (entries.Count > 0)
            {
                await _client.WriteLogEntriesAsync((LogNameOneof)null, _resource, _sinkOptions.Labels, entries, CancellationToken.None);
            }
        }
Esempio n. 7
0
 /// <inheritdoc />
 public Task ReceiveAsync(
     IEnumerable <LogEntry> logs, CancellationToken cancellationToken = default(CancellationToken))
 {
     GaxPreconditions.CheckNotNull(logs, nameof(logs));
     if (!logs.Any())
     {
         return(Task.CompletedTask);
     }
     return(_client.WriteLogEntriesAsync(null, null, EmptyDictionary, logs, cancellationToken));
 }
Esempio n. 8
0
        private async Task RunUploader()
        {
            TimeSpan errorDelay = _serverErrorBackoffSettings.Delay;

            while (true)
            {
                IEnumerable <LogEntryExtra> entries;
                // Wait/loop until there are some log entries that need uploading.
                // TODO: See if log4net can be shutdown. If so, then this loop needs to terminate after
                // all log entries have been uploaded.
                while (true)
                {
                    DateTimeRange logEntriesLost;
                    lock (_lockObj)
                    {
                        logEntriesLost  = _logEntriesLost;
                        _logEntriesLost = null;
                    }
                    entries = await _logQ.PeekAsync(_maxUploadBatchSize - (logEntriesLost != null ? 1 : 0));

                    if (logEntriesLost != null)
                    {
                        var lostEntryExtra = new LogEntryExtra(-1, MakeLostEntry(logEntriesLost));
                        entries = (new[] { lostEntryExtra }).Concat(entries ?? Enumerable.Empty <LogEntryExtra>());
                    }
                    if (entries != null && entries.Count() > 0)
                    {
                        // There are log entries that need uploading, so do that now.
                        break;
                    }
                    await _uploadReadyEvent.WaitAsync();
                }
                // Upload entries to the Cloud Logging server
                try
                {
                    await _client.WriteLogEntriesAsync("", null, s_emptyLabels, entries.Select(x => x.Entry));

                    await _logQ.RemoveUntilAsync(entries.Last().Id);

                    _emptyEvent.Set();
                    errorDelay = _serverErrorBackoffSettings.Delay;
                }
                catch (Exception)
                {
                    // Always retry, regardless of error, as there's nothing much else to be done.
                    await _scheduler.Delay(errorDelay);

                    errorDelay = errorDelay.Scale(_serverErrorBackoffSettings.DelayMultiplier);
                    if (errorDelay > _serverErrorBackoffSettings.MaxDelay)
                    {
                        errorDelay = _serverErrorBackoffSettings.MaxDelay;
                    }
                }
            }
        }
 protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
 {
     using (var writer = new StringWriter())
     {
         var entries = events.Select(e => CreateLogEntry(e, writer)).ToList();
         if (entries.Count > 0)
         {
             await _client.WriteLogEntriesAsync(_logNameToWrite, _resource, _sinkOptions.Labels, entries);
         }
     }
 }
Esempio n. 10
0
        // Note: calls to ConfigureAwait(false) aren't strictly required here,
        // as this method is only ever called in Task.Run, so there won't be a synchronization
        // context, but it's simple and harmless to make it "obviously okay".
        private async Task RunUploader(CancellationToken cancellationToken)
        {
            TimeSpan errorDelay = _serverErrorRetrySettings.InitialBackoff;

            while (true)
            {
                List <LogEntryExtra> entries;
                // Wait/loop until there are some log entries that need uploading.
                while (true)
                {
                    var peek = await _logQ.PeekAsync(_maxUploadBatchSize, cancellationToken).ConfigureAwait(false);

                    var logEntriesLost = peek.Lost;
                    entries = peek.Entries;
                    if (logEntriesLost != null)
                    {
                        var lostEntryExtra = new LogEntryExtra(-1, MakeLostEntry(logEntriesLost));
                        entries = (new[] { lostEntryExtra }).Concat(entries ?? Enumerable.Empty <LogEntryExtra>()).ToList();
                    }
                    if (entries != null && entries.Count() > 0)
                    {
                        // There are log entries that need uploading, so do that now.
                        break;
                    }
                    await _uploadReadyEvent.WaitAsync(cancellationToken).ConfigureAwait(false);
                }
                // Upload entries to the Cloud Logging server
                try
                {
                    await _client.WriteLogEntriesAsync((LogName)null, null, s_emptyLabels, entries.Select(x => x.Entry), cancellationToken).ConfigureAwait(false);

                    await _logQ.RemoveUntilAsync(entries.Last().Id, cancellationToken).ConfigureAwait(false);

                    lock (_lock)
                    {
                        _maxConfirmedSentId = entries.Last().Id;
                    }
                    _uploadCompleteEvent.Set();
                    errorDelay = _serverErrorRetrySettings.InitialBackoff;
                }
                catch (Exception e)
                {
                    // Always retry, regardless of error, as there's nothing much else to be done.
                    await _scheduler.Delay(errorDelay, cancellationToken).ConfigureAwait(false);

                    // TODO: Try to use RetryAttempt for this.
                    errorDelay = _serverErrorRetrySettings.NextBackoff(errorDelay);
                    // Use log4net internal logging to warn user of upload error.
                    // Internal logging can be enabled as described in the "Troubleshooting" section of the log4net FAQ.
                    log4net.Util.LogLog.Warn(typeof(LogUploader), "Error uploading log messages to server.", e);
                }
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Method to run Health Check for google Stack Driver Telemetry (not yet implemented)
 /// </summary>
 /// <param name="serviceKey"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public override LightHealth.HealthCheck HealthCheck(string serviceKey, string value)
 {
     try
     {
         clientV2.WriteLogEntriesAsync(LogNameOneof.From(logName), null, null, null);
         return(LightHealth.HealthCheck.Healthy);
     }
     catch
     {
         return(LightHealth.HealthCheck.Unhealthy);
     }
 }
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            using var writer = new StringWriter();
            var entries = new List <LogEntry>();

            foreach (var e in events)
            {
                entries.Add(CreateLogEntry(e, writer));
            }

            if (entries.Count > 0)
            {
                await _client.WriteLogEntriesAsync((LogName)null, _resource, _sinkOptions.Labels, entries, CancellationToken.None);
            }
        }
        /// <summary>Snippet for WriteLogEntriesAsync</summary>
        public async Task WriteLogEntriesAsync_RequestObject()
        {
            // Snippet: WriteLogEntriesAsync(WriteLogEntriesRequest,CallSettings)
            // Create client
            LoggingServiceV2Client loggingServiceV2Client = await LoggingServiceV2Client.CreateAsync();

            // Initialize request argument(s)
            WriteLogEntriesRequest request = new WriteLogEntriesRequest
            {
                Entries = { },
            };
            // Make the request
            WriteLogEntriesResponse response = await loggingServiceV2Client.WriteLogEntriesAsync(request);

            // End snippet
        }
        public async Task WriteLogEntriesAsync()
        {
            // Snippet: WriteLogEntriesAsync(string,MonitoredResource,IDictionary<string, string>,IEnumerable<LogEntry>,CallSettings)
            // Additional: WriteLogEntriesAsync(string,MonitoredResource,IDictionary<string, string>,IEnumerable<LogEntry>,CancellationToken)
            // Create client
            LoggingServiceV2Client loggingServiceV2Client = LoggingServiceV2Client.Create();
            // Initialize request argument(s)
            string                       formattedLogName = LoggingServiceV2Client.FormatLogName("[PROJECT]", "[LOG]");
            MonitoredResource            resource         = new MonitoredResource();
            IDictionary <string, string> labels           = new Dictionary <string, string>();
            IEnumerable <LogEntry>       entries          = new List <LogEntry>();
            // Make the request
            WriteLogEntriesResponse response = await loggingServiceV2Client.WriteLogEntriesAsync(formattedLogName, resource, labels, entries);

            // End snippet
        }
Esempio n. 15
0
        public Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            using var writer = new StringWriter();
            var entries = new List <LogEntry>();

            foreach (var e in events)
            {
                entries.Add(CreateLogEntry(e, writer));
            }

            if (entries.Count > 0)
            {
                return(_client.WriteLogEntriesAsync((LogName)null, _resource, _sinkOptions.Labels, entries, CancellationToken.None));
            }

            return(Task.CompletedTask);
        }
Esempio n. 16
0
        protected override async Task EmitBatchAsync(IEnumerable <LogEvent> events)
        {
            var logEntries = new List <LogEntry>();

            foreach (var e in events)
            {
                var entry = new LogEntry
                {
                    LogName   = _logName,
                    Severity  = TranslateSeverity(e.Level),
                    Timestamp = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTimeOffset(e.Timestamp),
                };

                if (_messageTemplateTextFormatter != null)
                {
                    using (var stringWriter = new StringWriter())
                    {
                        _messageTemplateTextFormatter.Format(e, stringWriter);
                        entry.TextPayload = stringWriter.ToString();
                    }
                }
                else
                {
                    if (e.Exception == null)
                    {
                        entry.TextPayload = e.RenderMessage();
                    }
                    else
                    {
                        entry.TextPayload = e.Exception.ToString();
                    }
                }

                foreach (var property in e.Properties)
                {
                    WriteProperty(entry, property.Key, property.Value);
                }

                logEntries.Add(entry);
            }

            await _client.WriteLogEntriesAsync(_logNameToWrite, _resource, _sinkOptions.Labels, logEntries);
        }
Esempio n. 17
0
 private async Task WriteLogEntriesBegin(Task _, object state)
 {
     var logEntries = state as IList <LogEntry>;
     await _client.WriteLogEntriesAsync(_logNameToWrite, _resource, s_emptyLabels, logEntries, _cancelTokenSource.Token).ConfigureAwait(false);
 }