Step 6: Logging & Monitoring¶
Track the health and performance of your .NET communication features using standard .NET logging patterns and Azure Monitor.
This tutorial walks these steps in order:
flowchart TD
START["Step 6: Logging & Monitoring"]
N1["1. SDK Logging with ILogger"]
N2["2. Application Insights Integration"]
N3["3. Client Diagnostics"]
N4["4. Custom Telemetry"]
N5["5. Diagnostic Settings in Azure"]
START --> N1
N1 --> N2
N2 --> N3
N3 --> N4
N4 --> N5 1. SDK Logging with ILogger¶
The Azure SDK for .NET integrates with ILogger. You can capture SDK logs by configuring your logging provider (e.g., Console, Serilog).
using Azure.Core.Diagnostics;
// Listen to all SDK events and write them to the console
using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
2. Application Insights Integration¶
For ASP.NET Core applications, add the Application Insights SDK.
Add NuGet Package¶
Configure Startup¶
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);
}
3. Client Diagnostics¶
You can configure diagnostic options when initializing your ACS clients.
var options = new SmsClientOptions();
options.Diagnostics.IsLoggingEnabled = true;
options.Diagnostics.LoggedHeaderNames.Add("x-ms-request-id");
var client = new SmsClient(connectionString, options);
4. Custom Telemetry¶
Use TelemetryClient to track custom events related to communications.
using Microsoft.ApplicationInsights;
public class MyService
{
private readonly TelemetryClient _telemetryClient;
public MyService(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
}
public void TrackSmsSent()
{
_telemetryClient.TrackEvent("SMSSent");
}
}
5. Diagnostic Settings in Azure¶
Ensure your ACS resource is configured to send logs to Log Analytics:
- Navigate to your ACS resource in the Azure Portal.
- Select Diagnostic settings > Add diagnostic setting.
- Select categories like SMS Operational Logs or Email Operational Logs.
- Send to your Log Analytics workspace.
Next Step¶
Finalize your solution with Infrastructure as Code.