diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs index cd04aea..72ce43f 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherDisk.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -15,12 +16,14 @@ namespace NucuCar.Domain.Telemetry public class TelemetryPublisherDisk : TelemetryPublisher { private readonly FileStream _fileStream; + private int _charactersWritten; /// /// Constructs an instance of . /// /// The connection string must contain the following options: /// Filename (required) - The path of the filename in which to log telemetry data. + /// FileExtension (optional) - The extension of the filename. Default txt /// BufferSize (optional) - The buffer size for the async writer. Default: 4096 /// /// @@ -28,10 +31,11 @@ namespace NucuCar.Domain.Telemetry public TelemetryPublisherDisk(TelemetryPublisherBuilderOptions opts) : base(opts) { var connectionStringParams = ConnectionStringParser.Parse(opts.ConnectionString); - var fileName = connectionStringParams.GetValueOrDefault("Filename"); + var fileName = connectionStringParams.GetValueOrDefault("FileName"); + var fileExtension = connectionStringParams.GetValueOrDefault("FileExtension", "txt"); var bufferSize = connectionStringParams.GetValueOrDefault("BufferSize", "4096"); - _fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, + _fileStream = new FileStream(NormalizeFilename(fileName, fileExtension), FileMode.Append, FileAccess.Write, FileShare.Read, int.Parse(bufferSize), true); Logger?.LogDebug("Initialized the TelemetryPublisherDisk!"); } @@ -43,13 +47,21 @@ namespace NucuCar.Domain.Telemetry Logger?.LogDebug($"Telemetry message: {messageString}"); var encodedText = Encoding.Unicode.GetBytes(messageString); - await _fileStream.WriteAsync(encodedText, (int) _fileStream.Length, + await _fileStream.WriteAsync(encodedText, _charactersWritten, encodedText.Length, cancellationToken); + _charactersWritten += encodedText.Length; + await _fileStream.FlushAsync(cancellationToken); } public override void Dispose() { _fileStream.Close(); } + + private static string NormalizeFilename(string filename, string extension) + { + var date = DateTime.UtcNow; + return $"{filename}-{date.Year}-{date.Month}-{date.Day}_{date.ToFileTimeUtc()}.{extension}"; + } } } \ No newline at end of file diff --git a/NucuCar.Sensors/appsettings.json b/NucuCar.Sensors/appsettings.json index 57d32f8..3e821b9 100644 --- a/NucuCar.Sensors/appsettings.json +++ b/NucuCar.Sensors/appsettings.json @@ -3,7 +3,7 @@ "Publisher": "Disk", "ServiceEnabled": true, "PublishInterval": 3000, - "ConnectionString": "Filename=nucucar.telemetry.txt" + "ConnectionString": "FileName=nucucar.telemetry" }, "EnvironmentSensor": { "Enabled": true,