From 4423c2883118e77c7f3a1042b375498c8712c3e2 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Mon, 24 Jan 2022 09:15:57 +0100 Subject: [PATCH 01/12] Cache SerialPort in SerialLoggerFactory --- .../SerialLoggerFactory.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs index ec0ce15..dad22ba 100644 --- a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs +++ b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs @@ -49,12 +49,15 @@ public SerialLoggerFactory( /// public ILogger CreateLogger(string categoryName) { - _serial = new SerialPort(_comPort); - _serial.BaudRate = _baudRate; - _serial.Parity = _parity; - _serial.StopBits = _stopBits; - _serial.Handshake = _handshake; - _serial.DataBits = _dataBits; + if (_serial is null) + { + _serial = new SerialPort(_comPort); + _serial.BaudRate = _baudRate; + _serial.Parity = _parity; + _serial.StopBits = _stopBits; + _serial.Handshake = _handshake; + _serial.DataBits = _dataBits; + } return new SerialLogger(ref _serial, categoryName); } From fb7ac9c8715f4c7a49c8dcdebea491971fb06d72 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Tue, 25 Jan 2022 11:37:19 +0100 Subject: [PATCH 02/12] SerialLogger[Factory] documentation update --- nanoFramework.Logging.Serial/SerialLogger.cs | 2 +- nanoFramework.Logging.Serial/SerialLoggerFactory.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nanoFramework.Logging.Serial/SerialLogger.cs b/nanoFramework.Logging.Serial/SerialLogger.cs index 36a4cca..044f43e 100644 --- a/nanoFramework.Logging.Serial/SerialLogger.cs +++ b/nanoFramework.Logging.Serial/SerialLogger.cs @@ -12,7 +12,7 @@ namespace nanoFramework.Logging.Serial { /// - /// A logger that outputs to a . + /// A logger that outputs to a . /// public class SerialLogger : ILogger { diff --git a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs index dad22ba..2298820 100644 --- a/nanoFramework.Logging.Serial/SerialLoggerFactory.cs +++ b/nanoFramework.Logging.Serial/SerialLoggerFactory.cs @@ -22,7 +22,7 @@ public class SerialLoggerFactory : ILoggerFactory private readonly Handshake _handshake; /// - /// Create a new instance of from a . + /// Create a new instance of from a . /// /// /// From f9832db08b7d4ccd1ac67f8e0664ed6ccfa07bd2 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Tue, 25 Jan 2022 14:06:38 +0100 Subject: [PATCH 03/12] Add Signature to Serial and Stream packages --- .../nanoFramework.Logging.Serial.nfproj | 9 +++++++++ .../nanoFramework.Logging.Stream.nfproj | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj b/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj index 0c1870e..77248fb 100644 --- a/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj +++ b/nanoFramework.Logging.Serial/nanoFramework.Logging.Serial.nfproj @@ -17,6 +17,15 @@ v1.0 bin\$(Configuration)\nanoFramework.Logging.Serial.xml + + true + + + key.snk + + + false + diff --git a/nanoFramework.Logging.Stream/nanoFramework.Logging.Stream.nfproj b/nanoFramework.Logging.Stream/nanoFramework.Logging.Stream.nfproj index a865911..06521af 100644 --- a/nanoFramework.Logging.Stream/nanoFramework.Logging.Stream.nfproj +++ b/nanoFramework.Logging.Stream/nanoFramework.Logging.Stream.nfproj @@ -17,6 +17,15 @@ v1.0 bin\$(Configuration)\nanoFramework.Logging.Stream.xml + + true + + + key.snk + + + false + From 6b97487c52f7f4e28dd182a066581d53b8d0a6a7 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Tue, 25 Jan 2022 15:58:27 +0100 Subject: [PATCH 04/12] Syslog Logging Provider initial commit --- README.md | 2 + nanoFramework.Logging.Syslog.nuspec | 42 ++++++ nanoFramework.Logging.Syslog/Facility.cs | 37 +++++ .../Properties/AssemblyInfo.cs | 22 +++ nanoFramework.Logging.Syslog/Severity.cs | 21 +++ nanoFramework.Logging.Syslog/SyslogClient.cs | 126 ++++++++++++++++++ nanoFramework.Logging.Syslog/SyslogLogger.cs | 76 +++++++++++ .../SyslogLoggerFactory.cs | 60 +++++++++ nanoFramework.Logging.Syslog/key.snk | Bin 0 -> 596 bytes .../nanoFramework.Logging.Syslog.nfproj | 75 +++++++++++ nanoFramework.Logging.Syslog/packages.config | 10 ++ nanoFramework.Logging.sln | 13 +- 12 files changed, 481 insertions(+), 3 deletions(-) create mode 100644 nanoFramework.Logging.Syslog.nuspec create mode 100644 nanoFramework.Logging.Syslog/Facility.cs create mode 100644 nanoFramework.Logging.Syslog/Properties/AssemblyInfo.cs create mode 100644 nanoFramework.Logging.Syslog/Severity.cs create mode 100644 nanoFramework.Logging.Syslog/SyslogClient.cs create mode 100644 nanoFramework.Logging.Syslog/SyslogLogger.cs create mode 100644 nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs create mode 100644 nanoFramework.Logging.Syslog/key.snk create mode 100644 nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj create mode 100644 nanoFramework.Logging.Syslog/packages.config diff --git a/README.md b/README.md index d17d13e..feaa0cb 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ | nanoFramework.Logging.Serial (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoframework.Logging/_apis/build/status/nanoframework.nanoFramework.Logging?branchName=develop)](https://dev.azure.com/nanoframework/nanoframework.Logging/_build/latest?definitionId=71&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.Logging.Serial.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Logging.Serial/) | | nanoFramework.Logging.Stream | [![Build Status](https://dev.azure.com/nanoframework/nanoframework.Logging/_apis/build/status/nanoframework.nanoFramework.Logging?branchName=main)](https://dev.azure.com/nanoframework/nanoframework.Logging/_build/latest?definitionId=71&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.Logging.Stream.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Logging.Stream/) | | nanoFramework.Logging.Stream (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoframework.Logging/_apis/build/status/nanoframework.nanoFramework.Logging?branchName=develop)](https://dev.azure.com/nanoframework/nanoframework.Logging/_build/latest?definitionId=71&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.Logging.Stream.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Logging.Stream/) | +| nanoFramework.Logging.Syslog | [![Build Status](https://dev.azure.com/nanoframework/nanoframework.Logging/_apis/build/status/nanoframework.nanoFramework.Logging?branchName=main)](https://dev.azure.com/nanoframework/nanoframework.Logging/_build/latest?definitionId=71&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.Logging.Syslog.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Logging.Syslog/) | +| nanoFramework.Logging.Syslog (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoframework.Logging/_apis/build/status/nanoframework.nanoFramework.Logging?branchName=develop)](https://dev.azure.com/nanoframework/nanoframework.Logging/_build/latest?definitionId=71&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.Logging.Syslog.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Logging.Syslog/) | ## Feedback and documentation diff --git a/nanoFramework.Logging.Syslog.nuspec b/nanoFramework.Logging.Syslog.nuspec new file mode 100644 index 0000000..79c3bcc --- /dev/null +++ b/nanoFramework.Logging.Syslog.nuspec @@ -0,0 +1,42 @@ + + + + nanoFramework.Logging.Syslog + $version$ + nanoFramework.Logging.Syslog + nanoFramework project contributors + nanoFramework,dotnetfoundation + false + LICENSE.md + + + docs\README.md + false + https://github.com/nanoframework/nanoFramework.Logging + images\nf-logo.png + + Copyright (c) .NET Foundation and Contributors + This package includes the nanoFramework.Logging.Syslog assembly (Syslog Logging only) for .NET nanoFramework C# projects. +There is also a package with the Serial Logging, another with Stream Logging only and another with the basic Debug Logging. + nanoFramework C# csharp netmf netnf nanoFramework.Logging.Syslog + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/nanoFramework.Logging.Syslog/Facility.cs b/nanoFramework.Logging.Syslog/Facility.cs new file mode 100644 index 0000000..85171ef --- /dev/null +++ b/nanoFramework.Logging.Syslog/Facility.cs @@ -0,0 +1,37 @@ +using System; + +namespace nanoFramework.Logging.Syslog +{ + /// + /// Available facility types (RFC 3164). + /// + public enum Facility + { +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + Kern = 0, + User = 1, + Mail = 2, + Daemon = 3, + Auth = 4, + Syslog = 5, + LPR = 6, + News = 7, + UUCP = 8, + Cron = 9, + AuthPriv = 10, + FTP = 11, + NTP = 12, + Audit = 13, + Audit2 = 14, + Cron2 = 15, + Local0 = 16, + Local1 = 17, + Local2 = 18, + Local3 = 19, + Local4 = 20, + Local5 = 21, + Local6 = 22, + Local7 = 23 +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member + } +} diff --git a/nanoFramework.Logging.Syslog/Properties/AssemblyInfo.cs b/nanoFramework.Logging.Syslog/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..387680d --- /dev/null +++ b/nanoFramework.Logging.Syslog/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("nanoFramework.Logging")] +[assembly: AssemblyCompany("nanoFramework Contributors")] +[assembly: AssemblyProduct("nanoFramework.Logging")] +[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +///////////////////////////////////////////////////////////////// +// This attribute is mandatory when building Interop libraries // +// update this whenever the native assembly signature changes // +[assembly: AssemblyNativeVersion("1.0.0.0")] +///////////////////////////////////////////////////////////////// diff --git a/nanoFramework.Logging.Syslog/Severity.cs b/nanoFramework.Logging.Syslog/Severity.cs new file mode 100644 index 0000000..0c2600a --- /dev/null +++ b/nanoFramework.Logging.Syslog/Severity.cs @@ -0,0 +1,21 @@ +using System; + +namespace nanoFramework.Logging.Syslog +{ + /// + /// Severity represents the Severity in the Syslog messages. This corresponds to the values defined in RFC 3164 except None which means that the message won't be sent. + /// + public enum Severity { +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + None = -1, + Emergency, + Alert, + Critical, + Error, + Warning, + Notice, + Informational, + Debug +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member + }; +} diff --git a/nanoFramework.Logging.Syslog/SyslogClient.cs b/nanoFramework.Logging.Syslog/SyslogClient.cs new file mode 100644 index 0000000..6a43b57 --- /dev/null +++ b/nanoFramework.Logging.Syslog/SyslogClient.cs @@ -0,0 +1,126 @@ +using System; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace nanoFramework.Logging.Syslog +{ + /// + /// Syslog Client that sends logs to a syslog server following RFC3164 format. + /// + public class SyslogClient : IDisposable + { + private readonly Socket _socket; + private bool _disposed; + + /// + /// Default Syslog UDP port : 514 + /// + public const int DefaultPort = 514; + + /// + /// Create a SyslogClient instance that will send RFC3164 compliant messages to a syslof server + /// + /// Endpoint of the server + /// 'Hostname' part of the RFC3164 message + /// Facility used by this logger + /// Local IP address to bind socket (if null IPAdress.Any will be used) + /// Local port to bind socket (0 to choose available port) + public SyslogClient(IPEndPoint endpoint, string localHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + { + if (localHostname is null) + throw new ArgumentNullException(nameof(localHostname)); + + Facility = facility; + LocalHostname = localHostname; + + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); + _socket.Bind(new IPEndPoint(localAddress ?? IPAddress.Any, localPort)); + _socket.Connect(endpoint); + } + + /// + /// Create a SyslogClient instance that will send RFC3164 compliant messages to a syslof server + /// + /// DNS name of the syslog server + /// Port of the syslog server + /// 'Hostname' part of the RFC3164 message (if null, use 'nanoframework') + /// Facility used by this logger + /// Local IP address to bind socket (if null IPAdress.Any will be used) + /// Local port to bind socket (0 to choose available port) + public SyslogClient(string hostname, int port, string localHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + : this(ResolveEndPoint(hostname, port), localHostname, facility, localAddress, localPort) + { + } + + /// + /// Facility used by this syslog client + /// + public Facility Facility { get; } + + /// + /// Hostname used in the 'Hostname' field in the syslog message + /// + public string LocalHostname { get; } + + /// + /// Send a message to the SysLog server + /// + /// Severity in the message + /// Message tag (will be followed by ': ' in the message) + /// Message content + /// + public void SendMessage(Severity severity, string messageTag, string messageContent) => SendMessage(Facility, severity, LocalHostname, messageTag, messageContent); + + /// + /// Send a generic syslog message to the SysLog server + /// + /// Facility in the message + /// Severity in the message + /// Hostname in the message + /// Message tag (will be followed by ': ' in the message) + /// Message content + /// + public void SendMessage(Facility facility,Severity severity, string hostname, string messageTag, string messageContent) + { + if (_disposed) + throw new ObjectDisposedException(nameof(SyslogClient)); + if (severity != Severity.None) // We don't send message with Severity.None which isn't a RFC6134 valid value + _socket.Send(FormatUdpMessage(facility, severity, hostname, messageTag, messageContent)); + } + + private static byte[] FormatUdpMessage(Facility facility,Severity severity, string localHostname, string messageTag, string messageContent) + { + int priorityValue = ((int)facility * 8) + (int)severity; + + var headerBuilder = new StringBuilder(); + // RFC3164 PRI + headerBuilder.Append('<').Append(priorityValue).Append('>'); + // RFC3164 HEADER + string timestamp = DateTime.UtcNow.ToString("MMM dd HH:mm:ss"); + headerBuilder.Append(timestamp).Append(' '); + headerBuilder.Append(localHostname).Append(' '); + // RFC3164 MSG + if (messageTag != null) + headerBuilder.Append(messageTag).Append(':').Append(' '); + headerBuilder.Append(messageContent ?? ""); + var s = headerBuilder.ToString(); + return UTF8Encoding.UTF8.GetBytes(s); // Ideally an UTF7 is required here to be RFC3164 compliant + } + + private static IPEndPoint ResolveEndPoint(string hostname, int port) + { + IPHostEntry hostEntry = Dns.GetHostEntry(hostname); + if (hostEntry is null) + throw new ArgumentException($"Hostname `{hostname}` is not resolvable"); + return new IPEndPoint(hostEntry.AddressList[0], port); + } + + /// + public void Dispose() + { + _disposed = true; + ((IDisposable)_socket).Dispose(); + } + } +} diff --git a/nanoFramework.Logging.Syslog/SyslogLogger.cs b/nanoFramework.Logging.Syslog/SyslogLogger.cs new file mode 100644 index 0000000..14fdc1e --- /dev/null +++ b/nanoFramework.Logging.Syslog/SyslogLogger.cs @@ -0,0 +1,76 @@ +using System; +using System.Reflection; +using Microsoft.Extensions.Logging; + +namespace nanoFramework.Logging.Syslog +{ + /// + /// ILogger implmentation that send message log to a Syslog server following RFC3164 convention. + /// While not mandatory it is recommended to use to create the SyslogLogger instances. + /// + public class SyslogLogger : ILogger + { + private SyslogClient _client; + + /// + /// Create new logger based on existing SyslogClient. Recommended to use LoggerFactory patern trough SyslogLoggerFactory instead of direct use. + /// + /// SyslogClient to use for sending message + /// CategoryName of this logger used as TAG in the Syslog messages + /// + public SyslogLogger(SyslogClient client, string categoryName) + { + if (client is null) + throw new ArgumentNullException(nameof(client)); + + _client = client; + LoggerCategoryName = categoryName; + } + + /// + /// Minimum log level used by this logger. + /// + public LogLevel MinLogLevel { get; set; } //by default 0 -> Trace + + /// + /// CategoryName of this logger used as TAG in the Syslog messages. + /// + public string LoggerCategoryName { get; } + + /// + /// Underlying Syslog client used by this logger. + /// + public SyslogClient Client => _client; + + /// + public bool IsEnabled(LogLevel logLevel) => logLevel >= MinLogLevel; + + /// + public void Log(LogLevel logLevel, EventId eventId, string state, Exception exception, MethodInfo format) + { + if (logLevel >= MinLogLevel && logLevel!=LogLevel.None) + { + string message; + if (format is null) + message = exception == null ? $"{state}" : $"{state} {exception}"; + else + message = $"{(string)format.Invoke(null, new object[] { LoggerCategoryName, logLevel, eventId, state, exception })}"; + + _client.SendMessage(LogLevelToSeverity(logLevel), LoggerCategoryName, message); + } + } + + private Severity LogLevelToSeverity(LogLevel level) => level switch + { + LogLevel.Trace => Severity.Debug, + LogLevel.Debug => Severity.Debug, + LogLevel.Information => Severity.Informational, + LogLevel.Warning => Severity.Warning, + LogLevel.Error => Severity.Error, + LogLevel.Critical => Severity.Critical, + LogLevel.None => Severity.None, + _ => Severity.None + }; + + } +} diff --git a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs new file mode 100644 index 0000000..32360d1 --- /dev/null +++ b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs @@ -0,0 +1,60 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Net; + +namespace nanoFramework.Logging.Syslog +{ + /// + /// ILoggerFactory implementation that creates SyslogLogger instances. + /// The logger factory initializes an underlying that will be shared by all instances it will create. + /// + public class SyslogLoggerFactory : ILoggerFactory + { + const string DefaultHostname = "nanoframework"; + private readonly SyslogClient _client; + private bool _disposed; + + /// + /// Create a logging factory that will provide support for Syslog ILogger provider + /// + /// Endpoint of the server + /// 'Hostname' part of the RFC3164 message + /// Facility used by this logger + /// Local IP address to bind socket (if null IPAdress.Any will be used) + /// Local port to bind socket (0 to choose available port) + public SyslogLoggerFactory(IPEndPoint endpoint, string localHostname=DefaultHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + { + _client = new SyslogClient(endpoint, localHostname, facility, localAddress, localPort); + } + + /// + /// Create a logging factory that will provide support for Syslog ILogger provider + /// + /// DNS name of the syslog server + /// Port of the syslog server (default to 514) + /// 'Hostname' part of the RFC3164 message + /// Facility used by this logger + /// Local IP address to bind socket (if null IPAdress.Any will be used) + /// Local port to bind socket (0 to choose available port) + public SyslogLoggerFactory(string hostname, int port= SyslogClient.DefaultPort, string localHostname=DefaultHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + { + _client = new SyslogClient(hostname, port, localHostname, facility , localAddress, localPort); + } + + /// + public ILogger CreateLogger(string categoryName) + { + if (_disposed) + throw new ObjectDisposedException(nameof(SyslogLoggerFactory)); + return new SyslogLogger(_client,categoryName); + } + + /// + public void Dispose() + { + _disposed = true; + // If logger created through this factory are used after this you will get exception + _client.Dispose(); + } + } +} diff --git a/nanoFramework.Logging.Syslog/key.snk b/nanoFramework.Logging.Syslog/key.snk new file mode 100644 index 0000000000000000000000000000000000000000..67c9bb0ad77fd9cfb31a5fe1f8e4f6537f8883f8 GIT binary patch literal 596 zcmV-a0;~N80ssI2Bme+XQ$aES1ONa50096IAgVrqn?0oVUK<}}z@wqRV>QE=V9G(P zv&4>n;N)r`28$iZ}__~(k83t)%SuJd!((DT{8XK)X~rK64E`*F3l z_5^K&AM;wi;^44^3SyC?622Htvk|)gU+)k0iCdaqB2%u@FR*KYku>(Zol_KusD7d= zA40qu=-~L94PXsGJ#eO@FlCr&O~0PO!6o-*GQq`zy7)g4B)IFgLhI0vfH=+L6i;`a zn(iWy{`8{{oj}vGf9S9M=48~NC!$coEAg@m^H3NXd_JrnT}HOd0AR%m^g zLg4+2jC7g-iYgo9N=!v@nv=I99O}eXu|AyULz5*Z*LDuQt4pP~UX|Zf`1Y#v4}kU8;^ac8l5!?oeMbz%wA_dHw*Ud81*FK}w1r zw3t~`4#bY+anQY5XU!i9#lc>IiLAxX~!c5@{0(|Buf%3XVh2FE%G?X~1 z7e5uWO?|rx0b=vcr3gpq3{-d$I8fk&Qrx<8j#tO3HOsxjGMJn4IT0*|11Oqu{gBd3 z&+6?KA|)R~alAn@8a!Y9Eq$TwFM@mu$U=dJ?PzCQY8jWLBlv^YEFIb8Fvhr*vMTOc zG+a5pRFEs9BT_>6MCF{WdN&DTsp^F!N=e1<>cRNkafH_Fr3`v9f%5G2(}LQ$j`vCM iwz?1qR;-^j3D_^H<{zl%^r6ivhR`zlIvhi1+jkY8izavg literal 0 HcmV?d00001 diff --git a/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj new file mode 100644 index 0000000..6f048f5 --- /dev/null +++ b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj @@ -0,0 +1,75 @@ + + + + $(MSBuildExtensionsPath)\nanoFramework\v1.0\ + + + + Debug + AnyCPU + {11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + a778ed84-6ea0-48c5-b97c-dc64a8d97a50 + Library + Properties + 512 + nanoFramework.Logging.Syslog + nanoFramework.Logging.Syslog + v1.0 + bin\$(Configuration)\nanoFramework.Logging.Syslog.xml + + + true + + + key.snk + + + false + + + + + + + + + + + + + ..\packages\nanoFramework.CoreLibrary.1.12.0-preview.1\lib\mscorlib.dll + + + ..\packages\nanoFramework.Logging.1.0.1-preview.10\lib\nanoFramework.Logging.dll + + + ..\packages\nanoFramework.Runtime.Events.1.10.0-preview.4\lib\nanoFramework.Runtime.Events.dll + + + ..\packages\nanoFramework.System.Text.1.1.3-preview.7\lib\nanoFramework.System.Text.dll + + + ..\packages\nanoFramework.System.Net.1.8.0-preview.15\lib\System.Net.dll + + + ..\packages\nanoFramework.System.Threading.1.0.4-preview.8\lib\System.Threading.dll + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + + + \ No newline at end of file diff --git a/nanoFramework.Logging.Syslog/packages.config b/nanoFramework.Logging.Syslog/packages.config new file mode 100644 index 0000000..655de39 --- /dev/null +++ b/nanoFramework.Logging.Syslog/packages.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/nanoFramework.Logging.sln b/nanoFramework.Logging.sln index 18d7f1b..a50f122 100644 --- a/nanoFramework.Logging.sln +++ b/nanoFramework.Logging.sln @@ -1,7 +1,6 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31105.61 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32112.339 MinimumVisualStudioVersion = 10.0.40219.1 Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Logging", "nanoFramework.Logging\nanoFramework.Logging.nfproj", "{0A60F291-B35A-4BF1-BAB9-CD686AF2EED6}" EndProject @@ -13,6 +12,8 @@ Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Logging.Seria EndProject Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Logging.Stream", "nanoFramework.Logging.Stream\nanoFramework.Logging.Stream.nfproj", "{38F918AD-5A39-4172-B4B4-ECC2A3FA5B4E}" EndProject +Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Logging.Syslog", "nanoFramework.Logging.Syslog\nanoFramework.Logging.Syslog.nfproj", "{A778ED84-6EA0-48C5-B97C-DC64A8D97A50}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -43,6 +44,12 @@ Global {38F918AD-5A39-4172-B4B4-ECC2A3FA5B4E}.Release|Any CPU.ActiveCfg = Release|Any CPU {38F918AD-5A39-4172-B4B4-ECC2A3FA5B4E}.Release|Any CPU.Build.0 = Release|Any CPU {38F918AD-5A39-4172-B4B4-ECC2A3FA5B4E}.Release|Any CPU.Deploy.0 = Release|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Debug|Any CPU.Deploy.0 = Debug|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Release|Any CPU.Build.0 = Release|Any CPU + {A778ED84-6EA0-48C5-B97C-DC64A8D97A50}.Release|Any CPU.Deploy.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 98cce5764f976dea6a66526409b99b8dad72f04d Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Tue, 25 Jan 2022 16:28:29 +0100 Subject: [PATCH 05/12] Revert Visual Studio SLN version --- nanoFramework.Logging.sln | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nanoFramework.Logging.sln b/nanoFramework.Logging.sln index a50f122..6ef4703 100644 --- a/nanoFramework.Logging.sln +++ b/nanoFramework.Logging.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.32112.339 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31105.61 MinimumVisualStudioVersion = 10.0.40219.1 Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Logging", "nanoFramework.Logging\nanoFramework.Logging.nfproj", "{0A60F291-B35A-4BF1-BAB9-CD686AF2EED6}" EndProject From f7f1525b4578bce38c56bfa680c50d5528975197 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Tue, 25 Jan 2022 18:11:07 +0100 Subject: [PATCH 06/12] Implement remarks from PR --- nanoFramework.Logging.Syslog/Facility.cs | 6 ++++- nanoFramework.Logging.Syslog/Severity.cs | 6 ++++- nanoFramework.Logging.Syslog/SyslogClient.cs | 27 ++++++++++++++----- nanoFramework.Logging.Syslog/SyslogLogger.cs | 17 +++++++----- .../SyslogLoggerFactory.cs | 11 ++++++-- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/nanoFramework.Logging.Syslog/Facility.cs b/nanoFramework.Logging.Syslog/Facility.cs index 85171ef..d5d92bf 100644 --- a/nanoFramework.Logging.Syslog/Facility.cs +++ b/nanoFramework.Logging.Syslog/Facility.cs @@ -1,4 +1,8 @@ -using System; +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// +using System; namespace nanoFramework.Logging.Syslog { diff --git a/nanoFramework.Logging.Syslog/Severity.cs b/nanoFramework.Logging.Syslog/Severity.cs index 0c2600a..05770c1 100644 --- a/nanoFramework.Logging.Syslog/Severity.cs +++ b/nanoFramework.Logging.Syslog/Severity.cs @@ -1,4 +1,8 @@ -using System; +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// +using System; namespace nanoFramework.Logging.Syslog { diff --git a/nanoFramework.Logging.Syslog/SyslogClient.cs b/nanoFramework.Logging.Syslog/SyslogClient.cs index 6a43b57..b0905dd 100644 --- a/nanoFramework.Logging.Syslog/SyslogClient.cs +++ b/nanoFramework.Logging.Syslog/SyslogClient.cs @@ -1,4 +1,8 @@ -using System; +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// +using System; using System.Net; using System.Net.Sockets; using System.Text; @@ -28,11 +32,8 @@ public class SyslogClient : IDisposable /// Local port to bind socket (0 to choose available port) public SyslogClient(IPEndPoint endpoint, string localHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) { - if (localHostname is null) - throw new ArgumentNullException(nameof(localHostname)); - Facility = facility; - LocalHostname = localHostname; + LocalHostname = localHostname ?? throw new ArgumentNullException(nameof(localHostname)); _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); _socket.Bind(new IPEndPoint(localAddress ?? IPAddress.Any, localPort)); @@ -84,9 +85,15 @@ public SyslogClient(string hostname, int port, string localHostname, Facility fa public void SendMessage(Facility facility,Severity severity, string hostname, string messageTag, string messageContent) { if (_disposed) + { throw new ObjectDisposedException(nameof(SyslogClient)); - if (severity != Severity.None) // We don't send message with Severity.None which isn't a RFC6134 valid value + } + + // We don't send message with Severity.None which isn't a RFC6134 valid value + if (severity != Severity.None) + { _socket.Send(FormatUdpMessage(facility, severity, hostname, messageTag, messageContent)); + } } private static byte[] FormatUdpMessage(Facility facility,Severity severity, string localHostname, string messageTag, string messageContent) @@ -102,7 +109,10 @@ private static byte[] FormatUdpMessage(Facility facility,Severity severity, stri headerBuilder.Append(localHostname).Append(' '); // RFC3164 MSG if (messageTag != null) + { headerBuilder.Append(messageTag).Append(':').Append(' '); + } + headerBuilder.Append(messageContent ?? ""); var s = headerBuilder.ToString(); return UTF8Encoding.UTF8.GetBytes(s); // Ideally an UTF7 is required here to be RFC3164 compliant @@ -112,7 +122,10 @@ private static IPEndPoint ResolveEndPoint(string hostname, int port) { IPHostEntry hostEntry = Dns.GetHostEntry(hostname); if (hostEntry is null) + { throw new ArgumentException($"Hostname `{hostname}` is not resolvable"); + } + return new IPEndPoint(hostEntry.AddressList[0], port); } @@ -120,7 +133,7 @@ private static IPEndPoint ResolveEndPoint(string hostname, int port) public void Dispose() { _disposed = true; - ((IDisposable)_socket).Dispose(); + ((IDisposable)_socket)?.Dispose(); } } } diff --git a/nanoFramework.Logging.Syslog/SyslogLogger.cs b/nanoFramework.Logging.Syslog/SyslogLogger.cs index 14fdc1e..c459e8e 100644 --- a/nanoFramework.Logging.Syslog/SyslogLogger.cs +++ b/nanoFramework.Logging.Syslog/SyslogLogger.cs @@ -1,4 +1,8 @@ -using System; +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// +using System; using System.Reflection; using Microsoft.Extensions.Logging; @@ -10,7 +14,7 @@ namespace nanoFramework.Logging.Syslog /// public class SyslogLogger : ILogger { - private SyslogClient _client; + private readonly SyslogClient _client; /// /// Create new logger based on existing SyslogClient. Recommended to use LoggerFactory patern trough SyslogLoggerFactory instead of direct use. @@ -20,10 +24,7 @@ public class SyslogLogger : ILogger /// public SyslogLogger(SyslogClient client, string categoryName) { - if (client is null) - throw new ArgumentNullException(nameof(client)); - - _client = client; + _client = client ?? throw new ArgumentNullException(nameof(client)); LoggerCategoryName = categoryName; } @@ -52,9 +53,13 @@ public void Log(LogLevel logLevel, EventId eventId, string state, Exception exce { string message; if (format is null) + { message = exception == null ? $"{state}" : $"{state} {exception}"; + } else + { message = $"{(string)format.Invoke(null, new object[] { LoggerCategoryName, logLevel, eventId, state, exception })}"; + } _client.SendMessage(LogLevelToSeverity(logLevel), LoggerCategoryName, message); } diff --git a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs index 32360d1..67a9665 100644 --- a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs +++ b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs @@ -1,4 +1,8 @@ -using Microsoft.Extensions.Logging; +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// +using Microsoft.Extensions.Logging; using System; using System.Net; @@ -45,7 +49,10 @@ public SyslogLoggerFactory(string hostname, int port= SyslogClient.DefaultPort, public ILogger CreateLogger(string categoryName) { if (_disposed) + { throw new ObjectDisposedException(nameof(SyslogLoggerFactory)); + } + return new SyslogLogger(_client,categoryName); } @@ -54,7 +61,7 @@ public void Dispose() { _disposed = true; // If logger created through this factory are used after this you will get exception - _client.Dispose(); + _client?.Dispose(); } } } From 09c66a5a3c5ec26c13bdab6c0a431373d19ff44e Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Wed, 26 Jan 2022 12:35:40 +0100 Subject: [PATCH 07/12] Fix last PR remarks --- nanoFramework.Logging.Syslog/Facility.cs | 1 + nanoFramework.Logging.Syslog/Severity.cs | 1 + nanoFramework.Logging.Syslog/SyslogClient.cs | 1 + nanoFramework.Logging.Syslog/SyslogLogger.cs | 28 +++++++++---------- .../SyslogLoggerFactory.cs | 1 + 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/nanoFramework.Logging.Syslog/Facility.cs b/nanoFramework.Logging.Syslog/Facility.cs index d5d92bf..b37acb0 100644 --- a/nanoFramework.Logging.Syslog/Facility.cs +++ b/nanoFramework.Logging.Syslog/Facility.cs @@ -2,6 +2,7 @@ // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // + using System; namespace nanoFramework.Logging.Syslog diff --git a/nanoFramework.Logging.Syslog/Severity.cs b/nanoFramework.Logging.Syslog/Severity.cs index 05770c1..ac6b21f 100644 --- a/nanoFramework.Logging.Syslog/Severity.cs +++ b/nanoFramework.Logging.Syslog/Severity.cs @@ -2,6 +2,7 @@ // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // + using System; namespace nanoFramework.Logging.Syslog diff --git a/nanoFramework.Logging.Syslog/SyslogClient.cs b/nanoFramework.Logging.Syslog/SyslogClient.cs index b0905dd..4c03a55 100644 --- a/nanoFramework.Logging.Syslog/SyslogClient.cs +++ b/nanoFramework.Logging.Syslog/SyslogClient.cs @@ -2,6 +2,7 @@ // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // + using System; using System.Net; using System.Net.Sockets; diff --git a/nanoFramework.Logging.Syslog/SyslogLogger.cs b/nanoFramework.Logging.Syslog/SyslogLogger.cs index c459e8e..a392593 100644 --- a/nanoFramework.Logging.Syslog/SyslogLogger.cs +++ b/nanoFramework.Logging.Syslog/SyslogLogger.cs @@ -2,6 +2,7 @@ // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // + using System; using System.Reflection; using Microsoft.Extensions.Logging; @@ -32,7 +33,7 @@ public SyslogLogger(SyslogClient client, string categoryName) /// Minimum log level used by this logger. /// public LogLevel MinLogLevel { get; set; } //by default 0 -> Trace - + /// /// CategoryName of this logger used as TAG in the Syslog messages. /// @@ -49,7 +50,7 @@ public SyslogLogger(SyslogClient client, string categoryName) /// public void Log(LogLevel logLevel, EventId eventId, string state, Exception exception, MethodInfo format) { - if (logLevel >= MinLogLevel && logLevel!=LogLevel.None) + if (logLevel >= MinLogLevel && logLevel != LogLevel.None) { string message; if (format is null) @@ -64,18 +65,17 @@ public void Log(LogLevel logLevel, EventId eventId, string state, Exception exce _client.SendMessage(LogLevelToSeverity(logLevel), LoggerCategoryName, message); } } - - private Severity LogLevelToSeverity(LogLevel level) => level switch - { - LogLevel.Trace => Severity.Debug, - LogLevel.Debug => Severity.Debug, - LogLevel.Information => Severity.Informational, - LogLevel.Warning => Severity.Warning, - LogLevel.Error => Severity.Error, - LogLevel.Critical => Severity.Critical, - LogLevel.None => Severity.None, - _ => Severity.None - }; + private Severity LogLevelToSeverity(LogLevel level) => level switch + { + LogLevel.Trace => Severity.Debug, + LogLevel.Debug => Severity.Debug, + LogLevel.Information => Severity.Informational, + LogLevel.Warning => Severity.Warning, + LogLevel.Error => Severity.Error, + LogLevel.Critical => Severity.Critical, + LogLevel.None => Severity.None, + _ => Severity.None + }; } } diff --git a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs index 67a9665..173dc3c 100644 --- a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs +++ b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs @@ -2,6 +2,7 @@ // Copyright (c) .NET Foundation and Contributors // See LICENSE file in the project root for full license information. // + using Microsoft.Extensions.Logging; using System; using System.Net; From 9f8025d5329fac78544fcdaf4c5fb7b31f03f4e3 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Wed, 26 Jan 2022 13:47:14 +0100 Subject: [PATCH 08/12] Syslog.nfproj: Use reference to local project instead of nuget --- .../nanoFramework.Logging.Syslog.nfproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj index 6f048f5..c1057bb 100644 --- a/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj +++ b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj @@ -39,9 +39,6 @@ ..\packages\nanoFramework.CoreLibrary.1.12.0-preview.1\lib\mscorlib.dll - - ..\packages\nanoFramework.Logging.1.0.1-preview.10\lib\nanoFramework.Logging.dll - ..\packages\nanoFramework.Runtime.Events.1.10.0-preview.4\lib\nanoFramework.Runtime.Events.dll @@ -59,6 +56,9 @@ + + + From 29e87b4d47b7e3d1530cb5ac046d938397f8d860 Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Wed, 26 Jan 2022 14:41:55 +0100 Subject: [PATCH 09/12] With package versions included --- .../nanoFramework.Logging.Syslog.nfproj | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj index c1057bb..f56a396 100644 --- a/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj +++ b/nanoFramework.Logging.Syslog/nanoFramework.Logging.Syslog.nfproj @@ -15,7 +15,7 @@ nanoFramework.Logging.Syslog nanoFramework.Logging.Syslog v1.0 - bin\$(Configuration)\nanoFramework.Logging.Syslog.xml + bin\$(Configuration)\nanoFramework.Logging.Syslog.xml true @@ -36,19 +36,19 @@ - + ..\packages\nanoFramework.CoreLibrary.1.12.0-preview.1\lib\mscorlib.dll - + ..\packages\nanoFramework.Runtime.Events.1.10.0-preview.4\lib\nanoFramework.Runtime.Events.dll - + ..\packages\nanoFramework.System.Text.1.1.3-preview.7\lib\nanoFramework.System.Text.dll - + ..\packages\nanoFramework.System.Net.1.8.0-preview.15\lib\System.Net.dll - + ..\packages\nanoFramework.System.Threading.1.0.4-preview.8\lib\System.Threading.dll @@ -57,7 +57,7 @@ - + @@ -67,9 +67,9 @@ - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. - - + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. + + \ No newline at end of file From eac0fa78f007d14f1dd675356323c82e1d250db6 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Wed, 26 Jan 2022 14:35:38 +0000 Subject: [PATCH 10/12] Remove Logging from package.config --- nanoFramework.Logging.Syslog/packages.config | 1 - 1 file changed, 1 deletion(-) diff --git a/nanoFramework.Logging.Syslog/packages.config b/nanoFramework.Logging.Syslog/packages.config index 655de39..0fd4153 100644 --- a/nanoFramework.Logging.Syslog/packages.config +++ b/nanoFramework.Logging.Syslog/packages.config @@ -1,7 +1,6 @@  - From 533d2d28694c37f5ee64c3170ceb2712a9b979e2 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Wed, 26 Jan 2022 15:14:34 +0000 Subject: [PATCH 11/12] Miscellaneous improvements - General improvements in comments. - Rename some parameters for clarity. - Add links in comments. - Fix syslog names and typos for coherency. - Wrap parameters. - Fix formatting and code style. --- nanoFramework.Logging.Syslog/Facility.cs | 62 +++++----- nanoFramework.Logging.Syslog/Severity.cs | 19 ++- nanoFramework.Logging.Syslog/SyslogClient.cs | 115 ++++++++++++------ nanoFramework.Logging.Syslog/SyslogLogger.cs | 21 ++-- .../SyslogLoggerFactory.cs | 50 +++++--- 5 files changed, 164 insertions(+), 103 deletions(-) diff --git a/nanoFramework.Logging.Syslog/Facility.cs b/nanoFramework.Logging.Syslog/Facility.cs index b37acb0..4f82f22 100644 --- a/nanoFramework.Logging.Syslog/Facility.cs +++ b/nanoFramework.Logging.Syslog/Facility.cs @@ -3,40 +3,38 @@ // See LICENSE file in the project root for full license information. // -using System; - namespace nanoFramework.Logging.Syslog { - /// - /// Available facility types (RFC 3164). - /// - public enum Facility - { + /// + /// Available facility types (RFC 3164). + /// + public enum Facility + { #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member - Kern = 0, - User = 1, - Mail = 2, - Daemon = 3, - Auth = 4, - Syslog = 5, - LPR = 6, - News = 7, - UUCP = 8, - Cron = 9, - AuthPriv = 10, - FTP = 11, - NTP = 12, - Audit = 13, - Audit2 = 14, - Cron2 = 15, - Local0 = 16, - Local1 = 17, - Local2 = 18, - Local3 = 19, - Local4 = 20, - Local5 = 21, - Local6 = 22, - Local7 = 23 + Kern = 0, + User = 1, + Mail = 2, + Daemon = 3, + Auth = 4, + Syslog = 5, + LPR = 6, + News = 7, + UUCP = 8, + Cron = 9, + AuthPriv = 10, + FTP = 11, + NTP = 12, + Audit = 13, + Audit2 = 14, + Cron2 = 15, + Local0 = 16, + Local1 = 17, + Local2 = 18, + Local3 = 19, + Local4 = 20, + Local5 = 21, + Local6 = 22, + Local7 = 23 #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member - } + } } diff --git a/nanoFramework.Logging.Syslog/Severity.cs b/nanoFramework.Logging.Syslog/Severity.cs index ac6b21f..d8cf619 100644 --- a/nanoFramework.Logging.Syslog/Severity.cs +++ b/nanoFramework.Logging.Syslog/Severity.cs @@ -3,23 +3,22 @@ // See LICENSE file in the project root for full license information. // -using System; - namespace nanoFramework.Logging.Syslog { /// /// Severity represents the Severity in the Syslog messages. This corresponds to the values defined in RFC 3164 except None which means that the message won't be sent. /// - public enum Severity { + public enum Severity + { #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member None = -1, - Emergency, - Alert, - Critical, - Error, - Warning, - Notice, - Informational, + Emergency, + Alert, + Critical, + Error, + Warning, + Notice, + Informational, Debug #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member }; diff --git a/nanoFramework.Logging.Syslog/SyslogClient.cs b/nanoFramework.Logging.Syslog/SyslogClient.cs index 4c03a55..1e68f2b 100644 --- a/nanoFramework.Logging.Syslog/SyslogClient.cs +++ b/nanoFramework.Logging.Syslog/SyslogClient.cs @@ -11,7 +11,7 @@ namespace nanoFramework.Logging.Syslog { /// - /// Syslog Client that sends logs to a syslog server following RFC3164 format. + /// Syslog Client that sends logs to a Syslog server following RFC3164 format. /// public class SyslogClient : IDisposable { @@ -24,14 +24,19 @@ public class SyslogClient : IDisposable public const int DefaultPort = 514; /// - /// Create a SyslogClient instance that will send RFC3164 compliant messages to a syslof server + /// Create a instance that will send RFC3164 compliant messages to a Syslog server. /// - /// Endpoint of the server - /// 'Hostname' part of the RFC3164 message - /// Facility used by this logger - /// Local IP address to bind socket (if null IPAdress.Any will be used) - /// Local port to bind socket (0 to choose available port) - public SyslogClient(IPEndPoint endpoint, string localHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + /// Endpoint of the server. + /// 'Hostname' part of the RFC3164 message. + /// used by this logger. + /// Local IP address to bind (if null will be used). + /// Local port to bind to (0 to choose available port). + public SyslogClient( + IPEndPoint endpoint, + string localHostname, + Facility facility = default, + IPAddress localAddress = null, + int localPort = 0) { Facility = facility; LocalHostname = localHostname ?? throw new ArgumentNullException(nameof(localHostname)); @@ -42,48 +47,72 @@ public SyslogClient(IPEndPoint endpoint, string localHostname, Facility facility } /// - /// Create a SyslogClient instance that will send RFC3164 compliant messages to a syslof server + /// Create a instance that will send RFC3164 compliant messages to a Syslog server. /// - /// DNS name of the syslog server - /// Port of the syslog server - /// 'Hostname' part of the RFC3164 message (if null, use 'nanoframework') - /// Facility used by this logger - /// Local IP address to bind socket (if null IPAdress.Any will be used) - /// Local port to bind socket (0 to choose available port) - public SyslogClient(string hostname, int port, string localHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) - : this(ResolveEndPoint(hostname, port), localHostname, facility, localAddress, localPort) + /// Fully Qualified Domain Name of the Syslog server. + /// Port of the Syslog server. + /// 'Hostname' part of the RFC3164 message (if null, 'nanoframework' will be used). + /// used by this logger. + /// Local IP address to bind to (if null will be used). + /// Local port to bind to (0 to choose available port). + public SyslogClient( + string hostname, + int port, + string localHostname, + Facility facility = default, + IPAddress localAddress = null, + int localPort = 0) + : this( + ResolveEndPoint(hostname, port), + localHostname, + facility, + localAddress, + localPort) { } /// - /// Facility used by this syslog client + /// Facility used by this Syslog client. /// public Facility Facility { get; } - + /// - /// Hostname used in the 'Hostname' field in the syslog message + /// Hostname used in the 'Hostname' field in the Syslog message. /// public string LocalHostname { get; } /// - /// Send a message to the SysLog server + /// Send a message to a SysLog server. /// - /// Severity in the message - /// Message tag (will be followed by ': ' in the message) - /// Message content + /// of the message. + /// Message tag (will be followed by ': ' in the message). + /// Message content. /// - public void SendMessage(Severity severity, string messageTag, string messageContent) => SendMessage(Facility, severity, LocalHostname, messageTag, messageContent); + public void SendMessage( + Severity severity, + string tag, + string content) => SendMessage( + Facility, + severity, + LocalHostname, + tag, + content); /// - /// Send a generic syslog message to the SysLog server + /// Send a generic Syslog message to a SysLog server. /// - /// Facility in the message - /// Severity in the message + /// in the message + /// in the message /// Hostname in the message - /// Message tag (will be followed by ': ' in the message) - /// Message content + /// Message tag (will be followed by ': ' in the message). + /// Message content /// - public void SendMessage(Facility facility,Severity severity, string hostname, string messageTag, string messageContent) + public void SendMessage( + Facility facility, + Severity severity, + string hostname, + string tag, + string content) { if (_disposed) { @@ -91,42 +120,52 @@ public void SendMessage(Facility facility,Severity severity, string hostname, st } // We don't send message with Severity.None which isn't a RFC6134 valid value - if (severity != Severity.None) + if (severity != Severity.None) { - _socket.Send(FormatUdpMessage(facility, severity, hostname, messageTag, messageContent)); + _socket.Send(FormatUdpMessage(facility, severity, hostname, tag, content)); } } - private static byte[] FormatUdpMessage(Facility facility,Severity severity, string localHostname, string messageTag, string messageContent) + private static byte[] FormatUdpMessage( + Facility facility, + Severity severity, + string localHostname, + string messageTag, + string messageContent) { int priorityValue = ((int)facility * 8) + (int)severity; var headerBuilder = new StringBuilder(); + // RFC3164 PRI headerBuilder.Append('<').Append(priorityValue).Append('>'); + // RFC3164 HEADER string timestamp = DateTime.UtcNow.ToString("MMM dd HH:mm:ss"); headerBuilder.Append(timestamp).Append(' '); headerBuilder.Append(localHostname).Append(' '); + // RFC3164 MSG if (messageTag != null) { headerBuilder.Append(messageTag).Append(':').Append(' '); } - + headerBuilder.Append(messageContent ?? ""); var s = headerBuilder.ToString(); return UTF8Encoding.UTF8.GetBytes(s); // Ideally an UTF7 is required here to be RFC3164 compliant } - private static IPEndPoint ResolveEndPoint(string hostname, int port) + private static IPEndPoint ResolveEndPoint( + string hostname, + int port) { IPHostEntry hostEntry = Dns.GetHostEntry(hostname); if (hostEntry is null) - { + { throw new ArgumentException($"Hostname `{hostname}` is not resolvable"); } - + return new IPEndPoint(hostEntry.AddressList[0], port); } diff --git a/nanoFramework.Logging.Syslog/SyslogLogger.cs b/nanoFramework.Logging.Syslog/SyslogLogger.cs index a392593..d78389c 100644 --- a/nanoFramework.Logging.Syslog/SyslogLogger.cs +++ b/nanoFramework.Logging.Syslog/SyslogLogger.cs @@ -10,7 +10,7 @@ namespace nanoFramework.Logging.Syslog { /// - /// ILogger implmentation that send message log to a Syslog server following RFC3164 convention. + /// implementation that send message log to a Syslog server following RFC3164 convention. /// While not mandatory it is recommended to use to create the SyslogLogger instances. /// public class SyslogLogger : ILogger @@ -18,12 +18,14 @@ public class SyslogLogger : ILogger private readonly SyslogClient _client; /// - /// Create new logger based on existing SyslogClient. Recommended to use LoggerFactory patern trough SyslogLoggerFactory instead of direct use. + /// Create new logger based on existing . Recommended to use LoggerFactory pattern trough instead of direct use. /// - /// SyslogClient to use for sending message - /// CategoryName of this logger used as TAG in the Syslog messages + /// to use for sending message. + /// CategoryName of this logger used as TAG in the Sys log messages. /// - public SyslogLogger(SyslogClient client, string categoryName) + public SyslogLogger( + SyslogClient client, + string categoryName) { _client = client ?? throw new ArgumentNullException(nameof(client)); LoggerCategoryName = categoryName; @@ -40,7 +42,7 @@ public SyslogLogger(SyslogClient client, string categoryName) public string LoggerCategoryName { get; } /// - /// Underlying Syslog client used by this logger. + /// Underlying used by this logger. /// public SyslogClient Client => _client; @@ -48,7 +50,12 @@ public SyslogLogger(SyslogClient client, string categoryName) public bool IsEnabled(LogLevel logLevel) => logLevel >= MinLogLevel; /// - public void Log(LogLevel logLevel, EventId eventId, string state, Exception exception, MethodInfo format) + public void Log( + LogLevel logLevel, + EventId eventId, + string state, + Exception exception, + MethodInfo format) { if (logLevel >= MinLogLevel && logLevel != LogLevel.None) { diff --git a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs index 173dc3c..d424cb5 100644 --- a/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs +++ b/nanoFramework.Logging.Syslog/SyslogLoggerFactory.cs @@ -6,11 +6,12 @@ using Microsoft.Extensions.Logging; using System; using System.Net; +using System.Net.Sockets; namespace nanoFramework.Logging.Syslog { /// - /// ILoggerFactory implementation that creates SyslogLogger instances. + /// implementation that creates instances. /// The logger factory initializes an underlying that will be shared by all instances it will create. /// public class SyslogLoggerFactory : ILoggerFactory @@ -22,12 +23,17 @@ public class SyslogLoggerFactory : ILoggerFactory /// /// Create a logging factory that will provide support for Syslog ILogger provider /// - /// Endpoint of the server - /// 'Hostname' part of the RFC3164 message - /// Facility used by this logger - /// Local IP address to bind socket (if null IPAdress.Any will be used) - /// Local port to bind socket (0 to choose available port) - public SyslogLoggerFactory(IPEndPoint endpoint, string localHostname=DefaultHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + /// Endpoint of the server. + /// 'Hostname' part of the RFC3164 message. + /// used by this logger. + /// Local IP address to bind to (if null will be used). + /// Local port to bind to (0 to choose available port). + public SyslogLoggerFactory( + IPEndPoint endpoint, + string localHostname = DefaultHostname, + Facility facility = default, + IPAddress localAddress = null, + int localPort = 0) { _client = new SyslogClient(endpoint, localHostname, facility, localAddress, localPort); } @@ -35,15 +41,27 @@ public SyslogLoggerFactory(IPEndPoint endpoint, string localHostname=DefaultHost /// /// Create a logging factory that will provide support for Syslog ILogger provider /// - /// DNS name of the syslog server - /// Port of the syslog server (default to 514) + /// Fully Qualified Domain Name of the Syslog server. + /// Port of the Syslog server (default is 514). /// 'Hostname' part of the RFC3164 message - /// Facility used by this logger - /// Local IP address to bind socket (if null IPAdress.Any will be used) - /// Local port to bind socket (0 to choose available port) - public SyslogLoggerFactory(string hostname, int port= SyslogClient.DefaultPort, string localHostname=DefaultHostname, Facility facility = default, IPAddress localAddress = null, int localPort = 0) + /// used by this logger + /// Local IP address to bind to (if null will be used). + /// Local port to bind (0 to choose available port). + public SyslogLoggerFactory( + string hostname, + int port = SyslogClient.DefaultPort, + string localHostname = DefaultHostname, + Facility facility = default, + IPAddress localAddress = null, + int localPort = 0) { - _client = new SyslogClient(hostname, port, localHostname, facility , localAddress, localPort); + _client = new SyslogClient( + hostname, + port, + localHostname, + facility, + localAddress, + localPort); } /// @@ -53,8 +71,8 @@ public ILogger CreateLogger(string categoryName) { throw new ObjectDisposedException(nameof(SyslogLoggerFactory)); } - - return new SyslogLogger(_client,categoryName); + + return new SyslogLogger(_client, categoryName); } /// From ad0ae20247db7001e34a82727fdbeac108d0867b Mon Sep 17 00:00:00 2001 From: Olivier Blaise <16148676+oblaise@users.noreply.github.com> Date: Wed, 26 Jan 2022 17:49:27 +0100 Subject: [PATCH 12/12] XML comments for enums --- nanoFramework.Logging.Syslog/Facility.cs | 74 +++++++++++++++++++++++- nanoFramework.Logging.Syslog/Severity.cs | 31 +++++++++- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/nanoFramework.Logging.Syslog/Facility.cs b/nanoFramework.Logging.Syslog/Facility.cs index 4f82f22..6bacae4 100644 --- a/nanoFramework.Logging.Syslog/Facility.cs +++ b/nanoFramework.Logging.Syslog/Facility.cs @@ -10,31 +10,101 @@ namespace nanoFramework.Logging.Syslog /// public enum Facility { -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + /// + /// kernel messages + /// Kern = 0, + /// + /// user-level messages + /// User = 1, + /// + /// mail system + /// Mail = 2, + /// + /// system daemons + /// Daemon = 3, + /// + /// security/authorization messages + /// Auth = 4, + /// + /// messages generated internally by syslogd + /// Syslog = 5, + /// + /// line printer subsystem + /// LPR = 6, + /// + /// network news subsystem + /// News = 7, + /// + /// UUCP subsystem + /// UUCP = 8, + /// + /// clock daemon + /// Cron = 9, + /// + /// security/authorization messages + /// AuthPriv = 10, + /// + /// FTP daemon + /// FTP = 11, + /// + /// NTP subsystem + /// NTP = 12, + /// + /// log audit + /// Audit = 13, + /// + /// log alert + /// Audit2 = 14, + /// + /// clock daemon + /// Cron2 = 15, + /// + /// local use 0 (local0) + /// Local0 = 16, + /// + /// local use 1 (local1) + /// Local1 = 17, + /// + /// local use 2 (local2) + /// Local2 = 18, + /// + /// local use 3 (local3) + /// Local3 = 19, + /// + /// local use 4 (local4) + /// Local4 = 20, + /// + /// local use 5 (local5) + /// Local5 = 21, + /// + /// local use 6 (local6) + /// Local6 = 22, + /// + /// local use 7 (local7) + /// Local7 = 23 -#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member } } diff --git a/nanoFramework.Logging.Syslog/Severity.cs b/nanoFramework.Logging.Syslog/Severity.cs index d8cf619..4c715ce 100644 --- a/nanoFramework.Logging.Syslog/Severity.cs +++ b/nanoFramework.Logging.Syslog/Severity.cs @@ -3,6 +3,8 @@ // See LICENSE file in the project root for full license information. // +using Microsoft.Extensions.Logging; + namespace nanoFramework.Logging.Syslog { /// @@ -10,16 +12,41 @@ namespace nanoFramework.Logging.Syslog /// public enum Severity { -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + /// + /// Not an official RFC3164 facility. No entry will be generated when called with this Severity + /// None = -1, + /// + /// Emergency: system is unusable. No equivalent in + /// Emergency, + /// + /// Alert: action must be taken immediately. No equivalent in + /// Alert, + /// + /// Critical: critical conditions. Mapped to + /// Critical, + /// + /// Error: error conditions. Mapped to + /// Error, + /// + /// Warning: warning conditions. Mapped to + /// Warning, + /// + /// Notice: normal but significant condition. No equivalent in + /// Notice, + /// + /// Informational: informational messages. Mapped to + /// Informational, + /// + /// Debug: debug-level messages. Mapped to and + /// Debug -#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member }; }