public static ClsLogGroupList Format(List <LogEvent> logEvents, RenderingConfiguration renderingConfiguration, IFormatProvider formatProvider)
        {
            if (logEvents == null)
            {
                throw new ArgumentNullException(nameof(logEvents));
            }

            var logGroup = new ClsLogGroup();

            foreach (var logEvent in logEvents)
            {
                var log = new ClsLog();

                try {
                    LogEventEnricherManager.Enricher(logEvent);
                    var contents = GetLogEventContents(logEvent, renderingConfiguration, formatProvider);
                    log.Contents.AddRange(contents);
                    log.Time = logEvent.Timestamp.ToUnixTimeMilliseconds();
                }
                catch (Exception e) {
                    LogNonFormattableEvent(logEvent, e);
                }

                logGroup.Logs.Add(log);
            }

            var logGroupList = new ClsLogGroupList();

            logGroupList.LogGroupList_.Add(logGroup);

            return(logGroupList);
        }
 /// <summary>
 /// Set enricher
 /// </summary>
 /// <param name="enricher"></param>
 public void SetEnricher(Maybe <ILogEventEnricher> enricher)
 {
     if (enricher.HasValue)
     {
         LogEventEnricherManager.UpdateEnricher(enricher.Value);
     }
 }
Beispiel #3
0
 /// <summary>
 /// Set enricher
 /// </summary>
 /// <param name="enricher"></param>
 public void SetEnricher(ILogEventEnricher enricher)
 {
     if (enricher != null)
     {
         LogEventEnricherManager.UpdateEnricher(enricher);
     }
 }
        /// <inheritdoc />
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var logger         = log4net.LogManager.GetLogger(payload.SourceType);

                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);
                    }

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        stringBuilder.AppendLine("Extra properties:");
                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null)
                            {
                                stringBuilder.AppendLine($"    {property}");
                            }
                        }
                    }

                    switch (logEvent.Level)
                    {
                    case LogEventLevel.Verbose:
                    case LogEventLevel.Debug:
                        logger.Debug(stringBuilder.ToString(), logEvent.Exception);
                        break;

                    case LogEventLevel.Information:
                        logger.Info(stringBuilder.ToString(), logEvent.Exception);
                        break;

                    case LogEventLevel.Warning:
                        logger.Warn(stringBuilder.ToString(), logEvent.Exception);
                        break;

                    case LogEventLevel.Error:
                        logger.Error(stringBuilder.ToString(), logEvent.Exception);
                        break;

                    case LogEventLevel.Fatal:
                        logger.Fatal(stringBuilder.ToString(), logEvent.Exception);
                        break;

                    default:
                        logger.Info(stringBuilder.ToString(), logEvent.Exception);
                        break;
                    }
                }
            }

            return(Tasks.CompletedTask());
        }
        /// <inheritdoc />
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var source         = payload.SourceType.FullName;
                using (var logger = ExceptionlessClient.Default) {
                    foreach (var logEvent in legalityEvents)
                    {
                        LogEventEnricherManager.Enricher(logEvent);
                        var exception     = logEvent.Exception;
                        var level         = LogLevelSwitcher.Switch(logEvent.Level);
                        var stringBuilder = new StringBuilder();
                        using (var output = new StringWriter(stringBuilder, _formatProvider)) {
                            logEvent.RenderMessage(output, _sinkConfiguration.Rendering, _formatProvider);
                        }

                        var builder = logger.CreateLog(source, stringBuilder.ToString(), level);
                        builder.Target.Date = logEvent.Timestamp;

                        if (level == LogLevel.Fatal)
                        {
                            builder.MarkAsCritical();
                        }

                        if (exception != null)
                        {
                            builder.SetException(exception);
                        }

                        var legalityOpts = logEvent.GetAdditionalOperations(typeof(ExceptionlessPayloadClient), AdditionalOperationTypes.ForLogSink);
                        foreach (var opt in legalityOpts)
                        {
                            if (opt.GetOpt() is Func <EventBuilder, EventBuilder> eventBuilderFunc)
                            {
                                eventBuilderFunc.Invoke(builder);
                            }
                        }

                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null)
                            {
                                builder.SetProperty(property.Name, property.Value);
                            }
                        }

                        builder.PluginContextData.MergeContextData(logEvent.ContextData);

                        builder.Submit();
                    }
                }
            }

            return(Tasks.CompletedTask());
        }
