Esempio n. 1
0
        /// <inheritdoc />
        public async Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var logger         = AliyunSlsClientManager.GetSlsClient(payload.Name, out var logStoreName);

                if (logger == null || string.IsNullOrWhiteSpace(logStoreName))
                {
                    return;
                }

                var aliyunLogGroup = new AliyunLogGroup {
                    Source = payload.SourceType.FullName,
                    Topic  = payload.Name,
                };

                foreach (var logEvent in legalityEvents)
                {
                    LogEventEnricherManager.Enricher(logEvent);

                    var stringBuilder = new StringBuilder();
                    using (var output = new StringWriter(stringBuilder, _formatProvider)) {
                        logEvent.RenderMessage(output, _sinkConfiguration.Rendering, _formatProvider);
                    }

                    var aliyunLogInfo = new AliyunLogInfo {
                        Contents = { { MessageBody, stringBuilder.ToString() } },
                        Time     = logEvent.Timestamp
                    };

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null &&
                                0 != string.Compare(property.Name, MessageBody, StringComparison.OrdinalIgnoreCase))
                            {
                                aliyunLogInfo.Contents.Add(property.Name, property.Value.ToString());
                            }
                        }
                    }

                    aliyunLogGroup.Logs.Add(aliyunLogInfo);
                }

                var response = await logger.PostLogStoreLogsAsync(new PostLogsRequest(logStoreName, aliyunLogGroup));

                ProcessError(response);
            }
        }
 public PostLogsRequest(String logstoreName, LogGroupInfo logGroup)
 {
     this.LogstoreName = logstoreName;
     this.LogGroup     = logGroup;
 }