using System; using System.Diagnostics; using InfluxDB.Client.Api.Domain; namespace InfluxDB.Client.Writes { public abstract class InfluxDBEventArgs : EventArgs { internal abstract void LogEvent(); } public class WriteSuccessEvent : AbstractWriteEvent { public WriteSuccessEvent(string organization, string bucket, WritePrecision precision, string lineProtocol) : base(organization, bucket, precision, lineProtocol) { } internal override void LogEvent() { Trace.WriteLine("The data was successfully written to InfluxDB 2."); } } public class WriteErrorEvent : AbstractWriteEvent { /// /// The exception that was throw. /// public Exception Exception { get; } public WriteErrorEvent(string organization, string bucket, WritePrecision precision, string lineProtocol, Exception exception) : base(organization, bucket, precision, lineProtocol) { Exception = exception; } internal override void LogEvent() { Trace.TraceError($"The error occurred during writing of data: {Exception.Message}"); } } /// /// The event is published when occurs a retriable write exception. /// public class WriteRetriableErrorEvent : AbstractWriteEvent { /// /// The exception that was throw. /// public Exception Exception { get; } /// /// The time to wait before retry unsuccessful write (milliseconds) /// public long RetryInterval { get; } public WriteRetriableErrorEvent(string organization, string bucket, WritePrecision precision, string lineProtocol, Exception exception, long retryInterval) : base(organization, bucket, precision, lineProtocol) { Exception = exception; RetryInterval = retryInterval; } internal override void LogEvent() { var message = "The retriable error occurred during writing of data. " + $"Reason: '{Exception.Message}'. " + $"Retry in: {(double)RetryInterval / 1000}s."; Trace.TraceWarning(message); } } /// /// Published when occurs a runtime exception in background batch processing. /// public class WriteRuntimeExceptionEvent : InfluxDBEventArgs { /// /// The Runtime Exception that was throw. /// public Exception Exception { get; } internal WriteRuntimeExceptionEvent(Exception exception) { Exception = exception; } internal override void LogEvent() { Trace.TraceError($"The unhandled exception occurs: {Exception}"); } } public abstract class AbstractWriteEvent : InfluxDBEventArgs { /// /// The organization that was used for write data. /// public string Organization { get; } /// /// The bucket that was used for write data. /// public string Bucket { get; } /// /// The Precision that was used for write data. /// public WritePrecision Precision { get; } /// /// The Data that was written. /// public string LineProtocol { get; } internal AbstractWriteEvent(string organization, string bucket, WritePrecision precision, string lineProtocol) { Organization = organization; Bucket = bucket; Precision = precision; LineProtocol = lineProtocol; } } }