Beispiel #6
0
        /// <inheritdoc />
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();

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

                    var strategyWrappers = NavigationFilterProcessor.GetValues(logEvent.StateNamespace);

                    if (strategyWrappers == null || !strategyWrappers.Any())
                    {
                        continue;
                    }

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

                    foreach (var strategy in strategyWrappers.Select(x => x.SavingStrategy))
                    {
                        var targetFilePath = strategy.CheckAndGetFilePath(logEvent);
                        if (string.IsNullOrWhiteSpace(targetFilePath))
                        {
                            continue;
                        }

                        //判断是否需要渲染 extra properties
                        //检查token

                        //渲染OutputTemplate
                        var stringBuilder = new StringBuilder();
                        using (var output = new StringWriter(stringBuilder, _formatProvider)) {
                            OutputTemplateRenderer.Render(strategy.FormattingStrategy.OutputTemplate, output, logEvent, targetMessageBuilder);
                        }

                        //写文件
                        if (_fileAstronautCache.TryGetFileAstronaut(strategy, targetFilePath, out var astronaut))
                        {
                            using (FileAstronautRemover.UsingRegister(targetFilePath, astronaut)) {
                                astronaut.Save(stringBuilder);
                            }
                        }
                    }
                }
            }

            return(Tasks.CompletedTask());
        }
Beispiel #7
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);
            }
        }
        /// <inheritdoc />
        public async Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var logger         = TomatoClientManager.Get();

                if (logger == null)
                {
                    return;
                }

                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 content = logEvent.ContextData?.ToString();

                    dynamic dynamicObj = null;

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        dynamicObj = new ExpandoObject();
                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null)
                            {
                                dynamicObj[property.Name] = property.Value.ToString();
                            }
                        }
                    }

                    await logger.WriteLogAsync(
                        logEvent.EventId.GetIntegerEventId(),
                        LogLevelSwitcher.Switch(logEvent.Level),
                        stringBuilder.ToString(),
                        content,
                        dynamicObj);
                }
            }
        }
Beispiel #9
0
        /// <inheritdoc />
        public async Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var logger         = Internals.JdCloudLogClientManager.GetLogClient(payload.Name, out var logStreamName);

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

                var jdcloudLogGroup = new JdCloudLogGroup {
                    Stream = logStreamName,
                };

                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 jdcloudLogInfo = new JdCloudLogInfo {
                        Content   = stringBuilder.ToString(),
                        Stream    = logStreamName,
                        Timestamp = logEvent.Timestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") //2019-04-08T03:08:04.123Z
                    };

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        jdcloudLogInfo.Tags = logEvent.ExtraProperties
                                              .Select(extra => extra.Value)
                                              .ToDictionary(property => property.Name, property => property.Value.ToString());
                    }

                    jdcloudLogGroup.Entries.Add(jdcloudLogInfo);
                }

                var response = await logger.Put(jdcloudLogGroup);

                ProcessError(response);
            }
        }
        /// <inheritdoc />
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var ix             = 0;
                var count          = legalityEvents.Count.ToString().Length;

                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);
                    }

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        stringBuilder.AppendLine();
                        stringBuilder.AppendLine("Extra properties:");
                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null)
                            {
                                stringBuilder.AppendLine($"    {property}");
                            }
                        }
                    }

//                    foreach (var token in logEvent.MessageTemplate.Tokens) {
//                        Console.WriteLine($"token={token}, type={token.TokenRenderType}, rawString={token.RawText}, tokenString={token.ToText()}");
//                    }

                    Console.WriteLine($@"
[{PadLeftByZero()(ix++)(count)('0')}] -- [{payload.Name}] -- [{GetLevelName()(logEvent.Level)}] 
{stringBuilder}");
                }
            }

            return(Tasks.CompletedTask());
        }
        /// <inheritdoc />
        public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default)
        {
            if (payload != null)
            {
                var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList();
                var logger         = global::NLog.LogManager.GetLogger(payload.Name, payload.SourceType);

                foreach (var logEvent in legalityEvents)
                {
                    LogEventEnricherManager.Enricher(logEvent);
                    var exception     = logEvent.Exception;
                    var level         = LogLevelSwitcher.Switch(logEvent.Level);
                    var stringBuilder = new StringBuilder();
                    using (var output = new StringWriter(stringBuilder, _formatProvider)) {
                        logEvent.RenderMessage(output, _sinkConfiguration.Rendering, _formatProvider);
                    }

                    if (logEvent.ExtraProperties.Count > 0)
                    {
                        stringBuilder.AppendLine("Extra properties:");
                        foreach (var extra in logEvent.ExtraProperties)
                        {
                            var property = extra.Value;
                            if (property != null)
                            {
                                stringBuilder.AppendLine($"    {property}");
                            }
                        }
                    }

                    logger.Log(level, exception, stringBuilder.ToString());
                }
            }

            return(Tasks.CompletedTask());
        }