Latest Version | Nuget.org | Issues | License | Discord |
---|---|---|---|---|
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.
Detailed documentation can be found in the docs folder.
- Log messages to the console or file.
- Configure logging through a JSON configuration file or programmatically.
- Automatic log file rotation based on file size.
- 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.
- **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.
-
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.");
-
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.")
To configure logging, use the appsettings.json
file with the following format:
{
"Logger": {
"LogToFile": true,
"LogToConsole": true,
"LogFilePath": "path/to/logfile.log"
}
}
- 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.