EasyElasticLogger.NETFramework
1.0.8
dotnet add package EasyElasticLogger.NETFramework --version 1.0.8
NuGet\Install-Package EasyElasticLogger.NETFramework -Version 1.0.8
<PackageReference Include="EasyElasticLogger.NETFramework" Version="1.0.8" />
<PackageVersion Include="EasyElasticLogger.NETFramework" Version="1.0.8" />
<PackageReference Include="EasyElasticLogger.NETFramework" />
paket add EasyElasticLogger.NETFramework --version 1.0.8
#r "nuget: EasyElasticLogger.NETFramework, 1.0.8"
#:package EasyElasticLogger.NETFramework@1.0.8
#addin nuget:?package=EasyElasticLogger.NETFramework&version=1.0.8
#tool nuget:?package=EasyElasticLogger.NETFramework&version=1.0.8
EasyElasticLogger.NETFramework
๐๐๐ A simple and easy-to-use Elasticsearch logger designed for .NET Framework applications
<div align="center">
Simple to use | Feature-rich | Production-ready
</div>
โจโจ Features
| 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 |
๐ฏ๐ฏ Applicable Scenarios
๐ข๐ข 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
๐๐ Systems that need to centralize logs to ELK for unified analysis
- Systems requiring real-time log monitoring and alerts
- Scenarios where you want to leverage Kibana for log visualization and analysis
๐ฅ๏ธ๐ฅ๏ธ Applications based on .NET Framework 4.7.2/4.8
- WinForm desktop applications (.NET Framework 4.7.2/4.8)
- WPF desktop applications (.NET Framework 4.7.2/4.8)
- WebForm traditional web applications (.NET Framework 4.7.2/4.8)
- Windows services and background task programs (.NET Framework 4.7.2/4.8)
- Console applications logging needs (.NET Framework 4.7.2/4.8)
๐ง๐ง Diagnostic and operational logging needs in special development environments
- AutoCAD secondary development plugin logging (based on .NET Framework)
- Revit and other BIM software plugin development (based on .NET Framework)
- Industrial software plugin diagnostics and operational monitoring
- Special environment deployments requiring custom configuration file paths
โโ Unsupported Scenarios
- .NET Core / .NET 5/6/7/8 applications
- .NET Standard class library projects
- .NET Framework versions below 4.7.2
- Cross-platform applications (Linux/macOS)
๐ฆ๐ฆ Installation
Install via NuGet Package Manager:
Install-Package EasyElasticLogger.NETFramework
```xml
Or install via .NET CLI:
```bash
dotnet add package EasyElasticLogger.NETFramework
```xml
---
## ๐๐๐ Quick Start
### 1. Basic Usage
```csharp
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"));
```xml
### 2. Environment Configuration
#### Using Environment Variables
```csharp
// Set environment variable
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
// Or set ENVIRONMENT environment variable
Environment.SetEnvironmentVariable("ENVIRONMENT", "Test");
// Initialize with environment-specific configuration
ElasticLogger.Initialize();
```xml
#### Using Initialization Method to Specify Environment
```csharp
// Directly specify the environment
ElasticLogger.Initialize("Production");
```xml
---
## โโโ๏ธ Configuration Methods
Configuration loading priority: **Direct parameters > JSON configuration > .config configuration**
### 1. ๐๐ Direct Parameter Configuration
```csharp
var config = new LoggerConfiguration
{
Enabled = true,
IndexPrefix = "myapp-log",
IndexStrategy = IndexStrategy.DailyRolling,
ApplicationName = "MyApplication",
BatchSize = 100,
TimeoutMs = 10000,
Cluster = new ClusterConfigurationElement
{
Nodes = new List<string> { "http://localhost:9200" },
Username = "elastic",
Password = "password",
UseSsl = false,
SkipSslVerification = false,
ConnectionTimeoutMs = 5000,
MaxRetries = 2
}
};
ElasticLogger.Initialize(config);
```xml
### 2. ๐๐ JSON Configuration File (Supports Environment-Specific Configuration)
```csharp
// Automatically looks for logger-config.Production.json or logger-config.json
ElasticLogger.InitializeFromJson("logger-config.json", "Production");
```xml
#### JSON Configuration File Example
**Default Configuration (logger-config.json)**
```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
}
}
```xml
**Production Environment Configuration (logger-config.Production.json)**
```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
}
}
```xml
**Usage Example**
```csharp
// Use default JSON configuration file
ElasticLogger.InitializeFromJson("logger-config.json");
// Use specified environment JSON configuration file
ElasticLogger.InitializeFromJson("logger-config.json", "Production");
```xml
### 3. โโโ๏ธ .config Configuration File (Supports Environment-Specific Configuration)
```xml
<configuration>
<appSettings>
<add key="Environment" value="Development" />
</appSettings>
<configSections>
<section name="elasticLogger" type="EasyElasticLogger.NETFramework.ES.Configuration.LoggerConfiguration, EasyElasticLogger.NETFramework" />
<section name="elasticLogger.Development" type="EasyElasticLogger.NETFramework.ES.Configuration.LoggerConfiguration, EasyElasticLogger.NETFramework" />
<section name="elasticLogger.Test" type="EasyElasticLogger.NETFramework.ES.Configuration.LoggerConfiguration, EasyElasticLogger.NETFramework" />
<section name="elasticLogger.Production" type="EasyElasticLogger.NETFramework.ES.Configuration.LoggerConfiguration, EasyElasticLogger.NETFramework" />
</configSections>
<elasticLogger enabled="true" indexPrefix="app-log" applicationName="MyApplication" batchSize="100" timeoutMs="10000" indexStrategy="DailyRolling">
<cluster nodes="http://localhost:9200" username="" password="" useSsl="false" skipSslVerification="false" connectionTimeoutMs="5000" maxRetries="2" />
</elasticLogger>
<elasticLogger.Development enabled="true" indexPrefix="dev-app-log" applicationName="MyApplication-Dev" batchSize="50" timeoutMs="15000" indexStrategy="DailyRolling">
<cluster nodes="http://localhost:9200" username="elastic" password="changeme" useSsl="false" skipSslVerification="true" connectionTimeoutMs="10000" maxRetries="3" />
</elasticLogger.Development>
<elasticLogger.Production enabled="true" indexPrefix="prod-app-log" applicationName="MyApplication-Prod" batchSize="200" timeoutMs="30000" indexStrategy="DailyRolling" fixedIndexName="my-fixed-index">
<cluster nodes="https://es1.prod.com,https://es2.prod.com,https://es3.prod.com" username="prod-user" password="prod-password" useSsl="true" skipSslVerification="false" connectionTimeoutMs="15000" maxRetries="5" />
</elasticLogger.Production>
</configuration>
```xml
### 4. ๐ฅ๐ฅ๐ฅ๏ธ Custom Configuration File Path (Compatible with AutoCAD Secondary Development)
```csharp
// Use custom configuration file path and environment for initialization
ElasticLogger.Initialize(@"C:\\MyApp\\config\\myapp.config", "Production");
๐๐ Logging Examples
Synchronous Logging
``csharp // 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);
### Asynchronous Logging
``csharp
// 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");
๐ง๐ง Configuration Parameter Details
Logger Configuration Items
| 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 |
Cluster Configuration Items
| 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 |
๐๐ Index Strategy
| 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) |
๐๐ Environment Configuration Support
Supported Environments
- ๐ ๐ ๐ ๏ธ Development - Development environment
- ๐งช๐งช๐งช Test - Testing environment
- ๐ญ๐ญ๐ญ Production - Production environment
Environment Recognition Priority
- ๐ฅ๐ฅ๐ฅ Environment variable
ASPNETCORE_ENVIRONMENT - ๐ฅ๐ฅ๐ฅ Environment variable
ENVIRONMENT - ๐ฅ๐ฅ๐ฅ AppSettings configuration item
Environment
๐๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
<div align="center">
Make logging simple and powerful โจโจ
</div>
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- Elasticsearch.Net (>= 7.17.5)
- NEST (>= 7.17.5)
- Newtonsoft.Json (>= 13.0.1)
- System.Buffers (>= 4.5.1)
- System.Diagnostics.DiagnosticSource (>= 5.0.0)
- System.Memory (>= 4.5.4)
- System.Numerics.Vectors (>= 4.5.0)
- System.Runtime.CompilerServices.Unsafe (>= 5.0.0)
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.8
### Performance Improvements
- Enhanced support for .config file configuration
- Improved the LoggerConfiguration class to correctly parse configuration items in .config files
- Added support for nested cluster configurations
- Fixed issues with the serialization and deserialization of configuration properties
- Implemented more flexible environment-specific configuration support