ModPosh.Logger 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ModPosh.Logger --version 1.0.1                
NuGet\Install-Package ModPosh.Logger -Version 1.0.1                
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="ModPosh.Logger" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ModPosh.Logger --version 1.0.1                
#r "nuget: ModPosh.Logger, 1.0.1"                
#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 ModPosh.Logger as a Cake Addin
#addin nuget:?package=ModPosh.Logger&version=1.0.1

// Install ModPosh.Logger as a Cake Tool
#tool nuget:?package=ModPosh.Logger&version=1.0.1                
Latest Version Nuget.org Issues License Discord
Latest Version Nuget.org GitHub issues GitHub license Discord Server

ModPosh.Logger

ModPosh.Logger is a versatile logging library for C# projects and PowerShell scripts/modules. It provides functionality for logging messages to the console or a file, supporting custom configuration and log rotation.

Code Documentation

Detailed documentation can be found in the docs folder.

Features

  • Log messages to the console or file.
  • Configure logging through a JSON configuration file or programmatically.
  • Automatic log file rotation based on file size.

Installation

For C# Projects

  1. Add the Library: Include the ModPosh.Logger library in your C# project. If you use a solution, you can compile it from the source, reference the DLL, or include the project directly.

For PowerShell Scripts/Modules

  1. **Load the Assembly: Ensure the compiled `ModPosh.Logger.dll`` is accessible to your PowerShell script or module. You will need to load this assembly in your scripts.

Usage

In C# Projects

  1. Initialization: Create an instance of the Logger class.

    This method creates a Logger instance using configuration settings from a specified file path.

    using ModPosh.Logger;
    
    // Assume you have a configuration file at the specified path
    string configFilePath = "path/to/config.json";
    
    // Create a Logger instance using the configuration file
    Logger loggerFromConfig = Factory.LoggerFactory.CreateLogger(configFilePath);
    
    // Use the logger
    loggerFromConfig.LogInformation("This is an informational message from the configured logger.");
    
    

    This method creates a Logger instance with default settings that log to the console.

    // Create a Logger instance that logs to the console
    Logger consoleLogger = Factory.LoggerFactory.CreateConsoleLogger();
    
    // Use the logger
    consoleLogger.LogInformation("This message will be logged to the console.");
    

    This method creates a Logger instance that logs messages to a specified file.

    // Specify the path where log messages will be written
    string logFilePath = "path/to/logfile.log";
    
    // Create a Logger instance that logs to the specified file
    Logger fileLogger = Factory.LoggerFactory.CreateFileLogger(logFilePath);
    
    // Use the logger
    fileLogger.LogInformation("This message will be logged to the file.");
    
  2. Logging Messages:

    logger.LogInformation("This is an informational message.");
    logger.LogWarning("This is a warning message.");
    logger.LogError("This is an error message.");
    

In PowerShell Scripts/Modules

  1. Load the Assembly:

    Add-Type -Path "path\to\ModPosh.Logger.dll"
    
  2. Create a Logger Instance:

    For default configuration:

    $logger = New-Object ModPosh.Logger.Implementations.Logger
    

    With a direct log file path:

    $logger = New-Object ModPosh.Logger.Implementations.Logger("path\to\logfile.log")
    
  3. Logging Messages:

    $logger.LogInformation("This is an informational message.")
    $logger.LogWarning("This is a warning message.")
    $logger.LogError("This is an error message.")
    

Configuration

To configure logging, use the appsettings.json file with the following format:

{
  "Logger": {
    "LogToFile": true,
    "LogToConsole": true,
    "LogFilePath": "path/to/logfile.log"
  }
}

Additional Notes

  • Ensure that the paths provided for log files are accessible and writable by the application or script.
  • In PowerShell, the path to the DLL and configuration files should use the full path or a path relative to the current PowerShell session's location.
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

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 119 8/26/2024
1.0.1 109 8/5/2024
1.0.0 214 1/5/2024

Version 1.0.1 of Logger  This should be a usable logger in both Cand PowerShell, which I've struggled with lately. It should be simple and allow logging to a file or the console if needed.  -DOCUMENTATION  - Add Documentation #5  -ENHANCEMENT  - Update Psake for use with CProjects #6 - Add Documentation #5 - Create Factory #4 - Add Testing Framework #3 - Add the ability to handle log rotation #2 - Add the ability to use a configuration file #1