Added new flows
This commit is contained in:
@@ -31,26 +31,398 @@ It features a rich fluent API for routing log events to dozens of destinations -
|
||||
- **Lazy initialization** - Flows are only initialized when first used, reducing startup overhead.
|
||||
|
||||
|
||||
### Telemetry & Observability
|
||||
## Telemetry & Observability
|
||||
|
||||
EonaCat.LogStack includes built-in telemetry primitives for production monitoring:
|
||||
EonaCat.LogStack includes **built-in, zero-dependency telemetry** for comprehensive production monitoring:
|
||||
|
||||
- **Metrics registry** - Low-overhead counters and snapshots for logging throughput.
|
||||
- **Telemetry snapshots** - Capture service name, environment, runtime/process metrics, log counters and trace activity.
|
||||
- **Runtime health data** - Export operational status for dashboards and monitoring systems.
|
||||
- **Tracing support** - Activity/span integration for distributed request correlation.
|
||||
- **Custom exporters** - Build exporters around telemetry snapshots for your monitoring backend.
|
||||
### Enhanced Telemetry System
|
||||
|
||||
#### Metrics Collection & Aggregation
|
||||
|
||||
Track logging operations with built-in metrics:
|
||||
|
||||
Example:
|
||||
```csharp
|
||||
var options = new TelemetryOptions
|
||||
{
|
||||
Enabled = true,
|
||||
IncludeRuntimeMetrics = true,
|
||||
IncludeTraceMetrics = true
|
||||
};
|
||||
var aggregator = new EonaCat.LogStack.Telemetry.TelemetryAggregator();
|
||||
|
||||
// Record counters
|
||||
aggregator.RecordCounter("logs_logged", 1);
|
||||
aggregator.RecordCounter("logs_dropped", 1);
|
||||
|
||||
// Record gauges (point-in-time measurements)
|
||||
aggregator.RecordGauge("memory_usage_mb", GC.GetTotalMemory(false) / 1024 / 1024);
|
||||
|
||||
// Record histograms (with percentiles)
|
||||
aggregator.RecordHistogram("request_duration_ms", 42);
|
||||
|
||||
// Get snapshot
|
||||
var snapshot = aggregator.GetSnapshot();
|
||||
Console.WriteLine($"Uptime: {snapshot.Uptime.TotalSeconds}s");
|
||||
Console.WriteLine($"Total Events: {snapshot.TotalEvents}");
|
||||
Console.WriteLine($"Logs Logged: {snapshot.LogsLogged}");
|
||||
Console.WriteLine($"Logs Dropped: {snapshot.LogsDropped}");
|
||||
```
|
||||
|
||||
#### Health Monitoring
|
||||
|
||||
Track component health and detect degradation:
|
||||
|
||||
```csharp
|
||||
var health = new EonaCat.LogStack.Telemetry.HealthMonitor();
|
||||
|
||||
// Update component health
|
||||
health.UpdateComponentHealth("EmailFlow", HealthStatus.Healthy);
|
||||
health.UpdateComponentHealth("ElasticsearchFlow", HealthStatus.Degraded, "High latency detected");
|
||||
|
||||
// Record errors automatically impact health
|
||||
health.RecordError("DatabaseFlow", new TimeoutException("Connection timeout"));
|
||||
|
||||
// Get health snapshot
|
||||
var snapshot = health.GetSnapshot();
|
||||
Console.WriteLine($"Overall Status: {snapshot.OverallStatus}");
|
||||
Console.WriteLine($"Healthy: {snapshot.HealthyComponents}");
|
||||
Console.WriteLine($"Degraded: {snapshot.DegradedComponents}");
|
||||
console.WriteLine($"Unhealthy: {snapshot.UnhealthyComponents}");
|
||||
```
|
||||
|
||||
#### Distributed Tracing
|
||||
|
||||
Enable end-to-end request tracing across async boundaries:
|
||||
|
||||
```csharp
|
||||
// Enable tracing in DI
|
||||
services.AddEonaCatTracing();
|
||||
|
||||
// Use trace context manager
|
||||
var traceId = TraceContextManager.GetTraceId();
|
||||
TraceContextManager.SetCorrelationId("order-123");
|
||||
TraceContextManager.AddBaggage("UserId", "user-456");
|
||||
|
||||
// Create spans for operations
|
||||
var spanFactory = sp.GetRequiredService<SpanFactory>();
|
||||
|
||||
using (spanFactory.CreateSpanScope("ProcessOrder"))
|
||||
{
|
||||
// Operation code here
|
||||
// Automatically tracked with context propagation
|
||||
}
|
||||
|
||||
// Access current trace context
|
||||
var context = TraceContextManager.Current;
|
||||
Console.WriteLine($"Trace: {context.TraceId}");
|
||||
Console.WriteLine($"Span: {context.SpanId}");
|
||||
Console.WriteLine($"Parent: {context.ParentSpanId}");
|
||||
```
|
||||
|
||||
#### Performance Monitoring & Insights
|
||||
|
||||
Automatic performance analysis with bottleneck detection:
|
||||
|
||||
```csharp
|
||||
var insights = new EonaCat.LogStack.PerformanceInsights.PerformanceInsightsCollector();
|
||||
|
||||
// Track operations
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
// ... operation ...
|
||||
sw.Stop();
|
||||
insights.RecordOperation("DatabaseQuery", sw.ElapsedTicks, byteCount: 1024, isError: false);
|
||||
|
||||
// Get performance analysis
|
||||
var analysis = new EonaCat.LogStack.PerformanceInsights.PerformanceAnalyzer(insights).Analyze();
|
||||
|
||||
Console.WriteLine(analysis.Summary);
|
||||
/* Output:
|
||||
Performance Analysis - 2026-03-27T09:15:00Z
|
||||
Uptime: 300.45s
|
||||
Total Operations: 15234
|
||||
Total Errors: 2
|
||||
|
||||
Top Slowest Operations:
|
||||
- DatabaseQuery: 245.32ms (min: 10.12ms, max: 1523.45ms)
|
||||
- HttpRequest: 156.78ms (min: 45.23ms, max: 892.34ms)
|
||||
|
||||
Operations with Most Errors:
|
||||
- ExternalAPI: 2 errors (0.13% error rate)
|
||||
|
||||
Recommendations:
|
||||
[High] ErrorRate: Error rate is 0.01%. Consider investigating error causes.
|
||||
[Medium] SlowOperations: Some operations are very slow (>1000ms avg).
|
||||
*/
|
||||
```
|
||||
|
||||
### Advanced DI Integration
|
||||
|
||||
#### Named Loggers per Category
|
||||
|
||||
Use different logger configurations per component:
|
||||
|
||||
```csharp
|
||||
// Register named loggers
|
||||
services.AddEonaCatNamedLoggers(new LoggerDIOptions
|
||||
{
|
||||
EnableTelemetry = true,
|
||||
EnableTracing = true,
|
||||
EnablePerformanceMonitoring = true,
|
||||
EagerlyInitializeNamedLoggers = true
|
||||
});
|
||||
|
||||
// Later, get loggers by name
|
||||
var namedLoggerFactory = sp.GetRequiredService<INamedLoggerFactory>();
|
||||
|
||||
var apiLogger = namedLoggerFactory.GetLogger("API", builder =>
|
||||
builder.WithMinimumLevel(LogLevel.Debug));
|
||||
|
||||
var dbLogger = namedLoggerFactory.GetLogger("Database", builder =>
|
||||
builder.WithMinimumLevel(LogLevel.Information));
|
||||
```
|
||||
|
||||
#### Composite Loggers
|
||||
|
||||
Combine multiple loggers into one:
|
||||
|
||||
```csharp
|
||||
var logger1 = new LogBuilder("Console").WriteToConsole().Build();
|
||||
var logger2 = new LogBuilder("File").WriteToFile("./logs").Build();
|
||||
|
||||
var composite = new EonaCat.LogStack.DependencyInjection.CompositeLogger(logger1, logger2);
|
||||
|
||||
composite.Information("This goes to both console and file");
|
||||
```
|
||||
|
||||
#### Logger Decorators
|
||||
|
||||
Add cross-cutting concerns to loggers:
|
||||
|
||||
```csharp
|
||||
var chain = new EonaCat.LogStack.DependencyInjection.DecoratorChain()
|
||||
.Add(new PerformanceDecorator())
|
||||
.Add(new SecurityDecorator());
|
||||
|
||||
var decoratedLogger = chain.Apply(baseLogger);
|
||||
```
|
||||
|
||||
### Premium Features
|
||||
|
||||
#### Automatic Policy Engine
|
||||
|
||||
Enable automatic optimization:
|
||||
|
||||
```csharp
|
||||
var policies = new EonaCat.LogStack.Policies.AutoPolicies
|
||||
{
|
||||
Batching = new AutoBatchingPolicy
|
||||
{
|
||||
MinimumBatchSize = 10,
|
||||
MaximumBatchSize = 100,
|
||||
AdaptBatchSize = true
|
||||
},
|
||||
Scaling = new AutoScalingPolicy
|
||||
{
|
||||
Enabled = true,
|
||||
BytesPerSecondThreshold = 10_000_000,
|
||||
ErrorRateThreshold = 0.05
|
||||
},
|
||||
Retention = new AutoRetentionPolicy
|
||||
{
|
||||
MaxAge = TimeSpan.FromDays(30),
|
||||
MaxSize = 10_737_418_240,
|
||||
CompressArchives = true
|
||||
}
|
||||
};
|
||||
|
||||
var engine = new EonaCat.LogStack.Policies.AutoPolicyEngine(policies);
|
||||
var actions = engine.EvaluatePolicies();
|
||||
|
||||
foreach (var action in actions)
|
||||
{
|
||||
Console.WriteLine($"Action: {action.Type} - {action.Reason}");
|
||||
}
|
||||
|
||||
Console.WriteLine(engine.GetPoliciesSummary());
|
||||
```
|
||||
|
||||
#### Common Logging Patterns
|
||||
|
||||
Pre-built patterns for common scenarios:
|
||||
|
||||
```csharp
|
||||
using EonaCat.LogStack.Patterns;
|
||||
|
||||
// HTTP Request logging
|
||||
var reqLogger = new RequestResponseLogger(logger);
|
||||
reqLogger.LogRequest("GET", "/api/users", "req-123");
|
||||
reqLogger.LogResponse(200, 42, "req-123");
|
||||
|
||||
// Database operation logging
|
||||
var dbLogger = new DatabaseOperationLogger(logger);
|
||||
dbLogger.LogQuery("SELECT", "Users");
|
||||
dbLogger.LogQueryTiming("SELECT", 156, rowsAffected: 500);
|
||||
dbLogger.LogConnection("opened");
|
||||
|
||||
// Service initialization logging
|
||||
var initLogger = new ServiceInitializationLogger(logger, "OrderService");
|
||||
initLogger.LogInitializationStart();
|
||||
initLogger.LogComponentInit("DatabaseConnection", success: true);
|
||||
initLogger.LogComponentInit("CacheConnection", success: true);
|
||||
initLogger.LogInitializationComplete(TimeSpan.FromMilliseconds(450));
|
||||
|
||||
// Performance measurement
|
||||
var perfLogger = new PerformanceLogger(logger, thresholdMs: 1000);
|
||||
perfLogger.LogTiming("UserQuery", 245);
|
||||
perfLogger.LogMemoryUsage(52_428_800, 104_857_600);
|
||||
|
||||
// Timed scope for automatic duration logging
|
||||
using (var scope = perfLogger.StartTimedScope("OrderProcessing"))
|
||||
{
|
||||
// ... processing code ...
|
||||
} // Automatically logs duration
|
||||
```
|
||||
|
||||
#### Auto-Initialization
|
||||
|
||||
Automatic logger initialization by type:
|
||||
|
||||
```csharp
|
||||
var provider = new EonaCat.LogStack.Utilities.AutoInitializingLoggerProvider(loggerFactory);
|
||||
|
||||
// Get logger for type (auto-creates if needed)
|
||||
var logger = provider.GetLoggerForType<UserService>();
|
||||
|
||||
// Or generic
|
||||
var logger = provider.GetLoggerForType(typeof(PaymentProcessor));
|
||||
|
||||
// Static provider for global access
|
||||
EonaCat.LogStack.Utilities.StaticLoggerProvider.Initialize(loggerFactory);
|
||||
var logger = EonaCat.LogStack.Utilities.StaticLoggerProvider.GetLoggerFor<OrderService>();
|
||||
```
|
||||
|
||||
#### Log Processing Utilities
|
||||
|
||||
Advanced log event processing:
|
||||
|
||||
```csharp
|
||||
// Batch processing
|
||||
var processor = new EonaCat.LogStack.Utilities.LogBatchProcessor(
|
||||
batchSize: 50,
|
||||
flushInterval: TimeSpan.FromSeconds(5),
|
||||
processor: batch => Console.WriteLine($"Processing {batch.Count} events"));
|
||||
|
||||
// Intelligent routing
|
||||
var router = new EonaCat.LogStack.Utilities.LogRouter();
|
||||
router.RegisterLevelRoute(LogLevel.Error, evt => SendAlert(evt));
|
||||
router.RegisterCategoryRoute("Security", evt => AuditLog(evt));
|
||||
router.RegisterDefaultRoute(evt => Console.WriteLine(evt.Message));
|
||||
|
||||
// Event filtering
|
||||
var filter = new EonaCat.LogStack.Utilities.LogEventFilter()
|
||||
.AddLevelFilter(LogLevel.Warning)
|
||||
.AddCategoryFilter("API")
|
||||
.AddMessageFilter("timeout");
|
||||
|
||||
var filtered = filter.Filter(allEvents);
|
||||
```
|
||||
|
||||
### DI Configuration Examples
|
||||
|
||||
#### Complete Setup
|
||||
|
||||
```csharp
|
||||
services.AddEonaCatLogging("MyApp", builder =>
|
||||
builder
|
||||
.WithMinimumLevel(LogLevel.Information)
|
||||
.WriteToConsole()
|
||||
.WriteToFile("./logs")
|
||||
.BoostWithCorrelationId());
|
||||
|
||||
// Enable all advanced features
|
||||
services.AddEonaCatNamedLoggers(new LoggerDIOptions
|
||||
{
|
||||
EnableTelemetry = true,
|
||||
EnableTracing = true,
|
||||
EnablePerformanceMonitoring = true,
|
||||
EnableHealthMonitoring = true
|
||||
});
|
||||
|
||||
services.AddEonaCatTracing();
|
||||
services.AddEonaCatPerformanceMonitoring();
|
||||
services.AddEonaCatHealthMonitoring();
|
||||
services.AddEonaCatTelemetryAggregation();
|
||||
```
|
||||
|
||||
#### Usage in Application
|
||||
|
||||
```csharp
|
||||
public class OrderService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly TelemetryAggregator _telemetry;
|
||||
private readonly SpanFactory _spans;
|
||||
private readonly PerformanceAnalyzer _perf;
|
||||
|
||||
public OrderService(
|
||||
ILogger logger,
|
||||
TelemetryAggregator telemetry,
|
||||
SpanFactory spans,
|
||||
PerformanceAnalyzer perf)
|
||||
{
|
||||
_logger = logger;
|
||||
_telemetry = telemetry;
|
||||
_spans = spans;
|
||||
_perf = perf;
|
||||
}
|
||||
|
||||
public async Task<Order> ProcessOrderAsync(string orderId)
|
||||
{
|
||||
using var span = _spans.CreateSpanScope("ProcessOrder");
|
||||
span.SetAttribute("orderId", orderId);
|
||||
|
||||
try
|
||||
{
|
||||
_logger.Information("Processing order",
|
||||
("OrderId", orderId),
|
||||
("Timestamp", DateTime.UtcNow));
|
||||
|
||||
_telemetry.RecordCounter("orders.processed", 1);
|
||||
|
||||
// ... processing logic ...
|
||||
|
||||
_telemetry.RecordGauge("order.revenue", 99.99);
|
||||
return order;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_telemetry.RecordCounter("orders.failed", 1);
|
||||
span.RecordException(ex);
|
||||
_logger.Error(ex, "Order processing failed", ("OrderId", orderId));
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Telemetry Export
|
||||
|
||||
Export metrics to external systems:
|
||||
|
||||
```csharp
|
||||
// Prometheus-format export
|
||||
var prometheus = aggregator.ExportPrometheusFormat();
|
||||
File.WriteAllText("metrics.txt", prometheus);
|
||||
|
||||
// Custom export
|
||||
var snapshot = aggregator.GetSnapshot();
|
||||
await httpClient.PostAsJsonAsync(
|
||||
"https://monitoring.example.com/metrics",
|
||||
new
|
||||
{
|
||||
snapshot.Uptime,
|
||||
snapshot.TotalEvents,
|
||||
snapshot.HealthStatus,
|
||||
Metrics = snapshot.RecordedMetrics
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
|
||||
## Supported Targets
|
||||
|
||||
- .NET Standard 2.1
|
||||
|
||||
Reference in New Issue
Block a user