Hexa.NET.Logging is a small, efficient logging framework for .NET applications. It provides a simple API for logging messages at various levels (e.g., error, warning, information). This framework is designed to be lightweight and easy to integrate into your existing .NET applications.
- Lightweight and efficient logging.
- Easy-to-use API.
- Support for multiple logging levels.
- Ability to cache loggers for improved performance.
- Exception logging support.
- Conditional logging support.
- Async logging support.
To install Hexa.NET.Logging, add the following package to your project:
dotnet add package Hexa.NET.Logging
Usage
Here is a basic example of how to use Hexa.NET.Logging in your application:
- Getting a Logger:
using Hexa.NET.Logging;
// Add a log writer. (LogFileWriter is a build-in log writer.)
var logWriter = new LogFileWriter("logs");
LoggerFactory.AddGlobalWriter(logWriter);
// Retrieve a logger for a specific category
var logger = LoggerFactory.GetLogger(nameof(MyClass));
// Store the logger in a static field/property for better performance
private static readonly ILogger _logger = LoggerFactory.GetLogger(nameof(MyClass));
- Logging Messages:
// Log an error message
_logger.Error("Failed to create input layout, signature was null.");
// Log a warning message
_logger.Warning("This is a warning message.");
// Log an informational message
_logger.Info("This is an informational message.");
- Logging Exceptions:
try
{
// Your code here
}
catch (Exception ex)
{
_logger.Log(ex); // Log the exception
}
- Conditional Logging:
// Log a message only if a condition is met
_logger.WarnIf(foo == bar, "This is a warning message.");
- Example in a Class:
using Hexa.NET.Logging;
public class MyClass
{
private static readonly ILogger _logger = LoggerFactory.GetLogger(nameof(MyClass));
public void SomeMethod()
{
_logger.Info("This is an informational message.");
try
{
// Your code here
}
catch (Exception ex)
{
_logger.Log(ex); // Log the exception
}
}
}
Hexa.NET.Logging supports the following logging levels:
- Critical: Use for logging critical messages that indicate a critical failure in the application.
- Error: Use for logging error messages that indicate a failure in the application.
- Warning: Use for logging warning messages that indicate a potential issue or important information.
- Info: Use for logging informational messages that highlight the progress of the application.
- Debug: Use for logging debug messages that provide detailed information for debugging purposes.
- Trace: Use for logging trace messages that provide detailed information for tracing purposes.
- Cache Loggers: Store the ILogger instances in static fields or properties to avoid retrieving the logger multiple times, which can improve performance.
- Use Appropriate Logging Levels: Use the correct logging level to ensure that the log output is meaningful and easy to understand.
- Log Exceptions: Use the Log(Exception ex) method to log exceptions and capture stack trace information.
- Conditional Logging: Use conditional logging methods to log messages based on specific conditions.
Contributions are welcome! Please fork the repository and submit a pull request with your changes. Ensure that your code follows the project's coding standards and includes appropriate tests.
Hexa.NET.Logging is licensed under the MIT License. See the LICENSE file for more details.