ModPosh.Logger
1.1.0
dotnet add package ModPosh.Logger --version 1.1.0
NuGet\Install-Package ModPosh.Logger -Version 1.1.0
<PackageReference Include="ModPosh.Logger" Version="1.1.0" />
paket add ModPosh.Logger --version 1.1.0
#r "nuget: ModPosh.Logger, 1.1.0"
// Install ModPosh.Logger as a Cake Addin #addin nuget:?package=ModPosh.Logger&version=1.1.0 // Install ModPosh.Logger as a Cake Tool #tool nuget:?package=ModPosh.Logger&version=1.1.0
Latest Version | Nuget.org | Issues | License | Discord |
---|---|---|---|---|
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
- 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
- **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
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.");
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
Load the Assembly:
Add-Type -Path "path\to\ModPosh.Logger.dll"
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")
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 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. |
-
net6.0
- No dependencies.
-
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 1.0.0.0 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