SmartLogging 2.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SmartLogging --version 2.1.0
                    
NuGet\Install-Package SmartLogging -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SmartLogging" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SmartLogging" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SmartLogging" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SmartLogging --version 2.1.0
                    
#r "nuget: SmartLogging, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SmartLogging@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SmartLogging&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=SmartLogging&version=2.1.0
                    
Install as a Cake Tool

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:

  1. creation time of the entry
  2. thread id of the calling thread
  3. log level (a value between 0 and 6)
  4. log context (usually the name of the calling class)
  5. log method (usually the name of the calling method)
  6. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.4.0 146 7/14/2025
2.3.2 139 7/14/2025
2.3.1 134 7/13/2025
2.3.0 101 7/12/2025
2.2.0 64 7/12/2025
2.1.0 110 7/11/2025
2.0.0 138 7/10/2025

Safer handling of invalid log file names.