forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
108 lines (99 loc) · 4.88 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using Microsoft.Extensions.Logging;
using nanoFramework.Logging;
using nanoFramework.Logging.Debug;
using nanoFramework.Logging.Serial;
using nanoFramework.Logging.Stream;
using System;
using System.Diagnostics;
using System.Threading;
using System.IO.Ports;
#if BUIID_FOR_ESP32
using nanoFramework.Hardware.Esp32;
#endif
namespace Logging
{
public class Program
{
private static DebugLogger _logger;
public static void Main()
{
_logger = new DebugLogger("Example");
_logger.MinLogLevel = LogLevel.Trace;
_logger.LogInformation("Hello from nanoFramework!");
_logger.LogTrace("Trace: the Debug Logger is initialized");
_logger.LogInformation($"Logger name is: {_logger.LoggerName}, you can use that to trace which component is used");
_logger.LogInformation("The next call to the class will log as well");
_logger.LogInformation("For this component, we're using the Logger Factory pattern. It will use the debugger as well");
LogDispatcher.LoggerFactory = new DebugLoggerFactory();
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();
_logger.LogInformation("Your responsibility is to make sure you set the right level as well as formatting the strings");
_logger.LogInformation("More examples below. All will display as the log level is Trace.");
_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 });
_logger.LogInformation("Now we will adjust the level to Critical, only the Critical message will appear");
_logger.MinLogLevel = LogLevel.Critical;
_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 });
// Now you can as well use the Stream and the Serial logger
// Uncomment the following lines to use one or the other
// SerialLogger();
}
private static void SerialLogger()
{
try
{
#if BUIID_FOR_ESP32
////////////////////////////////////////////////////////////////////////////////////////////////////
// COM2 in ESP32-WROVER-KIT mapped to free GPIO pins
// mind to NOT USE pins shared with other devices, like serial flash and PSRAM
// also it's MANDATORY to set pin funcion to the appropriate COM before instanciating it
// set GPIO functions for COM2 (this is UART1 on ESP32)
Configuration.SetPinFunction(Gpio.IO04, DeviceFunction.COM2_TX);
Configuration.SetPinFunction(Gpio.IO05, DeviceFunction.COM2_RX);
// open COM2
LogDispatcher.LoggerFactory = new SerialLoggerFactory("COM2");
#else
///////////////////////////////////////////////////////////////////////////////////////////////////
// COM6 in STM32F769IDiscovery board (Tx, Rx pins exposed in Arduino header CN13: TX->D1, RX->D0)
// open COM6
LogDispatcher.LoggerFactory = new SerialLoggerFactory("COM6");
#endif
// Then you can use the serial logger as expected
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();
}
finally
{
LogDispatcher.LoggerFactory = null;
}
}
private static void StreamLogger()
{
const string logFilePath = "C:\\logFile.txt";
try
{
LogDispatcher.LoggerFactory = new StreamLoggerFactory(logFilePath);
// This will log into the file
MyTestComponent test = new MyTestComponent();
test.DoSomeLogging();
}
finally
{
LogDispatcher.LoggerFactory = null;
}
}
}
}