/// <inheritdoc /> public async Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default) { if (payload != null) { var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList(); var logger = TencentCloudClsClientManager.GetClsClient(payload.Name, out var requestBaseUri, out var topicId); if (logger == null || string.IsNullOrWhiteSpace(requestBaseUri) || string.IsNullOrWhiteSpace(topicId)) { return; } var requestUri = $"http://{requestBaseUri}/structuredlog?topic_id={topicId}"; var clsLogPackage = TencentCloudClsFormatter.Format(legalityEvents, _sinkConfiguration.Rendering, _formatProvider); var content = new ByteArrayContent(clsLogPackage.ToByteArray()); content.Headers.Clear(); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType); var response = await logger.PostAsync(requestUri, content, cancellationToken); ProcessError(response, requestUri); } }
public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { 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) { 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(); } } } #if NET451 return(Task.FromResult(true)); #else return(Task.CompletedTask); #endif }
/// <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()); }
public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { if (payload != null) { var legalityEvents = LogEventSinkFilter.Filter(payload, _sinkConfiguration).ToList(); var ix = 0; var count = legalityEvents.Count; foreach (var logEvent in legalityEvents) { 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); //astronaut. ... Console.WriteLine("渲染模板为:" + strategy.FormattingStrategy.OutputTemplate.Text); Console.WriteLine("原始结果为:" + targetMessageBuilder); Console.WriteLine("渲染结果为:" + stringBuilder); } } } } } #if NET451 return(Task.FromResult(true)); #else return(Task.CompletedTask); #endif }
/// <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); } } }
/// <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()); }
public Task WriteAsync(ILogPayload payload, CancellationToken cancellationToken = default(CancellationToken)) { 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) { 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()); } } #if NET451 return(Task.FromResult(true)); #else return(Task.CompletedTask); #endif }