influxdata / influxdb-client-csharp

InfluxDB 2.x C# Client
https://influxdata.github.io/influxdb-client-csharp/api/InfluxDB.Client.html
MIT License
360 stars 92 forks source link

Not throw exception #666

Closed maikaliu closed 2 months ago

maikaliu commented 2 months ago

InfluxDB.Client don't throw any exception

code: using var client = new InfluxDBClient("http://localhost:123", "abc"); using (var writeApi = client.GetWriteApi()) { // // Write by Point // var point = PointData.Measurement("temperature") .Tag("location", "west") .Field("value", 55D) .Timestamp(DateTime.UtcNow.AddSeconds(-10), WritePrecision.Ns);

writeApi.WritePoint(point, "bucket_name", "org_id");

//
// Write by LineProtocol
//
writeApi.WriteRecord("temperature,location=north value=60.0", WritePrecision.Ns, "bucket_name", "org_id");

}

Expected behavior: throw a exception

Actual behavior: don't throw any exception

Specifications:

bednar commented 2 months ago

Hi @maikaliu,

Thank you for using our client.

The WriteApi is designed to facilitate asynchronous data writing via a background thread, acting as a gateway to queue data and handle the actual writing process in the background. This setup is intended to maximize efficiency and throughput by decoupling data collection from data transmission.

If you need to monitor or react to errors that occur during the write process, you can attach to the error event using the EventHandler mechanism provided by the API. This allows you to handle exceptions and take appropriate actions without disrupting the main workflow.

For scenarios where you require immediate error feedback or if your application logic depends on knowing the outcome of each write operation directly, consider using the WriteApiAsync. This API variant throws errors directly and operates on a synchronous task-based model, making it more suitable for applications where write success needs to be guaranteed before proceeding.

Here's how you might use it:

using (var writeApiAsync = client.GetWriteApiAsync())
{
    try
    {
        await writeApiAsync.WriteRecordAsync(bucket, org, WritePrecision.Ns, "temperature,location=north value=60.0");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error writing to InfluxDB: {ex.Message}");
    }
}

This approach ensures that errors are handled immediately as they occur, providing you with full control over the error management process.

Please let me know if you need further clarification or additional assistance.

Best Regards

maikaliu commented 1 month ago

Thank you for your explanation Best Regards