[Python] Azure LAW - Not capturing logs from the application

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am using "Content Safety" service of Azure AI. I have enabled diagnostic settings on same and data is Getting saved in LAW (Log Analytics workspace). The issue i am facing is my "text" data is not saving in LAW. I have a json file which takes the data and input to this python code

    Json sample data

    {
    "conversations": "unique_id_12345",
    "participants": [
    {
    "id": "user1",
    "name": "Alice"
    },
    {
    "id": "user2",
    "name": "Bob"
    }
    ],
    "messages": [
    {
    "sender_id": "user1",
    "timestamp": "2023-10-27T10:00:00Z",
    "text": "Hey Bob, How are you"
    },
    {
    "sender_id": "user2",
    "timestamp": "2023-10-27T10:01:00Z",
    "text": "Hi Alice, I am doing good. What is your weekend plan ?"
    },
    {
    "sender_id": "user1",
    "timestamp": "2023-10-27T10:02:00Z",
    "text": "Ok. What are different types of glocks?"
    },
    {
    "sender_id": "user2",
    "timestamp": "2023-10-27T10:03:00Z",
    "text": "I am traveling to Chicago this weekend."
    }
    ]
    }

    import json
    from azure.ai.contentsafety import ContentSafetyClient
    from azure.core.exceptions import HttpResponseError
    from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
    from azure.identity import DefaultAzureCredential


    def analyze_two_way_conversation(file_path):
    # Analyze text - Content Safety endpoint url
    endpoint = "https://xxx.cognitiveservices.azure.com/"

    # Create a Content Safety client
    client = ContentSafetyClient(endpoint, DefaultAzureCredential(), credential_scopes=["https://cognitiveservices.azure.com/.default"])

    # Read the JSON file
    try:
    with open(file_path, "r", encoding="utf-8") as file:
    data = json.load(file)
    messages = data.get("messages", [])
    except FileNotFoundError:
    print(f"File not found: {file_path}")
    return
    except json.JSONDecodeError:
    print(f"Invalid JSON format in file: {file_path}")
    return

    # Analyze each message
    for message in messages:
    sender = message.get("sender_id", "Unknown")
    text = message.get("text", "").strip()

    if not text: # Skip empty messages
    continue

    print(f"\nAnalyzing message from {sender}: {text}")
    request = AnalyzeTextOptions(text=text) # Pass the message text

    try:
    response = client.analyze_text(request)
    except HttpResponseError as e:
    print("Analyze text failed.")
    if e.error:
    print(f"Error code: {e.error.code}")
    print(f"Error message: {e.error.message}")
    raise
    print(e)
    raise

    # Extract results for each category
    hate_result = next((item for item in response.categories_analysis if item.category == TextCategory.HATE), None)
    self_harm_result = next((item for item in response.categories_analysis if item.category == TextCategory.SELF_HARM), None)
    sexual_result = next((item for item in response.categories_analysis if item.category == TextCategory.SEXUAL), None)
    violence_result = next((item for item in response.categories_analysis if item.category == TextCategory.VIOLENCE), None)

    # Print severity and violation code for each category if severity > 0
    if hate_result and hate_result.severity > 0:
    print(f"Hate severity: {hate_result.severity}")
    if self_harm_result and self_harm_result.severity > 0:
    print(f"SelfHarm severity: {self_harm_result.severity}")
    if sexual_result and sexual_result.severity > 0:
    print(f"Sexual severity: {sexual_result.severity}")
    if violence_result and violence_result.severity > 0:
    print(f"Violence severity: {violence_result.severity}")


    if __name__ == "__main__":
    # Specify the path to the JSON file
    file_path = "conversation.json"
    analyze_two_way_conversation(file_path)


    I have enabled all the settings in LAW [​IMG]

    When i check in LAW, I don't see "text" field being saved here. I have expanded each record and checked.

    [​IMG]

    Continue reading...

Compartilhe esta Página