From 79c40153418f2c96918c6f61267289e95949960e Mon Sep 17 00:00:00 2001 From: Osman Hadzic Date: Tue, 5 Sep 2023 09:12:25 +0200 Subject: [PATCH 1/2] Add static class for logger Signed-off-by: Osman Hadzic --- .../Utilities/Logger/AstarteLogger.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs diff --git a/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs b/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs new file mode 100644 index 0000000..ff4638e --- /dev/null +++ b/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs @@ -0,0 +1,59 @@ +/* + * This file is part of Astarte. + * + * Copyright 2023 SECO Mind Srl + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +using System.Diagnostics; + +public static class AstarteLogger +{ + public static void Info(string message, string className) + { + Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Info)); + } + + public static void Error(string message, string className) + { + Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Error)); + } + + public static void Warn(string message, string className) + { + Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Warn)); + } + + public static void Debug(string message, string className) + { + Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Debug)); + } + + private static string CreateMessage(string message, string className, AstarteLogLevel loggerLevel) + { + return String.Format("{0} : {1} : {2} : {3}", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"), + loggerLevel, className, message); + } +} + +enum AstarteLogLevel +{ + Info, + Error, + Warn, + Debug +} + From 1a345e49ec97bdcdf6cabf687221c978ce7266b2 Mon Sep 17 00:00:00 2001 From: Osman Hadzic Date: Tue, 5 Sep 2023 09:14:21 +0200 Subject: [PATCH 2/2] Replace Trace.WritteLine with AstarteLogger.LogLevel Signed-off-by: Osman Hadzic --- .../Device/AstarteDevice.cs | 4 ++-- ...tarteDeviceAggregateDatastreamInterface.cs | 4 ++-- .../AstarteDevicePropertyInterface.cs | 2 +- ...tarteServerAggregateDatastreamInterface.cs | 4 ++-- .../AstarteServerDatastreamInterface.cs | 7 ++++--- .../Transport/MQTT/AstarteMqttTransport.cs | 20 +++++++++---------- .../Utilities/Logger/AstarteLogger.cs | 2 +- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs b/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs index 10932ba..aef9c8b 100644 --- a/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs +++ b/AstarteDeviceSDKCSharp/Device/AstarteDevice.cs @@ -226,7 +226,7 @@ public async Task Connect() } catch (AstarteCryptoException) { - Trace.WriteLine("Regenerating the cert"); + AstarteLogger.Debug("Regenerating the cert", this.GetType().Name); try { await _pairingHandler.RequestNewCertificate(); @@ -395,7 +395,7 @@ public void OnTransportConnectionError(Exception ex) { if (ex is AstarteCryptoException) { - Trace.WriteLine("Regenerating the cert"); + AstarteLogger.Debug("Regenerating the cert", this.GetType().Name); try { Task.Run(() => _pairingHandler.RequestNewCertificate()); diff --git a/AstarteDeviceSDKCSharp/Protocol/AstarteDeviceAggregateDatastreamInterface.cs b/AstarteDeviceSDKCSharp/Protocol/AstarteDeviceAggregateDatastreamInterface.cs index 6e0dd1d..4f390cc 100644 --- a/AstarteDeviceSDKCSharp/Protocol/AstarteDeviceAggregateDatastreamInterface.cs +++ b/AstarteDeviceSDKCSharp/Protocol/AstarteDeviceAggregateDatastreamInterface.cs @@ -84,9 +84,9 @@ public void ValidatePayload(string path, Dictionary payload, Dat if (astarteInterfaceMapping.Path is null) { - Trace.WriteLine("Astarte mapping path " + + AstarteLogger.Warn("Astarte mapping path " + "{" + astarteInterfaceMapping.Path + " } " - + " is null."); + + " is null.", this.GetType().Name); continue; } if (!payload.ContainsKey( diff --git a/AstarteDeviceSDKCSharp/Protocol/AstarteDevicePropertyInterface.cs b/AstarteDeviceSDKCSharp/Protocol/AstarteDevicePropertyInterface.cs index 2c25a88..08ed871 100644 --- a/AstarteDeviceSDKCSharp/Protocol/AstarteDevicePropertyInterface.cs +++ b/AstarteDeviceSDKCSharp/Protocol/AstarteDevicePropertyInterface.cs @@ -55,7 +55,7 @@ public void SetProperty(string path, object payload) } catch (AstartePropertyStorageException e) { - Trace.WriteLine(e.Message); + AstarteLogger.Error(e.Message, this.GetType().Name); } if (!(storedValue?.PayloadEquality(payload) ?? false)) diff --git a/AstarteDeviceSDKCSharp/Protocol/AstarteServerAggregateDatastreamInterface.cs b/AstarteDeviceSDKCSharp/Protocol/AstarteServerAggregateDatastreamInterface.cs index 4fd9bb6..bc4c238 100644 --- a/AstarteDeviceSDKCSharp/Protocol/AstarteServerAggregateDatastreamInterface.cs +++ b/AstarteDeviceSDKCSharp/Protocol/AstarteServerAggregateDatastreamInterface.cs @@ -51,7 +51,7 @@ public void RemoveListener(IAstarteAggregateDatastreamEventListener listener) { if (serverValue is null) { - Trace.WriteLine("Unable to build AstarteServerValue, serverValue was empty"); + AstarteLogger.Error("Unable to build AstarteServerValue, serverValue was empty", this.GetType().Name); return null; } @@ -63,7 +63,7 @@ public void RemoveListener(IAstarteAggregateDatastreamEventListener listener) if (astartePayload is null) { - Trace.WriteLine("Unable to build AstarteServerValue, astartePayload was empty"); + AstarteLogger.Error("Unable to build AstarteServerValue, astartePayload was empty", this.GetType().Name); return null; } diff --git a/AstarteDeviceSDKCSharp/Protocol/AstarteServerDatastreamInterface.cs b/AstarteDeviceSDKCSharp/Protocol/AstarteServerDatastreamInterface.cs index d890daa..890275f 100644 --- a/AstarteDeviceSDKCSharp/Protocol/AstarteServerDatastreamInterface.cs +++ b/AstarteDeviceSDKCSharp/Protocol/AstarteServerDatastreamInterface.cs @@ -58,7 +58,7 @@ public void RemoveListener(IAstarteDatastreamEventListener listener) AstarteInterfaceMapping? targetMapping = null; if (serverValue is null) { - Trace.WriteLine($"Unable to build AstarteServerValue, astartePayload was empty"); + AstarteLogger.Error("Unable to build AstarteServerValue, astartePayload was empty", this.GetType().Name); return null; } @@ -87,8 +87,9 @@ public void RemoveListener(IAstarteDatastreamEventListener listener) } else { - Trace.WriteLine($"Got an unexpected path {interfacePath}" - + "for interface {GetInterfaceName()}"); + AstarteLogger.Warn($"Got an unexpected path {interfacePath}" + + "for interface {GetInterfaceName()}", this.GetType().Name); + } return astarteServerValue; } diff --git a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs index 252c58a..99b9623 100644 --- a/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs +++ b/AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs @@ -128,7 +128,7 @@ async Task OnConnectedAsync(MqttClientConnectedEventArgs args) } else { - Trace.WriteLine("Transport Connected"); + AstarteLogger.Info("Transport Connected", this.GetType().Name); } } @@ -276,7 +276,7 @@ async Task OnDisconnectAsync(MqttClientDisconnectedEventArgs e) } else { - Trace.WriteLine("The Connection was lost."); + AstarteLogger.Error("The Connection was lost.", this.GetType().Name); } } @@ -285,8 +285,8 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) { object? payload = null; - Trace.WriteLine("Incoming message: " - + Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0])); + AstarteLogger.Debug("Incoming message: " + + Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0]), this.GetType().Name); if (!e.ApplicationMessage.Topic.Contains(_connectionInfo.GetClientId()) || _messageListener == null) @@ -313,7 +313,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) } else { - Trace.WriteLine("Unhandled control message!" + path); + AstarteLogger.Error("Unhandled control message!" + path, this.GetType().Name); } return; } @@ -323,7 +323,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) if (!astarteDevice.HasInterface(astarteInterface)) { - Trace.WriteLine("Got an unexpected interface!" + astarteInterface); + AstarteLogger.Warn("Got an unexpected interface!" + astarteInterface, this.GetType().Name); return; } @@ -335,7 +335,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) decodedMessage = AstartePayload.Deserialize(e.ApplicationMessage.Payload); if (decodedMessage is null) { - Trace.WriteLine("Unable to get payload, decodedMessage was null"); + AstarteLogger.Warn("Unable to get payload, decodedMessage was null ", this.GetType().Name); return; } payload = decodedMessage.GetPayload(); @@ -362,7 +362,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) if (astarteServerValue == null) { - Trace.WriteLine("Unable to get value, astarteServerValue was null"); + AstarteLogger.Error("Unable to get value, astarteServerValue was null", this.GetType().Name); return; } @@ -389,7 +389,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e) } catch (AstartePropertyStorageException ex) { - Trace.WriteLine(ex.Message); + AstarteLogger.Error(ex.Message, this.GetType().Name); } } @@ -434,7 +434,7 @@ private void HandlePurgeProperties(byte[] payload, AstarteDevice? astarteDevice) } catch (IOException e) { - Trace.WriteLine(e.Message); + AstarteLogger.Error(e.Message, this.GetType().Name); } string purgePropertiesPayload = result.ToString(); diff --git a/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs b/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs index ff4638e..53e694b 100644 --- a/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs +++ b/AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs @@ -1,7 +1,7 @@ /* * This file is part of Astarte. * - * Copyright 2023 SECO Mind Srl + * Copyright 2023-2024 SECO Mind Srl * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.