SmartLogging 2.1.0
See the version list below for details.
dotnet add package SmartLogging --version 2.1.0
NuGet\Install-Package SmartLogging -Version 2.1.0
<PackageReference Include="SmartLogging" Version="2.1.0" />
<PackageVersion Include="SmartLogging" Version="2.1.0" />
<PackageReference Include="SmartLogging" />
paket add SmartLogging --version 2.1.0
#r "nuget: SmartLogging, 2.1.0"
#:package SmartLogging@2.1.0
#addin nuget:?package=SmartLogging&version=2.1.0
#tool nuget:?package=SmartLogging&version=2.1.0
SmartLogging
Easy to use .NET file based logging framework using JSON.
Overview
The SmartLogging package exports two main classes: SmartLogger and LogWriter.
SmartLogger
SmartLogger is used to create log entries with certain log levels.
A log entry contains the following information:
- creation time of the entry
- thread id of the calling thread
- log level (a value between 0 and 6)
- log context (usually the name of the calling class)
- log method (usually the name of the calling method)
- log message (a simple string or the JSON representation of an object)
LogWriter
LogWriter is a static class, so there is only one instance per application. LogWriter lets you specify the log file, its maximum allowed size and the minimimum log level to be processed. In most cases you will be happy with the default settings of LogWriter, so you won't have to deal with this class except for one method:
LogWriter.Exit()
The LogWriter cashes log entries before they are written to disk. So if your
application terminates unexpectedly there might be a few entries in the cache
which you will not see in the log file. To avoid this call LogWriter.Exit()
when your application is about to stop.
LogLevel
The enum LogLevel defines 7 levels:
Verbose (0): Logs that contain the most detailed messages. These messages may contain sensitive application data. These messages are disabled by default and should never be enabled in a production environment.
Debug (1): Logs that are used for interactive investigation during development. These logs should primarily contain information useful for debugging and have no long-term value.
Information (2): Logs that track the general flow of the application. These logs should have long-term value.
Warning (3): Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop.
Error (4): Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure.
Fatal (5): Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention.
None (6): Logs with this priority will be written to disk in any case. This level should not be used in production code. It is but being used when the LogWriter starts working and logs the first message "Start logging".
Usage
You will use a single SmartLogger in each class which has to create log entries.
You can declare this logger as static so every instance of this class will share the same logger. Since there is only one LogWriter per application this is fine. So in your class you should declare something like this:
private static readonly SmartLogger Log = new();
And when you want to create a log entry inside a method you will call:
Log.Information("this message is for you");
The resulting log entry looks like this:
{"Time":"2025-07-10T08:55:55.3391580Z","ThreadId":1,"Level":"Information", "Context":"TestApp.Program","Method":"Main","Message":"this message is for you"}
The log context is extracted from the declaration of the logger and the method name is extracted from the actual logging statement.
Even if you call any of the SmartLogger's methods without parameter, e.g.
Log.Warning()
you will have a log entry which shows the time, the class name and the method name.
Logging Objects
The nice thing about SmartLogger is that it not only logs simple strings but also objects. If you do a call like this:
Log.Information(DateTime.Now);
then the JSON-serialized DateTime object will appear as message in your log entry:
{"Time":"2025-07-09T12:49:18.9541781Z","ThreadId":1,"Level":"Information", "Context":"TestApp.Program","Method":"Main", "Message":"\"2025-07-09T14:49:18.9412736+02:00\""}
This is especially usefull when logging parameters of a method. Consider the following method:
void DoSomething(string name, double age)
If a logger can only log simple strings you would have to do this:
Log.Information($"name={name},age={age}");
With SmartLogger you can simply do this:
Log.Information(new {name, age})
You will get this log entry:
{"Time":"2025-07-09T13:23:22.9126806Z", "ThreadId":1,"Level":"Information","Context":"TestApp.Program", "Method":"DoSomething","Message":"{\"name\":\"asd\",\"age\":123}"}
LogWriter Details
If you don't explicitely call the (one and only) LogWriter's Init() method, your log entries will be written to the current user's temporary directory into a file called "name of your application.log", the maximum file size will be 16 MB and the minimum log level will be Information.
If you want to change the default behaviour, you can call
LogWriter.Init(string fileName, long maxSize)
and set the minimum log level with e.g.
LogWriter.MinimumLogLevel = LogLevel.Warning
Note that the lowest accepted maxSize is 64 kB and the highest is 64 MB.
If the log file exceeds the maximum size the current log file is copied to a file called "MyApp.log.log" if the original file is called "MyApp.log" and the file "MyApp.log" is cleared.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Safer handling of invalid log file names.