nanoFramework.Logging 1.1.150

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Logging --version 1.1.150
                    
NuGet\Install-Package nanoFramework.Logging -Version 1.1.150
                    
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" Version="1.1.150" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Logging" Version="1.1.150" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Logging" />
                    
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 --version 1.1.150
                    
#r "nuget: nanoFramework.Logging, 1.1.150"
                    
#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&version=1.1.150
                    
Install nanoFramework.Logging as a Cake Addin
#tool nuget:?package=nanoFramework.Logging&version=1.1.150
                    
Install nanoFramework.Logging 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 (18)

Showing the top 5 NuGet packages that depend on nanoFramework.Logging:

Package Downloads
nanoFramework.Logging.Stream

This package includes the nanoFramework.Logging.Stream assembly (Stream Logging only) for .NET nanoFramework C# projects. There is also a package with the Serial Logger only and another with the basic Debug Logger.

nanoFramework.Logging.Serial

This package includes the nanoFramework.Logging.Serial assembly (Serial Logging only) for .NET nanoFramework C# projects. There is also a package with the Stream Logging only and another with the basic Debug Logging.

nanoFramework.Iot.Device.Mfrc522

This package includes the .NET IoT Core binding Iot.Device.Mfrc522 for .NET nanoFramework C# projects.

nanoFramework.Iot.Device.Pn532

This package includes the .NET IoT Core binding Iot.Device.Pn532 for .NET nanoFramework C# projects.

nanoFramework.Iot.Device.Pn5180

This package includes the .NET IoT Core binding Iot.Device.Pn5180 for .NET nanoFramework C# projects.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on nanoFramework.Logging:

