diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ca69734..990eb50d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## 4.14.0 [unreleased] +### Features + +1. [#590](https://github.com/influxdata/influxdb-client-csharp/pull/590): Allows disable Trace verbose messages + ### Dependencies Update dependencies: diff --git a/Client.Core.Test/AbstractTest.cs b/Client.Core.Test/AbstractTest.cs index d37163755..fffd93f3e 100644 --- a/Client.Core.Test/AbstractTest.cs +++ b/Client.Core.Test/AbstractTest.cs @@ -11,7 +11,11 @@ namespace InfluxDB.Client.Core.Test { public class AbstractTest { - private static readonly TraceListener ConsoleOutListener = new TextWriterTraceListener(Console.Out); + private static readonly TraceListener ConsoleOutListener = new TextWriterTraceListener(Console.Out) + { + Filter = InfluxDBTraceFilter.SuppressInfluxVerbose() + }; + private static readonly int DefaultWait = 10; private static readonly int DefaultInfluxDBSleep = 100; diff --git a/Client.Core/InfluxDBTraceFilter.cs b/Client.Core/InfluxDBTraceFilter.cs new file mode 100644 index 000000000..3eb5814c5 --- /dev/null +++ b/Client.Core/InfluxDBTraceFilter.cs @@ -0,0 +1,67 @@ +using System.Diagnostics; +using System.Linq; + +namespace InfluxDB.Client.Core +{ + /// + /// The is used to filter client trace messages by category. + /// + public class InfluxDBTraceFilter : TraceFilter + { + public const string CategoryInflux = "influx-client"; + public const string CategoryInfluxError = "influx-client-error"; + public const string CategoryInfluxQuery = "influx-client-query"; + public const string CategoryInfluxQueryError = "influx-client-query-error"; + public const string CategoryInfluxWrite = "influx-client-write"; + public const string CategoryInfluxWriteError = "influx-client-write-error"; + public const string CategoryInfluxLogger = "influx-client-logger"; + + private readonly string[] _categoryToFilter; + private readonly bool _keep; + + public InfluxDBTraceFilter(string[] categoryToFilter, bool keep) + { + _categoryToFilter = categoryToFilter; + _keep = keep; + } + + public override bool ShouldTrace(TraceEventCache eventCache, string source, TraceEventType eventType, int id, + string formatOrMessage, object[] args, object data, object[] dataArray) + { + return _categoryToFilter.Any(x => x == source) ^ _keep; + } + + /// + /// Suppress all client trace messages. + /// + /// Trace Filter + public static InfluxDBTraceFilter SuppressInflux() + { + return new InfluxDBTraceFilter(new string[] + { + CategoryInflux, + CategoryInfluxError, + CategoryInfluxQuery, + CategoryInfluxQueryError, + CategoryInfluxWrite, + CategoryInfluxWriteError, + CategoryInfluxLogger + }, false); + } + + /// + /// Suppress all client trace messages except , , . + /// + /// Trace Filter + public static InfluxDBTraceFilter SuppressInfluxVerbose() + { + return new InfluxDBTraceFilter(new string[] + { + CategoryInflux, + CategoryInfluxQuery, + CategoryInfluxWrite, + CategoryInfluxLogger + }, false); + } + } +} \ No newline at end of file diff --git a/Client.Core/Internal/AbstractQueryClient.cs b/Client.Core/Internal/AbstractQueryClient.cs index c4d65b749..519586783 100644 --- a/Client.Core/Internal/AbstractQueryClient.cs +++ b/Client.Core/Internal/AbstractQueryClient.cs @@ -321,8 +321,9 @@ protected void CatchOrPropagateException(Exception exception, // if (IsCloseException(exception)) { - Trace.WriteLine("Socket closed by remote server or end of data"); - Trace.WriteLine(exception); + Trace.WriteLine("Socket closed by remote server or end of data", + InfluxDBTraceFilter.CategoryInfluxQueryError); + Trace.WriteLine(exception, InfluxDBTraceFilter.CategoryInfluxQueryError); } else { diff --git a/Client.Core/Internal/AbstractRestClient.cs b/Client.Core/Internal/AbstractRestClient.cs index 60692aae7..eee5220e9 100644 --- a/Client.Core/Internal/AbstractRestClient.cs +++ b/Client.Core/Internal/AbstractRestClient.cs @@ -25,7 +25,7 @@ protected async Task PingAsync(Task request) } catch (Exception e) { - Trace.WriteLine($"Error: {e.Message}"); + Trace.WriteLine($"Error: {e.Message}", InfluxDBTraceFilter.CategoryInfluxError); return false; } } diff --git a/Client.Core/Internal/EnumConverter.cs b/Client.Core/Internal/EnumConverter.cs index 39044aaa6..72cf8016b 100644 --- a/Client.Core/Internal/EnumConverter.cs +++ b/Client.Core/Internal/EnumConverter.cs @@ -16,7 +16,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist } catch (JsonSerializationException e) { - Trace.WriteLine($"Error converting enum value. Returning null. {e}"); + Trace.WriteLine($"Error converting enum value. Returning null. {e}", + InfluxDBTraceFilter.CategoryInfluxError); return null; } diff --git a/Client.Core/Internal/LoggingHandler.cs b/Client.Core/Internal/LoggingHandler.cs index 478167934..c5ae2c6e7 100644 --- a/Client.Core/Internal/LoggingHandler.cs +++ b/Client.Core/Internal/LoggingHandler.cs @@ -27,7 +27,7 @@ public void BeforeIntercept(RestRequest request) var isBody = Level == LogLevel.Body; var isHeader = isBody || Level == LogLevel.Headers; - Trace.WriteLine($"--> {request.Method} {request.Resource}"); + Trace.WriteLine($"--> {request.Method} {request.Resource}", InfluxDBTraceFilter.CategoryInfluxLogger); if (isHeader) { @@ -56,12 +56,12 @@ public void BeforeIntercept(RestRequest request) stringBody = body.Value.ToString(); } - Trace.WriteLine($"--> Body: {stringBody}"); + Trace.WriteLine($"--> Body: {stringBody}", InfluxDBTraceFilter.CategoryInfluxLogger); } } - Trace.WriteLine("--> END"); - Trace.WriteLine("-->"); + Trace.WriteLine("--> END", InfluxDBTraceFilter.CategoryInfluxLogger); + Trace.WriteLine("-->", InfluxDBTraceFilter.CategoryInfluxLogger); } public object AfterIntercept(int statusCode, Func> headers, object body) @@ -75,7 +75,7 @@ public object AfterIntercept(int statusCode, Func> var isBody = Level == LogLevel.Body; var isHeader = isBody || Level == LogLevel.Headers; - Trace.WriteLine($"<-- {statusCode}"); + Trace.WriteLine($"<-- {statusCode}", InfluxDBTraceFilter.CategoryInfluxLogger); if (isHeader) { @@ -101,11 +101,11 @@ public object AfterIntercept(int statusCode, Func> if (!string.IsNullOrEmpty(stringBody)) { - Trace.WriteLine($"<-- Body: {stringBody}"); + Trace.WriteLine($"<-- Body: {stringBody}", InfluxDBTraceFilter.CategoryInfluxLogger); } } - Trace.WriteLine("<-- END"); + Trace.WriteLine("<-- END", InfluxDBTraceFilter.CategoryInfluxLogger); return freshBody; } @@ -131,7 +131,7 @@ private void LogHeaders(IEnumerable headers, string direction, var value = string.Equals(emp.Name, "Authorization", StringComparison.OrdinalIgnoreCase) ? "***" : emp.Value; - Trace.WriteLine($"{direction} {type}: {emp.Name}={value}"); + Trace.WriteLine($"{direction} {type}: {emp.Name}={value}", InfluxDBTraceFilter.CategoryInfluxLogger); } } } diff --git a/Client/InfluxDBClient.cs b/Client/InfluxDBClient.cs index d990b15bc..6ab932297 100644 --- a/Client/InfluxDBClient.cs +++ b/Client/InfluxDBClient.cs @@ -359,8 +359,8 @@ public void Dispose() } catch (Exception e) { - Trace.WriteLine("The signout exception"); - Trace.WriteLine(e); + Trace.WriteLine("The signout exception", InfluxDBTraceFilter.CategoryInfluxError); + Trace.WriteLine(e, InfluxDBTraceFilter.CategoryInfluxError); } // diff --git a/Client/Internal/ApiClient.cs b/Client/Internal/ApiClient.cs index 5243d0107..a072a2e33 100644 --- a/Client/Internal/ApiClient.cs +++ b/Client/Internal/ApiClient.cs @@ -7,6 +7,7 @@ using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; +using InfluxDB.Client.Core; using InfluxDB.Client.Core.Internal; using RestSharp; using RestSharp.Authenticators; @@ -133,8 +134,8 @@ private void InitToken() } catch (IOException e) { - Trace.WriteLine("Cannot retrieve the Session token!"); - Trace.WriteLine(e); + Trace.WriteLine("Cannot retrieve the Session token!", InfluxDBTraceFilter.CategoryInfluxError); + Trace.WriteLine(e, InfluxDBTraceFilter.CategoryInfluxError); return; } diff --git a/Client/Internal/MeasurementMapper.cs b/Client/Internal/MeasurementMapper.cs index 236ccc33b..4dbca6164 100644 --- a/Client/Internal/MeasurementMapper.cs +++ b/Client/Internal/MeasurementMapper.cs @@ -97,7 +97,8 @@ internal PointData ToPoint(TM measurement, WritePrecision precision) } else { - Trace.WriteLine($"{value} is not supported as Timestamp"); + Trace.WriteLine($"{value} is not supported as Timestamp", + InfluxDBTraceFilter.CategoryInfluxError); } } else diff --git a/Client/Internal/RetryAttempt.cs b/Client/Internal/RetryAttempt.cs index d274ee9da..0d7eb2415 100644 --- a/Client/Internal/RetryAttempt.cs +++ b/Client/Internal/RetryAttempt.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Net; using System.Net.Sockets; +using InfluxDB.Client.Core; using InfluxDB.Client.Core.Exceptions; namespace InfluxDB.Client.Internal @@ -141,7 +142,8 @@ internal long GetRetryInterval() var retryInterval = (long)(rangeStart + (rangeStop - rangeStart) * _random.NextDouble()); Trace.WriteLine("The InfluxDB does not specify \"Retry-After\". " + - $"Use the default retryInterval: {retryInterval}"); + $"Use the default retryInterval: {retryInterval}" + , InfluxDBTraceFilter.CategoryInflux); return retryInterval; } diff --git a/Client/README.md b/Client/README.md index 83e23b9b4..4665d60c6 100644 --- a/Client/README.md +++ b/Client/README.md @@ -769,6 +769,33 @@ namespace Examples } ``` +## Filter trace verbose + +You can filter out verbose messages from `InfluxDB.Client` by using TraceListener. + +```cs +using System; +using System.Diagnostics; +using InfluxDB.Client.Core; + +namespace Examples +{ + public static class MyProgram + { + public static void Main() + { + TraceListener ConsoleOutListener = new TextWriterTraceListener(Console.Out) + { + Filter = CategoryTraceFilter.SuppressInfluxVerbose(), + }; + Trace.Listeners.Add(ConsoleOutListener); + + // My code ... + } + } +} +```` + ## Management API The client has following management API: diff --git a/Client/UsersApi.cs b/Client/UsersApi.cs index bb4899a24..2b9aa2226 100644 --- a/Client/UsersApi.cs +++ b/Client/UsersApi.cs @@ -307,7 +307,7 @@ public async Task MeUpdatePasswordAsync(string oldPassword, string newPassword, var me = await MeAsync(cancellationToken).ConfigureAwait(false); if (me == null) { - Trace.WriteLine("User is not authenticated."); + Trace.WriteLine("User is not authenticated.", InfluxDBTraceFilter.CategoryInfluxError); return; } diff --git a/Client/WriteApi.cs b/Client/WriteApi.cs index 944e2cf81..bff60a8e1 100644 --- a/Client/WriteApi.cs +++ b/Client/WriteApi.cs @@ -300,14 +300,17 @@ protected internal WriteApi( switch (notification.Kind) { case NotificationKind.OnNext: - Trace.WriteLine($"The batch item: {notification} was processed successfully."); + Trace.WriteLine($"The batch item: {notification} was processed successfully." + , InfluxDBTraceFilter.CategoryInfluxWrite); break; case NotificationKind.OnError: Trace.WriteLine( - $"The batch item wasn't processed successfully because: {notification.Exception}"); + $"The batch item wasn't processed successfully because: {notification.Exception}" + , InfluxDBTraceFilter.CategoryInfluxWriteError); break; default: - Trace.WriteLine($"The batch item: {notification} was processed"); + Trace.WriteLine($"The batch item: {notification} was processed" + , InfluxDBTraceFilter.CategoryInfluxWrite); break; } }, @@ -315,12 +318,14 @@ protected internal WriteApi( { Publish(new WriteRuntimeExceptionEvent(exception)); _disposed = true; - Trace.WriteLine($"The unhandled exception occurs: {exception}"); + Trace.WriteLine($"The unhandled exception occurs: {exception}" + , InfluxDBTraceFilter.CategoryInfluxWriteError); }, () => { _disposed = true; - Trace.WriteLine("The WriteApi was disposed."); + Trace.WriteLine("The WriteApi was disposed." + , InfluxDBTraceFilter.CategoryInfluxWrite); }); } @@ -337,7 +342,7 @@ internal void ReleaseAndClose(int millis = 30000) { _unsubscribeDisposeCommand.Dispose(); // avoid duplicate call to dispose - Trace.WriteLine("Flushing batches before shutdown."); + Trace.WriteLine("Flushing batches before shutdown.", InfluxDBTraceFilter.CategoryInfluxWrite); if (!_subject.IsDisposed) { @@ -572,7 +577,8 @@ internal override string ToLineProtocol() { if (!_point.HasFields()) { - Trace.WriteLine($"The point: ${_point} doesn't contains any fields, skipping"); + Trace.WriteLine($"The point: ${_point} doesn't contains any fields, skipping", + InfluxDBTraceFilter.CategoryInfluxWrite); return null; } @@ -603,7 +609,8 @@ internal override string ToLineProtocol() var point = _converter.ConvertToPointData(_measurement, Options.Precision); if (!point.HasFields()) { - Trace.WriteLine($"The point: ${point} doesn't contains any fields, skipping"); + Trace.WriteLine($"The point: ${point} doesn't contains any fields, skipping", + InfluxDBTraceFilter.CategoryInfluxWrite); return null; } diff --git a/Client/WriteApiAsync.cs b/Client/WriteApiAsync.cs index b64249c0f..bd926637b 100644 --- a/Client/WriteApiAsync.cs +++ b/Client/WriteApiAsync.cs @@ -411,7 +411,8 @@ private Task WriteData(string org, string bucket, WritePrecision precision, IEnu var sb = ToLineProtocolBody(data); if (sb.Length == 0) { - Trace.WriteLine($"The writes: {data} doesn't contains any Line Protocol, skipping"); + Trace.WriteLine($"The writes: {data} doesn't contains any Line Protocol, skipping", + InfluxDBTraceFilter.CategoryInfluxWrite); return Task.CompletedTask; } diff --git a/Client/Writes/Events.cs b/Client/Writes/Events.cs index 3124a2b12..14ae98db8 100644 --- a/Client/Writes/Events.cs +++ b/Client/Writes/Events.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using InfluxDB.Client.Api.Domain; +using InfluxDB.Client.Core; namespace InfluxDB.Client.Writes { @@ -18,7 +19,8 @@ public WriteSuccessEvent(string organization, string bucket, WritePrecision prec internal override void LogEvent() { - Trace.WriteLine("The data was successfully written to InfluxDB 2."); + Trace.WriteLine("The data was successfully written to InfluxDB 2.", + InfluxDBTraceFilter.CategoryInfluxWrite); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy