nanoFramework.Logging.Syslog 1.1.145

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Logging.Syslog --version 1.1.145
                    
NuGet\Install-Package nanoFramework.Logging.Syslog -Version 1.1.145
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Logging.Syslog" Version="1.1.145" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Logging.Syslog" Version="1.1.145" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Logging.Syslog" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoFramework.Logging.Syslog --version 1.1.145
                    
#r "nuget: nanoFramework.Logging.Syslog, 1.1.145"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=nanoFramework.Logging.Syslog&version=1.1.145
                    
Install nanoFramework.Logging.Syslog as a Cake Addin
#tool nuget:?package=nanoFramework.Logging.Syslog&version=1.1.145
                    
Install nanoFramework.Logging.Syslog as a Cake Tool

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework nanoFramework.Logging Library repository

Build status

Component Build Status NuGet Package
nanoFramework.Logging Build Status NuGet
nanoFramework.Logging.Serial Build Status NuGet
nanoFramework.Logging.Stream Build Status NuGet
nanoFramework.Logging.Syslog Build Status NuGet

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Usage

In your class, make sure you have a global ILogger declared and in your constructor that you call _logger = this.GetCurrentClassLogger();

using Microsoft.Extensions.Logging;
using nanoFramework.Logging;
using System;

namespace UnitTestDebugLogging
{
    internal class MyTestComponent
    {
        private ILogger _logger;

        public MyTestComponent()
        {
            _logger = this.GetCurrentClassLogger();
        }

        public void DoSomeLogging()
        {
            _logger.LogInformation("An informative message");
            _logger.LogError("An error situation");
            _logger.LogWarning(new Exception("Something is not supported"), "With exception context");
        }
    }
}

In your main code, you'll need to create a logger:

LogDispatcher.LoggerFactory = new DebugLoggerFactory();
// Then you can create your object and the logging will happen
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();

You can have 3 different types of logger: Debug, Serial and Stream.

Debug logger

As presented previously, you can use the Factory pattern:

LogDispatcher.LoggerFactory = new DebugLoggerFactory();
// Then you can create your object and the logging will happen
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();

You can as well directly create a DebugLogger:

DebugLogger _logger;
_logger = new DebugLogger("test");
_logger.MinLogLevel = LogLevel.Trace; 
_logger.LogTrace("This is a trace");

Serial logger

You can use the Factory pattern:

LogDispatcher.LoggerFactory = new SerialLoggerFactory("COM6");
// Then you can create your object and the logging will happen
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();

Note that you can adjust the baud speed and all other elements.

Or directly using a SerialLogger:

SerialPort _serial;
_serial = new SerialPort("COM6", 115200);
SerialLogger _logger = new SerialLogger(ref _serial);
_logger.MinLogLevel = LogLevel.Trace; 
_logger.LogTrace("This is a trace");

Important: make sure to refer to the documentation of your board to understand how to properly setup the serial port. The tests include an example with an ESP32.

Stream logger

Similar as for the others, you can either use a FileStream or a Stream in the LoggerFactory:

MemoryStream memoryStream = new MemoryStream();
LogDispatcher.LoggerFactory = new StreamLoggerFactory(memoryStream);
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();

And you can as well use it directly:

var _stream = new FileStream("D:\\mylog.txt", FileMode.Open, FileAccess.ReadWrite);
StreamLogger _logger = new StreamLogger(_stream);
_logger.MinLogLevel = LogLevel.Trace; 
_logger.LogTrace("This is a trace");

Important: please refer to the documentation for USB and SD Card reader to make sure they are properly setup before trying to setup the logger.

Create your own logger

You can create your own logger using the ILogger and ILoggerFactory interfaces. The DebugLogger is the simplest one.

The Log extensions

Different Log extensions are at your disposal to help you log the way you like. You can simply log a string or having parameters as well as exception and EventId:

_logger.LogTrace("TRACE {0} {1}", new object[] { "param 1", 42 });
_logger.LogDebug("DEBUG {0} {1}", new object[] { "param 1", 42 });
_logger.LogInformation("INFORMATION and nothing else");
_logger.LogWarning("WARNING {0} {1}", new object[] { "param 1", 42 });
_logger.LogError(new Exception("Big problem"), "ERROR {0} {1}", new object[] { "param 1", 42 });
_logger.LogCritical(42, new Exception("Insane problem"), "CRITICAL {0} {1}", new object[] { "param 1", 42 });

Note that all log level extensions have a minimum of string logging upo to EventId, string, parameters and exception. You are responsible to format properly the string when there are parameters.

Log level

You can adjust the log level in all the predefined logger. For example:

DebugLogger _logger;
_logger = new DebugLogger("test");
_logger.MinLogLevel = LogLevel.Trace;
_logger.LogTrace("This will be displayed");
_logger.LogCritical("Critical message will be displayed");
_logger.MinLogLevel = LogLevel.Critical;
_logger.LogTrace("This won't be displayed, only critical will be");
_logger.LogCritical("Critical message will be displayed");

Create your own formatting

You can use a custom formatter which will give you the name of the logger, the log level, the event ID, the message itself and a potential exception. The function definition should follow the following pattern:

