nanoFramework.Logging.Stream
1.0.1
Prefix Reserved
See the version list below for details.
dotnet add package nanoFramework.Logging.Stream --version 1.0.1
NuGet\Install-Package nanoFramework.Logging.Stream -Version 1.0.1
<PackageReference Include="nanoFramework.Logging.Stream" Version="1.0.1" />
paket add nanoFramework.Logging.Stream --version 1.0.1
#r "nuget: nanoFramework.Logging.Stream, 1.0.1"
// Install nanoFramework.Logging.Stream as a Cake Addin #addin nuget:?package=nanoFramework.Logging.Stream&version=1.0.1 // Install nanoFramework.Logging.Stream as a Cake Tool #tool nuget:?package=nanoFramework.Logging.Stream&version=1.0.1
Welcome to the .NET nanoFramework nanoFramework.Logging Library repository
Build status
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.12.0)
- nanoFramework.Logging (>= 1.0.1)
- nanoFramework.System.IO.FileSystem (>= 1.0.3)
- nanoFramework.System.Text (>= 1.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on nanoFramework.Logging.Stream:
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.113 | 336 | 9/30/2024 |
1.1.108 | 362 | 8/1/2024 |
1.1.107 | 173 | 7/23/2024 |
1.1.100 | 406 | 5/15/2024 |
1.1.98 | 79 | 5/13/2024 |
1.1.96 | 142 | 5/10/2024 |
1.1.94 | 276 | 4/10/2024 |
1.1.92 | 120 | 4/9/2024 |
1.1.90 | 102 | 4/8/2024 |
1.1.88 | 148 | 4/4/2024 |
1.1.86 | 122 | 4/3/2024 |
1.1.84 | 111 | 4/3/2024 |
1.1.81 | 499 | 2/1/2024 |
1.1.79 | 128 | 1/26/2024 |
1.1.76 | 537 | 11/16/2023 |
1.1.74 | 186 | 11/10/2023 |
1.1.63 | 2,471 | 1/4/2023 |
1.1.60 | 600 | 12/28/2022 |
1.1.58 | 300 | 12/28/2022 |
1.1.47 | 778 | 10/28/2022 |
1.1.45 | 424 | 10/26/2022 |
1.1.43 | 378 | 10/26/2022 |
1.1.41 | 386 | 10/25/2022 |
1.1.39 | 427 | 10/25/2022 |
1.1.37 | 402 | 10/24/2022 |
1.1.35 | 416 | 10/24/2022 |
1.1.33 | 416 | 10/24/2022 |
1.1.31 | 405 | 10/23/2022 |
1.1.29 | 396 | 10/23/2022 |
1.1.27 | 416 | 10/22/2022 |
1.1.25 | 470 | 10/10/2022 |
1.1.23 | 434 | 10/7/2022 |
1.1.19 | 875 | 9/22/2022 |
1.1.17 | 505 | 9/16/2022 |
1.1.15 | 430 | 9/15/2022 |
1.1.13 | 464 | 9/15/2022 |
1.1.9 | 460 | 9/15/2022 |
1.1.7 | 459 | 9/15/2022 |
1.1.2 | 797 | 8/5/2022 |
1.0.1.29 | 779 | 6/17/2022 |
1.0.1.27 | 444 | 6/16/2022 |
1.0.1.25 | 446 | 6/14/2022 |
1.0.1.23 | 429 | 6/13/2022 |
1.0.1.21 | 463 | 6/9/2022 |
1.0.1.19 | 408 | 6/8/2022 |
1.0.1.15 | 556 | 5/27/2022 |
1.0.1.13 | 490 | 5/19/2022 |
1.0.1.12 | 550 | 5/4/2022 |
1.0.1 | 679 | 3/28/2022 |
1.0.1-preview.32 | 125 | 3/28/2022 |
1.0.1-preview.31 | 127 | 3/28/2022 |
1.0.1-preview.30 | 126 | 3/28/2022 |
1.0.1-preview.28 | 199 | 3/18/2022 |
1.0.1-preview.27 | 130 | 3/17/2022 |
1.0.1-preview.26 | 143 | 3/15/2022 |
1.0.1-preview.25 | 147 | 3/14/2022 |
1.0.1-preview.24 | 146 | 3/11/2022 |
1.0.1-preview.23 | 205 | 2/25/2022 |
1.0.1-preview.21 | 154 | 2/17/2022 |
1.0.1-preview.19 | 151 | 2/11/2022 |
1.0.1-preview.18 | 146 | 2/8/2022 |
1.0.1-preview.17 | 146 | 2/4/2022 |
1.0.1-preview.16 | 173 | 1/28/2022 |
1.0.1-preview.15 | 133 | 1/28/2022 |
1.0.1-preview.14 | 167 | 1/28/2022 |
1.0.1-preview.13 | 160 | 1/27/2022 |
1.0.1-preview.12 | 134 | 1/26/2022 |
1.0.1-preview.11 | 136 | 1/25/2022 |
1.0.1-preview.10 | 134 | 1/25/2022 |
1.0.1-preview.9 | 150 | 1/21/2022 |
1.0.1-preview.8 | 141 | 1/21/2022 |
1.0.1-preview.6 | 137 | 1/14/2022 |
1.0.1-preview.4 | 182 | 1/4/2022 |
1.0.1-preview.3 | 142 | 12/29/2021 |
1.0.0 | 445 | 12/5/2021 |
1.0.0-preview.142 | 158 | 12/5/2021 |
1.0.0-preview.141 | 149 | 12/5/2021 |
1.0.0-preview.137 | 168 | 10/19/2021 |
1.0.0-preview.135 | 195 | 10/18/2021 |
1.0.0-preview.133 | 349 | 10/15/2021 |
1.0.0-preview.131 | 271 | 9/30/2021 |
1.0.0-preview.129 | 176 | 9/27/2021 |
1.0.0-preview.127 | 257 | 9/26/2021 |
1.0.0-preview.122 | 450 | 7/18/2021 |
1.0.0-preview.120 | 246 | 7/17/2021 |
1.0.0-preview.118 | 157 | 7/16/2021 |
1.0.0-preview.116 | 174 | 7/15/2021 |
1.0.0-preview.114 | 177 | 7/14/2021 |
1.0.0-preview.110 | 234 | 6/29/2021 |
1.0.0-preview.108 | 265 | 6/27/2021 |
1.0.0-preview.106 | 170 | 6/21/2021 |
1.0.0-preview.104 | 192 | 6/20/2021 |
1.0.0-preview.102 | 149 | 6/19/2021 |
1.0.0-preview.100 | 160 | 6/19/2021 |
1.0.0-preview.98 | 219 | 6/9/2021 |
1.0.0-preview.96 | 151 | 6/8/2021 |
1.0.0-preview.94 | 173 | 6/7/2021 |
1.0.0-preview.92 | 190 | 6/7/2021 |
1.0.0-preview.90 | 177 | 6/6/2021 |
1.0.0-preview.88 | 265 | 6/5/2021 |
1.0.0-preview.86 | 157 | 6/4/2021 |
1.0.0-preview.84 | 153 | 6/3/2021 |
1.0.0-preview.82 | 195 | 6/3/2021 |
1.0.0-preview.80 | 166 | 6/2/2021 |
1.0.0-preview.78 | 191 | 6/1/2021 |
1.0.0-preview.76 | 205 | 5/31/2021 |
1.0.0-preview.74 | 200 | 5/31/2021 |
1.0.0-preview.72 | 193 | 5/31/2021 |
1.0.0-preview.70 | 215 | 5/30/2021 |
1.0.0-preview.68 | 191 | 5/30/2021 |
1.0.0-preview.66 | 163 | 5/29/2021 |
1.0.0-preview.64 | 167 | 5/27/2021 |
1.0.0-preview.62 | 163 | 5/26/2021 |
1.0.0-preview.60 | 310 | 5/25/2021 |
1.0.0-preview.58 | 289 | 5/23/2021 |
1.0.0-preview.56 | 181 | 5/22/2021 |
1.0.0-preview.54 | 215 | 5/21/2021 |
1.0.0-preview.52 | 220 | 5/20/2021 |
1.0.0-preview.50 | 159 | 5/19/2021 |
1.0.0-preview.48 | 192 | 5/19/2021 |
1.0.0-preview.46 | 276 | 5/16/2021 |
1.0.0-preview.44 | 166 | 5/15/2021 |
1.0.0-preview.42 | 157 | 5/14/2021 |
1.0.0-preview.40 | 170 | 5/13/2021 |
1.0.0-preview.38 | 155 | 5/12/2021 |
1.0.0-preview.36 | 230 | 5/7/2021 |
1.0.0-preview.34 | 219 | 5/6/2021 |
1.0.0-preview.32 | 157 | 5/5/2021 |
1.0.0-preview.30 | 173 | 5/4/2021 |
1.0.0-preview.28 | 162 | 5/4/2021 |
1.0.0-preview.26 | 176 | 4/23/2021 |
1.0.0-preview.25 | 158 | 4/12/2021 |
1.0.0-preview.24 | 175 | 4/10/2021 |
1.0.0-preview.21 | 678 | 4/9/2021 |
1.0.0-preview.19 | 182 | 4/9/2021 |
1.0.0-preview.17 | 191 | 4/8/2021 |
1.0.0-preview.14 | 182 | 4/8/2021 |
1.0.0-preview.13 | 188 | 4/8/2021 |