|
1 | | -# LoggerLite [](https://choosealicense.com/licenses/mit/) |
| 1 | +# LoggerLite |
2 | 2 |
|
3 | | -| | Integrations | |
| 3 | +[](https://github.com/PFalkowski/LoggerLite/actions/workflows/ci.yml) |
| 4 | +[](https://www.nuget.org/packages/LoggerLite) |
| 5 | +[](https://www.nuget.org/packages/LoggerLite) |
| 6 | +[](License.txt) |
| 7 | + |
| 8 | +Lightweight, dependency-free logging for .NET — a thin, easy-to-read wrapper around `Console`, |
| 9 | +file streaming, `XDocument` and friends, behind a single `ILoggerLite` interface. Targets |
| 10 | +`netstandard2.0` and `net8.0`. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```bash |
| 15 | +dotnet add package LoggerLite |
| 16 | +``` |
| 17 | + |
| 18 | +## Why? |
| 19 | + |
| 20 | +Lots of everyday code is just "write this somewhere." Full-featured logging frameworks are |
| 21 | +powerful but can be heavy, opaque, or awkward to target portably. LoggerLite is the opposite: |
| 22 | +one small interface, a handful of implementations, **no external dependencies**, and a test |
| 23 | +suite covering the behavior. If you want something you can read in one sitting and drop into |
| 24 | +any project, this is it. |
| 25 | + |
| 26 | +## Loggers |
| 27 | + |
| 28 | +| Logger | Output | |
4 | 29 | | --- | --- | |
5 | | -| **Nuget** | [](https://www.nuget.org/packages/LoggerLite) | |
6 | | -| **Build** | [](https://piotrfalkowski.visualstudio.com/LoggerLite/_build/latest?definitionId=3) | |
7 | | -| **Coverage** | [](https://codecov.io/gh/PFalkowski/LoggerLite) | |
8 | | - |
9 | | -# Why? |
10 | | -Many programming tasks are reducible or somehow related to logging information. Tracing all the different implementations or handling concurency issues can be a nuisance. While there are many great, full-featured solutions, they are either not portable, bloated or hard to grasp. If you need lightweight, extensible and easy to understand logging solution, this is a library for you. Unit tests cover most of the codeline, there are no external dependencies and all relevant cade takes around 15 KB / 350 LOC. The LoggerLite is a .NET Core and .NET classic compatible solution, featuring one interface ILogger, handfull of implementations and a passive debouncer. The solution is a thin wrapper around .NET FileStreaming, XDocument, Console and other classes. Currently, the project contains following implementations: |
11 | | -- Console Logger |
12 | | -- Debug Trace Logger |
13 | | -- File Logger |
14 | | -- XML Logger |
15 | | -- YAML Logger |
16 | | -- JSON Logger |
17 | | -- HTML Logger |
18 | | - |
19 | | -# How? |
20 | | -The example of console logger: |
21 | | -```c# |
22 | | -using System; |
23 | | -using LoggerLite; |
| 30 | +| `ConsoleLogger` | colored console (per-severity colors) | |
| 31 | +| `DebugLogger` | `System.Diagnostics.Debug` trace | |
| 32 | +| `JsonFileLogger` | newline-delimited JSON file | |
| 33 | +| `YamlFileLogger` | YAML file | |
| 34 | +| `XLogger` | XML document | |
| 35 | +| `HtmlLogger` | HTML (XSLT-transformed) | |
| 36 | +| `AggregateLogger` | fans one message out to several loggers | |
| 37 | +| `QueuedLoggerWrapper` | buffers + debounces writes to a wrapped logger | |
| 38 | + |
| 39 | +All implement `ILoggerLite`: |
24 | 40 |
|
25 | | -namespace ConsoleApp1 |
| 41 | +```csharp |
| 42 | +public interface ILoggerLite |
26 | 43 | { |
27 | | - class Program |
28 | | - { |
29 | | - static void Main(string[] args) |
30 | | - { |
31 | | - var logger = new ConsoleLogger(); |
32 | | - logger.LogInfo("info!"); |
33 | | - logger.LogWarning("warning"); |
34 | | - logger.LogError("error :("); |
35 | | - Console.ReadKey(); |
36 | | - } |
37 | | - } |
| 44 | + void LogInfo(string message); |
| 45 | + void LogInformation(string message); // alias for LogInfo |
| 46 | + void LogSuccess(string message); |
| 47 | + void LogWarning(string warning); |
| 48 | + void LogError(string error); |
| 49 | + void LogError(Exception exception); |
| 50 | + void LogError(Exception exception, string description); |
| 51 | + void Log(string message, MessageSeverity severity); |
| 52 | + |
| 53 | + bool FlushAuto { get; } // true => writes immediately; false => call Save()/Flush() |
| 54 | + bool IsThreadSafe { get; } |
| 55 | + int Requests { get; } // total Log* calls |
| 56 | + int Successes { get; } // succeeded |
| 57 | + int Failures { get; } // threw internally (logging never throws to the caller) |
38 | 58 | } |
39 | 59 | ``` |
40 | | - |
41 | 60 |
|
42 | | -The example of yaml logger or any file logger based on FileLoggerBase: |
43 | | -```c# |
44 | | -using System; |
| 61 | +Logging never throws: failures are swallowed and counted in `Failures`. |
| 62 | + |
| 63 | +## Examples |
| 64 | + |
| 65 | +### Console |
| 66 | + |
| 67 | +```csharp |
45 | 68 | using LoggerLite; |
46 | 69 |
|
47 | | -namespace ConsoleApp1 |
48 | | -{ |
49 | | - class Program |
50 | | - { |
51 | | - static void Main(string[] args) |
52 | | - { |
53 | | - var logger = new YamlFileLogger("yamlLog.yaml"); |
54 | | - logger.LogInfo("info"); |
55 | | - logger.LogWarning("warning"); |
56 | | - logger.LogError("error");//no need to call save, it flushes automatically |
57 | | - Console.ReadKey(); |
58 | | - } |
59 | | - } |
60 | | -} |
| 70 | +var logger = new ConsoleLogger(); |
| 71 | +logger.LogInfo("info!"); |
| 72 | +logger.LogSuccess("done"); |
| 73 | +logger.LogWarning("warning"); |
| 74 | +logger.LogError("error :("); |
61 | 75 | ``` |
62 | 76 |
|
63 | | -The example of HTML logger: |
64 | | -```c# |
| 77 | + |
| 78 | + |
| 79 | +### File (JSON / YAML / …) |
| 80 | + |
| 81 | +`FileLoggerBase` subclasses flush automatically — no need to call `Save`: |
| 82 | + |
| 83 | +```csharp |
65 | 84 | using System; |
66 | 85 | using LoggerLite; |
67 | 86 |
|
68 | | -namespace ConsoleApp1 |
69 | | -{ |
70 | | - class Program |
71 | | - { |
72 | | - static void Main(string[] args) |
73 | | - { |
74 | | - var outputFile = new FileInfo(Path.ChangeExtension(Path.GetRandomFileName(), "html")); |
75 | | - var logger = new HtmlLogger(); |
76 | | - logger.LogWarning("warning"); |
77 | | - logger.LogWarning("warning"); |
78 | | - logger.LogWarning("warning"); |
79 | | - logger.LogWarning("warning"); |
80 | | - logger.LogInfo("info"); |
81 | | - logger.LogWarning("warning"); |
82 | | - logger.LogError("error, but not really:)"); |
83 | | - logger.Save(outputFile); |
84 | | - using (var process = Process.Start(new ProcessStartInfo |
85 | | - { FileName = outputFile.FullName, UseShellExecute = true })) |
86 | | - } |
87 | | - } |
88 | | -} |
| 87 | +var logger = new JsonFileLogger("log.json"); |
| 88 | +logger.LogInfo("info"); |
| 89 | +logger.LogError(new Exception("boom"), "while processing order 42"); |
89 | 90 | ``` |
90 | | - |
91 | 91 |
|
92 | | -Contributions are welcomed |
| 92 | +### HTML |
| 93 | + |
| 94 | +```csharp |
| 95 | +using System.Diagnostics; |
| 96 | +using System.IO; |
| 97 | +using LoggerLite; |
| 98 | + |
| 99 | +var outputFile = new FileInfo(Path.ChangeExtension(Path.GetRandomFileName(), "html")); |
| 100 | +var logger = new HtmlLogger(); |
| 101 | +logger.LogInfo("info"); |
| 102 | +logger.LogWarning("warning"); |
| 103 | +logger.LogError("error, but not really :)"); |
| 104 | +logger.Save(outputFile); |
| 105 | + |
| 106 | +Process.Start(new ProcessStartInfo { FileName = outputFile.FullName, UseShellExecute = true }); |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +### Aggregate — one call, many sinks |
| 112 | + |
| 113 | +```csharp |
| 114 | +using LoggerLite; |
| 115 | + |
| 116 | +var logger = new AggregateLogger(new ConsoleLogger(), new JsonFileLogger("log.json")); |
| 117 | +logger.LogInfo("goes to both the console and the file"); |
| 118 | +``` |
| 119 | + |
| 120 | +### Buffered / debounced writes |
| 121 | + |
| 122 | +Wrap any `FormattedLoggerBase` to batch writes through a debouncer: |
| 123 | + |
| 124 | +```csharp |
| 125 | +using LoggerLite; |
| 126 | + |
| 127 | +using var logger = new QueuedLoggerWrapper( |
| 128 | + new FileLoggerBase("log.log"), |
| 129 | + new PassiveDebouncer { DebounceMilliseconds = 250 }); |
| 130 | + |
| 131 | +logger.LogInfo("buffered"); |
| 132 | +logger.Flush(); // force pending writes |
| 133 | +``` |
| 134 | + |
| 135 | +## Notes |
| 136 | + |
| 137 | +- **Custom formatting** per logger via the `Formatter` property (`Func<string level, string message, string>`). |
| 138 | +- **Targets** `netstandard2.0` (broad reach) and `net8.0`; no external dependencies. |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +MIT — see [License.txt](https://github.com/PFalkowski/LoggerLite/blob/master/License.txt). Contributions welcome. |
0 commit comments