|
| 1 | +using nanoFramework.TestFramework; |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using nanoFramework.Logging; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using nanoFramework.Logging.Debug; |
| 7 | + |
| 8 | +namespace UnitTestDebugLogging |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + class FormattingTest |
| 12 | + { |
| 13 | + static DebugLogger _logger; |
| 14 | + |
| 15 | + [Setup] |
| 16 | + public void SteupFormattingTest() |
| 17 | + { |
| 18 | + _logger = new DebugLogger(nameof(FormattingTest)); |
| 19 | + LoggerExtensions.MessageFormatter = typeof(MyFormatter).GetType().GetMethod("MessageFormatterStatic"); |
| 20 | + Debug.WriteLine($"{LoggerExtensions.MessageFormatter.Name}"); |
| 21 | + _logger.MinLogLevel = LogLevel.Trace; |
| 22 | + } |
| 23 | + |
| 24 | + [TestMethod] |
| 25 | + public void TestInvoke() |
| 26 | + { |
| 27 | + string msg = (string)LoggerExtensions.MessageFormatter.Invoke(null, new object[] { "test", LogLevel.Trace, new EventId(0), "some text", null }); |
| 28 | + Debug.WriteLine(msg); |
| 29 | + } |
| 30 | + |
| 31 | + [TestMethod] |
| 32 | + public void TestFormatting() |
| 33 | + { |
| 34 | + LogAll(); |
| 35 | + } |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void TestFormattingSimple() |
| 39 | + { |
| 40 | + LoggerExtensions.MessageFormatter = typeof(MyFormatter).GetType().GetMethod("MessageFormatterSimple"); |
| 41 | + LogAll(); |
| 42 | + } |
| 43 | + |
| 44 | + private void LogAll() |
| 45 | + { |
| 46 | + Debug.WriteLine($"Expexcted level: {_logger.MinLogLevel}"); |
| 47 | + _logger.LogTrace("{0} {1}", new object[] { "param 1", 42 }); |
| 48 | + _logger.LogDebug("{0} {1}", new object[] { "param 1", 42 }); |
| 49 | + _logger.LogInformation("Just some information and nothing else"); |
| 50 | + _logger.LogWarning("{0} {1}", new object[] { "param 1", 42 }); |
| 51 | + _logger.LogError(new Exception("Big problem"), "{0} {1}", new object[] { "param 1", 42 }); |
| 52 | + _logger.LogCritical(42, new Exception("Insane problem"), "{0} {1}", new object[] { "param 1", 42 }); |
| 53 | + } |
| 54 | + |
| 55 | + [Cleanup] |
| 56 | + public void CleanupFormattingTest() |
| 57 | + { |
| 58 | + LoggerExtensions.MessageFormatter = null; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public class MyFormatter : IMessageFormatter |
| 63 | + { |
| 64 | + public string MessageFormatter(string className, LogLevel logLevel, EventId eventId, string state, Exception exception) |
| 65 | + => MessageFormatterStatic(className, logLevel, eventId, state, exception); |
| 66 | + |
| 67 | + public string MessageFormatterStatic(string className, LogLevel logLevel, EventId eventId, string state, Exception exception) |
| 68 | + { |
| 69 | + string logstr = string.Empty; |
| 70 | + switch (logLevel) |
| 71 | + { |
| 72 | + case LogLevel.Trace: |
| 73 | + logstr = "TRACE: "; |
| 74 | + break; |
| 75 | + case LogLevel.Debug: |
| 76 | + logstr = "I love debug: "; |
| 77 | + break; |
| 78 | + case LogLevel.Warning: |
| 79 | + logstr = "WARNING: "; |
| 80 | + break; |
| 81 | + case LogLevel.Error: |
| 82 | + logstr = "ERROR: "; |
| 83 | + break; |
| 84 | + case LogLevel.Critical: |
| 85 | + logstr = "CRITICAL:"; |
| 86 | + break; |
| 87 | + case LogLevel.None: |
| 88 | + case LogLevel.Information: |
| 89 | + default: |
| 90 | + break; |
| 91 | + } |
| 92 | + |
| 93 | + string eventstr = eventId.Id != 0 ? $" Event ID: {eventId}, " : string.Empty; |
| 94 | + string msg = $"[{className}] {eventstr}{logstr} {state}"; |
| 95 | + if (exception != null) |
| 96 | + { |
| 97 | + msg += $" {exception}"; |
| 98 | + } |
| 99 | + |
| 100 | + return msg; |
| 101 | + } |
| 102 | + |
| 103 | + public string MessageFormatterSimple(string className, LogLevel logLevel, EventId eventId, string state, Exception exception) |
| 104 | + { |
| 105 | + string msg = $"[{className}] {logLevel}-{state}"; |
| 106 | + if (exception != null) |
| 107 | + { |
| 108 | + msg += $" {exception}"; |
| 109 | + } |
| 110 | + |
| 111 | + return msg; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments