Instrument AL code with custom telemetry events

Completed

After you establish telemetry destinations, the next task is to emit signals that answer a diagnostic question. Useful instrumentation describes an operation's lifecycle and carries only the dimensions needed to correlate and classify the result.

Design an event contract

Start with a small, stable event contract. Assign each custom event an app-specific identifier, a clear message, a verbosity, a data classification, a scope, and a bounded set of dimensions. For an operation, a practical contract includes:

  • A started event that records the correlation identifier.

  • A succeeded event that records the same identifier and a result category.

  • A failed event that records the same identifier, a result category, and a non-sensitive failure category.

Prefix event IDs with a short code that uniquely identifies your app. For example, MTE001, MTE002, and MTE003 reduce collision risk and make ownership visible. Business Central adds AL when it emits a custom AL event, so MTE001 appears as ALMTE001 in customDimensions.eventId.

Keep event IDs constant across executions. Do not generate a new event ID for each operation or include customer identifiers in it. Document each event’s meaning, dimensions, severity, and expected response. Preserve existing meanings when evolving the schema so that queries and alerts remain valid.

Use PascalCase keys in the AL custom-dimensions dictionary. Business Central adds the al prefix to emitted custom dimensions. For example, OperationId, Result, and FailureReason appear in Application Insights as alOperationId, alResult, and alFailureReason.

Keep categorical dimensions low in sensitivity and bounded in variety. Use fixed categories such as Started, Succeeded, or ValidationFailed for grouping. A generated operation GUID is deliberately high-cardinality: retain it only for correlation, rather than as an alert dimension or routine aggregation category. Generate it independently of customer or document identifiers. Exclude customer names, document contents, email addresses, access tokens, and raw error messages.

Emit events with Session.LogMessage

Session.LogMessage sends a custom event from AL. The following pattern establishes one correlation identifier and sends a started event to the publisher destination:

procedure LogOperationStarted(OperationId: Guid)
var
    CustomDimensions: Dictionary of [Text, Text];
begin
    CustomDimensions.Add('OperationId', Format(OperationId));
    CustomDimensions.Add('Result', 'Started');

    Session.LogMessage(
        'MTE001',
        'Telemetry sample operation started.',
        Verbosity::Normal,
        DataClassification::SystemMetadata,
        TelemetryScope::ExtensionPublisher,
        CustomDimensions);
end;

This procedure is a teaching fragment. The complete exercise creates the GUID in its operation procedure and supplies the full implementation; don't add this fragment as a separate exercise object.

Use DataClassification::SystemMetadata for Application Insights custom telemetry. Business Central doesn't send custom events with another data classification to Application Insights. Changing scope or verbosity doesn't override this restriction. This behavior helps enforce a clear boundary, but you still need to review message text and dimensions so they contain no customer-sensitive values. Classification controls whether the event is eligible for transmission; it does not inspect, redact, or anonymize its contents. Marking an event as SystemMetadata does not make sensitive message text or dimension values safe to transmit.

Choose Verbosity::Normal for expected lifecycle events that support routine analysis. Reserve higher severities for conditions that warrant operational attention. High severity doesn't make an event more searchable or compensate for a weak event contract.

Log at meaningful operation boundaries rather than for every record in a large loop. Prefer a completion summary containing duration, processed count, rejected count, and a bounded outcome category. Reuse platform-provided extension and environment dimensions instead of duplicating them.

Select the narrowest useful scope

The event's audience determines its scope. An extension publisher often needs started, succeeded, and failed events to compare extension versions and diagnose app behavior. In that case, TelemetryScope::ExtensionPublisher is the narrowest useful choice. If an environment administrator also needs the custom event to operate the tenant, TelemetryScope::All makes it eligible for both configured destinations.

Avoid emitting the same event twice with different scopes. Duplicate signals complicate counts and can create misleading alerts. Select one scope as part of the event contract and document the reason.

Contrast online telemetry with Windows Event Log

Business Central on-premises deployments can write custom AL telemetry to Windows Event Log. That destination uses server infrastructure and operational practices that don't apply to a Business Central online sandbox. Application Insights is the focus of this module because it supports centralized querying, correlation, and Azure Monitor access for the online service.

The event design principles still transfer: stable identifiers, safe dimensions, and deliberate severity remain important. The setup and collection path don't transfer, so don't treat Event Log instrumentation as an online fallback. For details, see Create custom telemetry events for Application Insights and Create custom telemetry events for Event Log.