Repository Stars
nanoframework/Samples
🍬 Code samples from the nanoFramework team used in testing, proof of concepts and other explorational endeavours
nanoframework/nanoFramework.IoT.Device
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
Version Downloads Last updated
1.1.156 940 4/2/2025
1.1.155 308 4/2/2025
1.1.154 211 4/2/2025
1.1.153 210 4/2/2025
1.1.151 1,578 3/10/2025
1.1.150 298 3/10/2025
1.1.149 236 3/10/2025
1.1.148 252 3/10/2025
1.1.147 806 3/5/2025
1.1.146 573 3/3/2025
1.1.145 180 3/3/2025
1.1.144 572 2/27/2025
1.1.142 823 2/25/2025
1.1.140 3,138 2/5/2025
1.1.139 195 2/5/2025
1.1.138 186 2/4/2025
1.1.137 175 2/4/2025
1.1.136 190 2/4/2025
1.1.135 478 2/4/2025
1.1.134 916 2/4/2025
1.1.133 239 2/4/2025
1.1.131 1,639 1/31/2025
1.1.127 262 1/30/2025
1.1.125 1,829 1/8/2025
1.1.124 907 1/2/2025
1.1.123 190 1/2/2025
1.1.120 1,685 12/6/2024
1.1.113 2,927 9/30/2024
1.1.108 5,453 8/1/2024
1.1.107 937 7/23/2024
1.1.100 5,388 5/15/2024
1.1.98 539 5/13/2024
1.1.96 791 5/10/2024
1.1.94 2,644 4/10/2024
1.1.92 457 4/9/2024
1.1.90 247 4/8/2024
1.1.88 923 4/4/2024
1.1.86 460 4/3/2024
1.1.84 272 4/3/2024
1.1.81 4,237 2/1/2024
1.1.79 1,113 1/26/2024
1.1.76 6,014 11/16/2023
1.1.74 2,611 11/10/2023
1.1.63 19,452 1/4/2023
1.1.60 2,030 12/28/2022
1.1.58 1,054 12/28/2022
1.1.47 4,052 10/28/2022
1.1.45 1,478 10/26/2022
1.1.43 1,159 10/26/2022
1.1.41 2,360 10/25/2022
1.1.39 1,204 10/25/2022
1.1.37 1,502 10/24/2022
1.1.35 1,233 10/24/2022
1.1.33 1,276 10/24/2022
1.1.31 1,491 10/23/2022
1.1.29 1,231 10/23/2022
1.1.27 2,051 10/22/2022
1.1.25 3,458 10/10/2022
1.1.23 2,719 10/7/2022
1.1.19 2,776 9/22/2022
1.1.17 2,417 9/16/2022
1.1.15 1,340 9/15/2022
1.1.13 1,353 9/15/2022
1.1.9 1,394 9/15/2022
1.1.7 1,339 9/15/2022
1.1.2 5,530 8/5/2022
1.0.1.29 6,492 6/17/2022
1.0.1.27 2,242 6/16/2022
1.0.1.25 2,179 6/14/2022
1.0.1.23 2,141 6/13/2022
1.0.1.21 1,339 6/9/2022
1.0.1.19 1,333 6/8/2022
1.0.1.15 2,362 5/27/2022
1.0.1.13 2,814 5/19/2022
1.0.1.12 2,279 5/4/2022
1.0.1 5,861 3/28/2022
1.0.1-preview.32 160 3/28/2022
1.0.1-preview.31 146 3/28/2022
1.0.1-preview.30 153 3/28/2022
1.0.1-preview.28 227 3/18/2022
1.0.1-preview.27 155 3/17/2022
1.0.1-preview.26 163 3/15/2022
1.0.1-preview.25 174 3/14/2022
1.0.1-preview.24 167 3/11/2022
1.0.1-preview.23 228 2/25/2022
1.0.1-preview.21 233 2/17/2022
1.0.1-preview.19 194 2/11/2022
1.0.1-preview.18 187 2/8/2022
1.0.1-preview.17 186 2/4/2022
1.0.1-preview.16 209 1/28/2022
1.0.1-preview.15 191 1/28/2022
1.0.1-preview.14 197 1/28/2022
1.0.1-preview.13 197 1/27/2022
1.0.1-preview.12 171 1/26/2022
1.0.1-preview.11 163 1/25/2022
1.0.1-preview.10 179 1/25/2022
1.0.1-preview.9 190 1/21/2022
1.0.1-preview.8 172 1/21/2022
1.0.1-preview.6 215 1/14/2022
1.0.1-preview.4 228 1/4/2022
1.0.1-preview.3 176 12/29/2021
1.0.0 1,661 12/5/2021
1.0.0-preview.142 191 12/5/2021
1.0.0-preview.141 179 12/5/2021
1.0.0-preview.137 318 10/19/2021
1.0.0-preview.135 233 10/18/2021
1.0.0-preview.133 383 10/15/2021
1.0.0-preview.131 322 9/30/2021
1.0.0-preview.129 220 9/27/2021
1.0.0-preview.127 297 9/26/2021
1.0.0-preview.122 941 7/18/2021
1.0.0-preview.120 283 7/17/2021
1.0.0-preview.118 592 7/16/2021
1.0.0-preview.116 208 7/15/2021
1.0.0-preview.114 228 7/14/2021
1.0.0-preview.110 290 6/29/2021
1.0.0-preview.108 290 6/27/2021
1.0.0-preview.106 198 6/21/2021
1.0.0-preview.104 226 6/20/2021
1.0.0-preview.102 182 6/19/2021
1.0.0-preview.100 190 6/19/2021
1.0.0-preview.98 248 6/9/2021
1.0.0-preview.96 211 6/8/2021
1.0.0-preview.94 199 6/7/2021
1.0.0-preview.92 225 6/7/2021
1.0.0-preview.90 203 6/6/2021
1.0.0-preview.88 297 6/5/2021
1.0.0-preview.86 189 6/4/2021
1.0.0-preview.84 172 6/3/2021
1.0.0-preview.82 220 6/3/2021
1.0.0-preview.80 199 6/2/2021
1.0.0-preview.78 225 6/1/2021
1.0.0-preview.76 218 5/31/2021
1.0.0-preview.74 227 5/31/2021
1.0.0-preview.72 218 5/31/2021
1.0.0-preview.70 254 5/30/2021
1.0.0-preview.68 224 5/30/2021
1.0.0-preview.66 192 5/29/2021
1.0.0-preview.64 194 5/27/2021
1.0.0-preview.62 199 5/26/2021
1.0.0-preview.60 336 5/25/2021
1.0.0-preview.58 325 5/23/2021
1.0.0-preview.56 215 5/22/2021
1.0.0-preview.54 244 5/21/2021
1.0.0-preview.52 255 5/20/2021
1.0.0-preview.50 186 5/19/2021
1.0.0-preview.48 215 5/19/2021
1.0.0-preview.46 305 5/16/2021
1.0.0-preview.44 201 5/15/2021
1.0.0-preview.42 195 5/14/2021
1.0.0-preview.40 200 5/13/2021
1.0.0-preview.38 190 5/12/2021
1.0.0-preview.36 253 5/7/2021
1.0.0-preview.34 250 5/6/2021
1.0.0-preview.32 197 5/5/2021
1.0.0-preview.30 200 5/4/2021
1.0.0-preview.28 192 5/4/2021
1.0.0-preview.26 210 4/23/2021
1.0.0-preview.25 197 4/12/2021
1.0.0-preview.24 207 4/10/2021
1.0.0-preview.21 713 4/9/2021
1.0.0-preview.19 206 4/9/2021
1.0.0-preview.17 224 4/8/2021
1.0.0-preview.14 221 4/8/2021
1.0.0-preview.13 221 4/8/2021