LogHelper (VB.NET)
A comprehensive logging utility class that provides structured logging capabilities with multiple output targets and log levels.
Features
- Multiple log levels (Debug, Info, Warning, Error, Fatal)
- File logging
- Console logging
- Database logging
- Log rotation
- Log filtering
- Structured logging
- Performance logging
- Log aggregation
Installation
Add the LogHelper.vb
file to your project.
Imports JVR.Helpers
Usage Examples
Basic Logging
Dim logger As New LogHelper()
' Log messages at different levels
logger.Debug("Debug message")
logger.Info("Information message")
logger.Warning("Warning message")
logger.Error("Error message")
logger.Fatal("Fatal error message")
Structured Logging
Dim logger As New LogHelper()
' Log with context
logger.LogWithContext("User login", New With {
.UserId = "123",
.Username = "john.doe",
.LoginTime = DateTime.Now
})
' Log with custom fields
Dim fields As New Dictionary(Of String, Object) From {
{"Amount", 100.5},
{"Currency", "USD"},
{"TransactionId", "TX123"}
}
logger.LogWithFields("Payment processed", fields)
Performance Logging
Dim logger As New LogHelper()
Using logger.MeasureOperation("Database Query")
' Perform database operation
' Duration will be automatically logged
End Using
' Log with custom timer
logger.LogPerformance("API Call", stopwatch.Elapsed)
API Reference
Basic Logging Methods
Debug(message As String)
: Logs debug messageInfo(message As String)
: Logs information messageWarning(message As String)
: Logs warning messageError(message As String)
: Logs error messageFatal(message As String)
: Logs fatal error message
Structured Logging
LogWithContext(message As String, context As Object)
: Logs with context objectLogWithFields(message As String, fields As Dictionary(Of String, Object))
: Logs with custom fieldsLogException(ex As Exception, Optional message As String = Nothing)
: Logs exception details
Performance Logging
MeasureOperation(operationName As String) As IDisposable
: Creates performance measurement scopeLogPerformance(operation As String, duration As TimeSpan)
: Logs operation durationStartMeasurement(operation As String)
: Starts performance measurementStopMeasurement(operation As String)
: Stops and logs measurement
Configuration
SetLogLevel(level As LogLevel)
: Sets minimum log levelSetLogFile(path As String)
: Sets log file pathEnableConsoleLogging(enable As Boolean)
: Enables/disables console outputConfigureRotation(maxFiles As Integer, maxSize As Long)
: Configures log rotation
Advanced Features
CreateScope(scopeName As String) As IDisposable
: Creates logging scopeAddLogTarget(target As ILogTarget)
: Adds custom log targetFilterLogs(level As LogLevel, start As DateTime, [end] As DateTime)
: Filters logsArchiveLogs(archivePath As String)
: Archives old logs
Log Levels
- Debug: Detailed information for debugging
- Info: General information about application flow
- Warning: Potentially harmful situations
- Error: Error events that might still allow application to continue
- Fatal: Very severe error events that may lead to application termination
Dependencies
- .NET 6.0 or higher
- System.IO
- System.Diagnostics
- Serilog (optional for advanced logging)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contact
- Website: jvrsoftware.co.za
- Email: jvrsoftware@gmail.com or jan@jvrsoftware.co.za
Version History
- 2.0.1: Added performance logging
- 2.0.0: Added structured logging support
- 1.0.0: Initial release with basic logging capabilities
Legal Disclaimer
This documentation and associated helper scripts are provided "as is" without warranty of any kind, either express or implied.
- The code examples and helper functions are for illustrative purposes only.
- Users should thoroughly test any implementation in their specific environment.
- The authors are not responsible for any issues or damages arising from the use of these scripts.
- Always follow security best practices and your organization's coding guidelines.
