A simple and easy-to-use Elasticsearch logger designed for .NET Framework applications. Supports multiple configuration methods (direct parameters, JSON files, .config files) and index strategies (fixed index, date-based rolling index), with comprehensive fault tolerance and error handling to ensure no log loss. Suitable for WinForm, WPF, WebForm, Windows services, console applications, and AutoCAD secondary development scenarios.
$ dotnet add package EasyElasticLogger.NETFramework<div align="center">🚀🚀🚀 A simple and easy-to-use Elasticsearch logger designed for .NET Framework applications
Simple to use | Feature-rich | Production-ready
</div>| Feature Category | Function Description |
|---|---|
| ✅ Flexible Configuration | Supports multiple configuration methods: direct parameters, JSON file, .config file |
| ✅ Index Strategy | Multiple index strategies: fixed index, date-based rolling index, compliant with ELK best practices |
| ✅ Cluster Support | Supports both single-node and cluster modes, with robust connection management |
| ✅ Log Levels | Rich log levels: Debug, Info, Warn, Error, Fatal |
| ✅ Fault Tolerance | Comprehensive fault tolerance and error handling to ensure no log loss |
| ✅ Performance Optimization | Flexible API design, supports both synchronous and asynchronous logging |
| ✅ Multi-environment Support | Supports multi-environment configurations (development, testing, production) |
| ✅ Strong Compatibility | Compatible with AutoCAD secondary development, supports custom configuration file paths |
| ✅ Easy to Use | Designed as a static class, making it simple and intuitive to use |
📢📢 Important Note: This plugin is only applicable to .NET Framework 4.7.2 and 4.8 version applications. The Elasticsearch version is compatible with Elasticsearch 7.x
Install via NuGet Package Manager:
Install-Package EasyElasticLogger.NETFramework
Or install via .NET CLI:
dotnet add package EasyElasticLogger.NETFramework
using EasyElasticLogger.NETFramework.ES.Logging;
// Initialize at application startup (e.g., in Program.cs or Startup.cs)
ElasticLogger.Initialize("Production");
// Use static methods anywhere you need to log
// Synchronous logging
ElasticLogger.Info("This is an info log");
ElasticLogger.Warn("This is a warning log");
ElasticLogger.Error("This is an error log", new Exception("Test exception"));
// Asynchronous logging
await ElasticLogger.InfoAsync("This is an async info log");
await ElasticLogger.ErrorAsync("This is an async error log", new Exception("Test exception"));
// Set environment variable
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
// Or set ENVIRONMENT environment variable
Environment.SetEnvironmentVariable("ENVIRONMENT", "Test");
// Initialize with environment-specific configuration
ElasticLogger.Initialize();
// Directly specify the environment
ElasticLogger.Initialize("Production");
Configuration loading priority: Direct parameters > JSON configuration > .config configuration
var config = new LoggerConfiguration
{
Enabled = true,
IndexPrefix = "myapp-log",
IndexStrategy = IndexStrategy.DailyRolling,
ApplicationName = "MyApplication",
BatchSize = 100,
TimeoutMs = 10000,
Cluster = new ClusterConfiguration
{
Nodes = new List<string> { "http://localhost:9200" },
Username = "elastic",
Password = "password",
UseSsl = false,
SkipSslVerification = false,
ConnectionTimeoutMs = 5000,
MaxRetries = 2
}
};
ElasticLogger.Initialize(config);
// Automatically looks for logger-config.Production.json or logger-config.json
ElasticLogger.InitializeFromJson("logger-config.json", "Production");
Default Configuration (logger-config.json)
{
"Enabled": true,
"IndexPrefix": "myapp-log",
"IndexStrategy": "DailyRolling",
"ApplicationName": "MyApplication",
"BatchSize": 100,
"TimeoutMs": 10000,
"Cluster": {
"Nodes": ["http://localhost:9200"],
"Username": "elastic",
"Password": "password",
"UseSsl": false,
"SkipSslVerification": false,
"ConnectionTimeoutMs": 5000,
"MaxRetries": 2
}
}
Production Environment Configuration (logger-config.Production.json)
{
"Enabled": true,
"IndexPrefix": "myapp-log-prod",
"IndexStrategy": "DailyRolling",
"ApplicationName": "MyProductionApp",
"BatchSize": 100,
"TimeoutMs": 10000,
"Cluster": {
"Nodes": ["http://prod-es1:9200", "http://prod-es2:9200", "http://prod-es3:9200"],
"Username": "elastic",
"Password": "prod_password",
"UseSsl": true,
"SkipSslVerification": false,
"ConnectionTimeoutMs": 10000,
"MaxRetries": 3
}
}
Usage Example
// Use default JSON configuration file
ElasticLogger.InitializeFromJson("logger-config.json");
// Use specified environment JSON configuration file
ElasticLogger.InitializeFromJson("logger-config.json", "Production");
<configuration>
<appSettings>
<add key="Environment" value="Development" />
</appSettings>
<configSections>
<!-- Default Configuration -->
<section name="elasticCluster" type="EasyElasticLogger.NETFramework.ES.Configuration.ElasticClusterConfigSection, EasyElasticLogger.NETFramework" />
<section name="elasticLogger" type="EasyElasticLogger.NETFramework.ES.Configuration.ElasticLoggerConfigSection, EasyElasticLogger.NETFramework" />
<!-- Environment-Specific Configuration -->
<section name="elasticCluster.Development" type="EasyElasticLogger.NETFramework.ES.Configuration.ElasticClusterConfigSection, EasyElasticLogger.NETFramework" />
<section name="elasticLogger.Development" type="EasyElasticLogger.NETFramework.ES.Configuration.ElasticLoggerConfigSection, EasyElasticLogger.NETFramework" />
</configSections>
<!-- Default Configuration -->
<elasticCluster username="elastic" password="dev_password" nodes="http://localhost:9200" />
<elasticLogger indexPrefix="myapp-log" enabled="true" />
<!-- Development Environment Configuration -->
<elasticCluster.Development username="elastic" password="dev_password" nodes="http://localhost:9200" />
<elasticLogger.Development indexPrefix="myapp-log-dev" enabled="true" />
</configuration>
// Use custom configuration file path and environment for initialization
ElasticLogger.Initialize(@"C:\MyApp\config\myapp.config", "Production");
// Debug level log
ElasticLogger.Debug("Debug information", "MyClass.Method");
// Info level log
ElasticLogger.Info("General information", "MyClass.Method");
// Warn level log
ElasticLogger.Warn("Warning information", "MyClass.Method");
// Error level log
ElasticLogger.Error("Error information", new Exception("Exception details"), "MyClass.Method");
// Fatal level log
ElasticLogger.Fatal("Critical error", new Exception("Exception details"), "MyClass.Method");
// Log with additional data
var userData = new { UserId = 123, UserName = "Zhang San" };
ElasticLogger.Info("User login", "UserService.Login", userData);
// Asynchronously log different levels
await ElasticLogger.DebugAsync("Debug information", "MyClass.Method");
await ElasticLogger.InfoAsync("General information", "MyClass.Method");
await ElasticLogger.WarnAsync("Warning information", "MyClass.Method");
await ElasticLogger.ErrorAsync("Error information", new Exception("Exception details"), "MyClass.Method");
await ElasticLogger.FatalAsync("Critical error", new Exception("Exception details"), "MyClass.Method");
| Property | Type | Default Value | Description |
|---|---|---|---|
Enabled | bool | true | Whether to enable the logging feature |
IndexPrefix | string | "app-log" | Index prefix |
IndexStrategy | IndexStrategy | DailyRolling | Index strategy (Fixed/DailyRolling) |
FixedIndexName | string | null | Fixed index name (only used when IndexStrategy is Fixed) |
ApplicationName | string | "DefaultApp" | Application name |
BatchSize | int | 100 | Batch size for sending logs |
TimeoutMs | int | 10000 | Sending timeout in milliseconds |
| Property | Type | Default Value | Description |
|---|---|---|---|
Nodes | List<string> | empty | List of Elasticsearch node addresses |
Username | string | null | Username (for Basic authentication) |
Password | string | null | Password (for Basic authentication) |
UseSsl | bool | false | Whether to enable SSL |
SkipSslVerification | bool | false | Whether to skip SSL certificate verification |
ConnectionTimeoutMs | int | 5000 | Connection timeout in milliseconds |
MaxRetries | int | 2 | Maximum number of retries |
| Strategy | Description |
|---|---|
| Fixed | Fixed index, all logs are written to the same index |
| DailyRolling | Date-based rolling index, a new index is generated each day (default) |
ASPNETCORE_ENVIRONMENTENVIRONMENTEnvironmentThis project is licensed under the MIT License - see the LICENSE file for details.
Make logging simple and powerful ✨✨