Monitoring and Logging in Azure Service Fabric
Monitoring and Logging in Service Fabric Applications
Building applications is only half the story — you must monitor and log them properly to ensure smooth, reliable operations. Service Fabric provides built-in tools and APIs to help developers monitor service health and troubleshoot issues.
🛰️ Why Monitoring and Logging are Critical?
- Early detection of issues (failures, slowdowns, bottlenecks).
- Understanding system behavior and usage patterns.
- Essential for troubleshooting and root cause analysis.
- Supports proactive scaling and maintenance.
Real-World Analogy:
Monitoring and logging are like the black box and radar in an aircraft — even if everything looks fine, they continuously record data that’s invaluable during emergencies.
🚀 Monitoring Tools in Service Fabric
1. Service Fabric Explorer (SFX)
A real-time dashboard that shows:
- Cluster health (overall and per node).
- Application health and event logs.
- Replica status and load metrics.
2. Application Health Reports
Services can report their health explicitly using Service Fabric APIs:
var healthInfo = new HealthInformation("MyApp", "DatabaseConnectivity", HealthState.Warning) { Description = "Database connection slow." }; await this.Partition.ReportPartitionHealthAsync(healthInfo);
Key Points: Healthy, Warning, Error states can be reported.
3. Event Tracing for Windows (ETW)
- Built-in support for high-performance event logging.
- View ETW logs using tools like PerfView, Windows Event Viewer, or Azure Monitor.
4. Application Insights Integration
- Can be easily integrated into Service Fabric services.
- Provides dashboards for request rates, response times, failure rates, etc.
- Custom telemetry like traces, exceptions, dependencies.
🛠️ Basic Logging in Service Fabric Services
Using Built-in EventSource Class
Example:
public static class MyServiceEventSource { private static readonly EventSource eventSource = new EventSource("MyServiceEvents"); public static void ServiceMessage(StatelessService service, string message) { eventSource.Write("ServiceMessage", new { ServiceName = service.Context.ServiceName, Message = message }); } }
Calling Logger inside Services
MyServiceEventSource.ServiceMessage(this, "Order processed successfully!");
📈 Visual Monitoring Flow
[Service Health Events] → [Service Fabric Explorer Dashboard] → [Alerts, Warnings, Errors Visible] → [Root Cause Analysis and Actions]
💡 Did You Know?
Even if the cluster is down, Service Fabric can persist health events for later analysis!
⚡ Common Beginner Issues and Solutions
-
Problem: Missing health reports.
Solution: Ensure services call health APIs periodically. -
Problem: Logs not visible in Event Viewer.
Solution: Check if ETW providers are enabled and logs are written correctly. -
Problem: High latency in monitoring dashboard.
Solution: Reduce the number of frequent health reports; aggregate where possible.
🚨 Best Practices
- Send regular health checks from services (heartbeat).
- Integrate Application Insights early — much easier than retrofitting later.
- Use structured logging (JSON format) when logging complex data.
✅ Self-Check Quiz
- What are the three health states that can be reported?
- Name one tool you can use to view ETW logs.
- Why is Application Insights useful for Service Fabric applications?