Édition

Partage via


Collect logs from a JSON file with Azure Monitor Agent

Custom JSON Logs is one of the data sources used in a data collection rule (DCR). Details for the creation of the DCR are provided in Collect data with Azure Monitor Agent. This article provides additional details for the text and JSON logs type.

Many applications and services will log information to a JSON files instead of standard logging services such as Windows Event log or Syslog. This data can be collected with Azure Monitor Agent and stored in a Log Analytics workspace with data collected from other sources.

Prerequisites

Basic operation

The following diagram shows the basic operation of collecting log data from a json file.

  1. The agent watches for any log files that match a specified name pattern on the local disk.
  2. Each entry in the log is collected and sent to Azure Monitor. The incoming stream defined by the user is used to parse the log data into columns.
  3. A default transformation is used if the schema of the incoming stream matches the schema of the target table.

Screenshot that shows log query returning results of comma-delimited file collection.

JSON file requirements and best practices

The file that the Azure Monitor Agent is monitoring must meet the following requirements:

  • The file must be stored on the local drive of the machine with the Azure Monitor Agent in the directory that is being monitored.
  • Each record must be delineated with an end of line.
  • The file must use ASCII or UTF-8 encoding. Other formats such as UTF-16 aren't supported.
  • New records should be appended to the end of the file and not overwrite old records. Overwriting will cause data loss.
  • JSON text must be contained in a single row. The JSON body format is not supported. See sample below.

Adhere to the following recommendations to ensure that you don't experience data loss or performance issues:

  • Create a new log file every day so that you can easily clean up old files.
  • Continuously clean up log files in the monitored directory. Tracking many log files can drive up agent CPU and Memory usage. Wait for at least 2 days to allow ample time for all logs to be processed.
  • Don't rename a file that matches the file scan pattern to another name that also matches the file scan pattern. This will cause duplicate data to be ingested.
  • Don't rename or copy large log files that match the file scan pattern into the monitored directory. If you must, do not exceed 50MB per minute.

Custom table

Before you can collect log data from a JSON file, you must create a custom table in your Log Analytics workspace to receive the data. The table schema must match the columns in the incoming stream, or you must add a transformation to ensure that the output schema matches the table. For example, you can use the following PowerShell script to create a custom table with multiple columns.

$tableParams = @'
{
    "properties": {
        "schema": {
               "name": "{TableName}_CL",
               "columns": [
                    {
                        "name": "TimeGenerated",
                        "type": "DateTime"
                    }, 
                    {
                        "name": "MyStringColumn",
                        "type": "string"
                    },
                    {
                        "name": "MyIntegerColumn",
                        "type": "int"
                    },
                    {
                        "name": "MyRealColumn",
                        "type": "real"
                    },
                    {
                        "name": "MyBooleanColumn",
                        "type": "bool"
                    },
                    {
                        "name": "FilePath",
                        "type": "string"
                    },
                    {
                        "name": "Computer",
                        "type": "string"
                    }
              ]
        }
    }
}
'@

Invoke-AzRestMethod -Path "/subscriptions/{subscription}/resourcegroups/{resourcegroup}/providers/microsoft.operationalinsights/workspaces/{WorkspaceName}/tables/{TableName}_CL?api-version=2021-12-01-preview" -Method PUT -payload $tableParams

Create a data collection rule for a JSON file

Note

The agent based JSON custom file ingestion is currently in preview and does not have a complete UI experience in the portal yet. While you can create the DCR using the portal, you must modify it to define the columns in the incoming stream. See the Resource Manager template tab for details on creating the required DCR.

Incoming stream

JSON files include a property name with each value, and the incoming stream in the DCR needs to include a column matching the name of each property. If you create the DCR using the Azure portal, the columns in the following table will be included in the incoming stream, and you must manually modify the DCR or create it using another method where you can explicitly define the incoming stream.

Column Type Description
TimeGenerated datetime The time the record was generated.
RawData string This column will be empty for a JSON log.
FilePath string If you add this column to the incoming stream in the DCR, it will be populated with the path to the log file. This column is not created automatically and can't be added using the portal. You must manually modify the DCR created by the portal or create the DCR using another method where you can explicitly define the incoming stream.
Computer string If you add this column to the incoming stream in the DCR, it will be populated with the name of the computer. This column is not created automatically and can't be added using the portal. You must manually modify the DCR created by the portal or create the DCR using another method where you can explicitly define the incoming stream.

Create a data collection rule, as described in Collect data with Azure Monitor Agent. In the Collect and deliver step, select JSON Logs from the Data source type dropdown.

Setting Description
File pattern Identifies the location and name of log files on the local disk. Use a wildcard for filenames that vary, for example when a new file is created each day with a new name. You can enter multiple file patterns separated by commas.

Examples:
- C:\Logs\MyLog.json
- C:\Logs\MyLog*.json
- C:\App01\AppLog.json, C:\App02\AppLog.json
- /var/mylog.json
- /var/mylog*.json
Table name Name of the destination table in your Log Analytics Workspace.
Record delimiter Not currently used but reserved for future potential use allowing delimiters other than the currently supported end of line (/r/n).
Transform Ingestion-time transformation to filter records or to format the incoming data for the destination table. Use source to leave the incoming data unchanged.

Troubleshooting

Go through the following steps if you aren't collecting data from the JSON log that you're expecting.

  • Verify that data is being written to the log file being collected.
  • Verify that the name and location of the log file matches the file pattern you specified.
  • Verify that the schema of the incoming stream in the DCR matches the schema in the log file.
  • Verify that the schema of the target table matches the incoming stream or that you have a transformation that will convert the incoming stream to the correct schema.
  • See Verify operation to verify whether the agent is operational and data is being received.

Next steps

Learn more about: