Toms.Logger 1.1.0

dotnet add package Toms.Logger --version 1.1.0                
NuGet\Install-Package Toms.Logger -Version 1.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="Toms.Logger" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toms.Logger --version 1.1.0                
#r "nuget: Toms.Logger, 1.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.
// Install Toms.Logger as a Cake Addin
#addin nuget:?package=Toms.Logger&version=1.1.0

// Install Toms.Logger as a Cake Tool
#tool nuget:?package=Toms.Logger&version=1.1.0                

Toms Logger

This is a simple logging library that allows you to log messages with different levels of severity. It also supports logging method calls and exits, and saving logs to a file.

Installation

You can install the Toms Logger library via NuGet:

PM> Install-Package Toms.Logger

Usage

Logger Class

The Logger class is the main class of the library. You can create an instance of it and use its methods to log messages.

var logger = new Logger();

The Logger class has methods for logging messages at different levels of severity:

  • Debug: Logs a debug message.
  • Info: Logs an informational message.
  • Warning: Logs a warning message.
  • Error: Logs an error message.
logger.Debug("This is a debug message");
logger.Info("This is an informational message");
logger.Warning("This is a warning message");
logger.Error("This is an error message");

You can also save the logs to a file:

logger.SaveToFile("log.txt");

LogCall and LogExit Attributes

The LogCall and LogExit attributes can be used to log method calls and exits. They are always Debug.

[LogCall]
[LogExit]
public void MyMethodWithAttr() {
    logger.Info("Inside the method");
}

This will log a message both when the method is called and when it exits.

Configuration

The LogConfig class is used to configure the logger. You can set the display level, whether to use full class names, and whether to save logs to a file.

var conf = LogConfigBuilder.Default
    .SetDisplayLevel(LogLevel.Debug)
    .SetUseFullClassName(false)
    .SaveToFile(false)
    .Build();

Example

Here's an example of how to use the logger:

class MyClass
{
    Logger Logger {get;}

    public MyClass(Logger logger)
    {
        Logger = logger;
    }

    public void MyMethod() {
        Logger.Debug("First message");
        Logger.Info("2nd text");
        Logger.Warning("This is a warning message");
        Logger.Error("Error message");
    }

    [LogCall]
    [LogExit]
    public void MyMethodWithAttr() {
        Logger.Info("Inside the method");
    }
}

var conf = LogConfigBuilder.Default
    .SetDisplayLevel(LogLevel.Debug)
    .SetUseFullClassName(false)
    .SaveToFile(false)
    .Build();
var l = new Logger(conf);
var myInstance = new MyClass(l);
myInstance.MyMethod();
myInstance.MyMethodWithAttr();

This will log the messages from MyMethod and the calls and exits of MyMethodWithAttr.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
1.1.0 138 8/23/2024
1.0.0 119 4/21/2024