public interface IMessageFormatter
{     
    string MessageFormatter(string className, LogLevel logLevel, EventId eventId, string state, Exception exception);
}

Important: this function will be called directly, without instantiating the class it is part of. So make sure either this function is a static, either it's part of the class using the logger. The static option always works. The interface is given for convenience and to give the format.

To setup the formatting, just use the following line. The type of the class containing the function and the exact name of the function are required.

LoggerExtensions.MessageFormatter = typeof(MyFormatter).GetType().GetMethod("MessageFormatterStatic");

public class MyFormatter
{        
    public string MessageFormatterStatic(string className, LogLevel logLevel, EventId eventId, string state, Exception exception)
    {
        string logstr = string.Empty;
        switch (logLevel)
        {
            case LogLevel.Trace:
                logstr = "TRACE: ";
                break;
            case LogLevel.Debug:
                logstr = "I love debug: ";
                break;
            case LogLevel.Warning:
                logstr = "WARNING: ";
                break;
            case LogLevel.Error:
                logstr = "ERROR: ";
                break;
            case LogLevel.Critical:
                logstr = "CRITICAL:";
                break;
            case LogLevel.None:
            case LogLevel.Information:
            default:
                break;
        }

        string eventstr = eventId.Id != 0 ? $" Event ID: {eventId}, " : string.Empty;
        string msg = $"[{className}] {eventstr}{logstr} {state}";
        if (exception != null)
        {
            msg += $" {exception}";
        }

        return msg;
    }
}

You are free to use anything you'd like and format as you like the message.

Note: It is not necessary to add a \r\n at the end, this is done by each logger.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.156 156 24 days ago
1.1.155 147 24 days ago
1.1.154 152 24 days ago
1.1.153 149 24 days ago
1.1.151 182 2 months ago
1.1.150 157 2 months ago
1.1.149 157 2 months ago
1.1.148 171 2 months ago
1.1.147 212 2 months ago
1.1.146 101 2 months ago
1.1.145 100 2 months ago
1.1.144 103 2 months ago
1.1.142 96 2 months ago
1.1.140 103 3 months ago
1.1.139 94 3 months ago
1.1.138 95 3 months ago
1.1.137 99 3 months ago
1.1.136 103 3 months ago
1.1.135 99 3 months ago
1.1.134 101 3 months ago
1.1.133 99 3 months ago
1.1.131 104 3 months ago
1.1.127 92 3 months ago
1.1.125 100 4 months ago
1.1.124 117 4 months ago
1.1.123 115 4 months ago
1.1.120 117 5 months ago
1.1.113 110 7 months ago
1.1.108 117 9 months ago
1.1.107 92 9 months ago
1.1.100 140 5/15/2024
1.1.98 116 5/13/2024
1.1.96 128 5/10/2024
1.1.94 147 4/10/2024
1.1.92 136 4/9/2024
1.1.90 136 4/8/2024
1.1.88 135 4/4/2024
1.1.86 131 4/3/2024
1.1.84 127 4/3/2024
1.1.81 155 2/1/2024
1.1.79 121 1/26/2024
1.1.76 228 11/16/2023
1.1.74 131 11/10/2023
1.1.63 398 1/4/2023
1.1.60 321 12/28/2022
1.1.58 308 12/28/2022
1.1.47 535 10/28/2022
1.1.45 436 10/26/2022
1.1.43 393 10/26/2022
1.1.41 393 10/25/2022
1.1.39 399 10/25/2022
1.1.37 429 10/24/2022
1.1.35 455 10/24/2022
1.1.33 437 10/24/2022
1.1.31 433 10/23/2022
1.1.29 445 10/23/2022
1.1.27 436 10/22/2022
1.1.25 430 10/10/2022
1.1.23 422 10/7/2022
1.1.19 473 9/22/2022
1.1.17 476 9/16/2022
1.1.15 441 9/15/2022
1.1.13 471 9/15/2022
1.1.9 488 9/15/2022
1.1.7 470 9/15/2022
1.1.2 463 8/5/2022
1.0.1.29 468 6/17/2022
1.0.1.27 461 6/16/2022
1.0.1.25 451 6/14/2022
1.0.1.23 449 6/13/2022
1.0.1.21 462 6/9/2022
1.0.1.19 483 6/8/2022
1.0.1.15 434 5/27/2022
1.0.1.13 463 5/19/2022
1.0.1.12 470 5/4/2022
1.0.1 473 3/28/2022
1.0.1-preview.32 156 3/28/2022
1.0.1-preview.31 152 3/28/2022
1.0.1-preview.30 139 3/28/2022
1.0.1-preview.28 149 3/18/2022
1.0.1-preview.27 147 3/17/2022
1.0.1-preview.26 143 3/15/2022
1.0.1-preview.25 148 3/14/2022
1.0.1-preview.24 135 3/11/2022
1.0.1-preview.23 146 2/25/2022
1.0.1-preview.21 150 2/17/2022
1.0.1-preview.19 135 2/11/2022
1.0.1-preview.18 150 2/8/2022
1.0.1-preview.17 162 2/4/2022
1.0.1-preview.16 161 1/28/2022
1.0.1-preview.15 153 1/28/2022
1.0.1-preview.14 157 1/28/2022
1.0.1-preview.13 163 1/27/2022