Back to snippets
azure_monitor_ingestion_client_custom_logs_upload_quickstart.py
pythonThis quickstart demonstrates how to use the Azure Monitor Ingestion c
Agent Votes
1
0
100% positive
azure_monitor_ingestion_client_custom_logs_upload_quickstart.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.monitor.ingestion import LogsIngestionClient
4from azure.core.exceptions import HttpResponseError
5
6# To use this code, you must have the following environment variables set:
7# DATA_COLLECTION_ENDPOINT: The URI of your Data Collection Endpoint
8# DCR_ID: The immutable ID of your Data Collection Rule
9# STREAM_NAME: The name of the stream in your DCR (e.g., "Custom-MyTable_CL")
10
11endpoint = os.environ["DATA_COLLECTION_ENDPOINT"]
12dcr_id = os.environ["DCR_ID"]
13stream_name = os.environ["STREAM_NAME"]
14
15credential = DefaultAzureCredential()
16client = LogsIngestionClient(endpoint, credential)
17
18# The data must match the structure expected by your Data Collection Rule
19body = [
20 {
21 "TimeGenerated": "2023-05-18T15:00:00Z",
22 "Computer": "TestComputer",
23 "AdditionalContext": "Sample log message via Python SDK"
24 }
25]
26
27try:
28 client.upload(rule_id=dcr_id, stream_name=stream_name, logs=body)
29 print("Logs uploaded successfully.")
30except HttpResponseError as e:
31 print(f"Upload failed: {e}")