
Azure SQL auditing is a powerful tool that helps you monitor and track database activity, ensuring compliance and security.
To get started with Azure SQL auditing, you'll need to enable it on your server, which can be done through the Azure portal.
Auditing can be configured to capture specific events, such as logins, queries, and DML operations, allowing you to tailor the auditing process to your organization's needs.
By default, Azure SQL auditing will store audit logs in a storage account for up to 90 days, but you can also configure it to send logs to Azure Monitor or Azure Event Grid for further analysis.
Azure SQL Auditing Basics
You can use SQL Database auditing to retain an audit trail of selected events. This allows you to define categories of database actions to be audited.
Auditing helps you report on database activity. You can use pre-configured reports and a dashboard to get started quickly with activity and event reporting.
With auditing, you can analyze reports to find suspicious events, unusual activity, and trends. This helps you stay on top of your database's security and performance.
Here are the key benefits of Azure SQL Auditing:
- Retain an audit trail of selected events
- Report on database activity
- Analyze reports to find suspicious events, unusual activity, and trends
Storage and Destination Options
To configure Azure SQL auditing, you have several storage and destination options. You can store audit logs in a storage account, event hub, or Log Analytics workspace.
Azure storage account is a popular choice for storing audit logs. To configure it, select Storage when you get to the Auditing section and choose an Azure storage account. You can use either Managed Identity or Storage Access Keys for authentication.
Make sure the storage account is in the same region as your database and server, unless you're deploying through other methods. The default retention period for audit logs is 0, which means unlimited retention. You can change this value by moving the Retention (Days) slider in Advanced properties.
If you change the retention period from unlimited to a specific value, note that retention will only apply to logs written after the retention value was changed. Logs written during the period when retention was set to unlimited are preserved, even after retention is enabled.
You can also store audit logs in an event hub. To configure it, select Event Hub and choose an event hub where logs will be stored. Ensure the event hub is in the same region as your database and server.
If you're using multiple targets like storage account, log analytics, or event hub, make sure you have permissions for all the targets, or saving audit configuration will fail.
Here are the key points to keep in mind when configuring storage and destination options:
- Azure storage account requires the storage account to be in the same region as your database and server.
- Event hub requires the event hub to be in the same region as your database and server.
- Multiple targets require permissions for all targets to avoid configuration failures.
- Retention period can be changed, but logs written during unlimited retention period are preserved.
Policy Configuration
Policy Configuration is a crucial aspect of Azure SQL Auditing. You can configure both server and database level auditing, but be aware that enabling database auditing can result in duplicate auditing.
Server-level auditing is applicable to all databases, including newly created ones. This means you don't need to worry about setting up auditing for each individual database.
To avoid duplicate auditing, enable database auditing only when specific requirements cannot be met using server-level auditing. Otherwise, it's best to stick with server-level auditing.
Azure SQL Database's default auditing policy enables all actions from three audit groups: SUCCESSFUL_DATABASE_AUTHENTICATION_GROUP, BATCH_COMPLETED_GROUP, and FAILED_DATABASE_AUTHENTICATION_GROUP. These groups capture important events such as successful database authentication, query completion, and failed database authentication.
To view server or database audit logs, click on View audit logs. This will allow you to review the audit logs and ensure that your auditing policy is working as intended.
Data Management
Data management is crucial for Azure SQL auditing, as it helps organizations track and analyze database activity. Azure SQL auditing provides a centralized repository for audit logs.
Audit logs can be stored in Azure Blob Storage or Azure Data Lake Storage Gen2, which allows for scalable and secure storage of large amounts of data. This ensures that audit logs are retained for as long as needed, without incurring additional costs.
Azure SQL auditing also provides the ability to configure retention policies, which dictate how long audit logs are stored before they are automatically deleted. This helps organizations comply with regulatory requirements and maintain data integrity.
Filter Data with Predicate

Filtering data is a crucial step in data management, and Azure SQL Database offers a feature called PredicateExpression to help you do just that. This feature allows you to configure advanced auditing for Azure SQL Database.
You can use the PredicateExpression argument to specify a condition that captures records that don't meet your criteria, such as non-SELECT statements. For instance, you can use the command `PredicateExpression` and specify the condition to capture records that don't have SELECT statements.
To enable server-level auditing with your modified condition, simply specify the command with the `PredicateExpression` argument. You can then view the audit data and see that it only includes records that meet your specified condition.
To disable server-level audit, you can specify the `disabled` value in the `BlobStorageTargetState` argument. This will prevent any further auditing activity.
Here are some best practices to keep in mind when working with PredicateExpression:
- Evaluate your audit requirements carefully before configuring server or database level audits.
- Avoid configuring both server and database audits unless you have specific requirements.
Log Fields
Log Fields are the building blocks of a data management system. They help define what data is collected, how it's organized, and how it's used.
A well-designed log field can make all the difference in data management. For example, a company might use a log field to track customer interactions, such as when a customer makes a purchase or contacts customer support.
Log fields can be categorized into different types, including text, numerical, and date fields. A text field might be used to store a customer's name or address, while a numerical field might store a customer's order total.
A date field is useful for tracking events that occur over time, such as when a customer places an order or makes a payment. This can help identify trends and patterns in customer behavior.
The number of log fields used can vary depending on the specific needs of the data management system. Some systems might use just a few log fields, while others might use dozens or even hundreds.
Storage and Retrieval
To configure storage for Azure SQL auditing, you'll want to select Storage when you get to the Auditing section. You can use either Managed Identity or Storage Access Keys for authentication, and system-assigned and user-assigned managed identity is supported.
The default value for retention period is 0 (unlimited retention), but you can change this value by moving the Retention (Days) slider in Advanced properties. This means that logs older than the retention period will be deleted.
If you're deploying from the Azure portal, make sure that the storage account is in the same region as your database and server. If you're deploying through other methods, the storage account can be in any region.
You can view audit files in the Azure storage account by going to your resource group and opening the Azure storage account. Then, go to your database container, and it has a folder called sqldbAutiding_ServerAudit_NoRetention, where you'll find XEL files containing audit data.
The XEL files use the extended events audit mechanisms for storing audit data, and by default, it shows the event type, principal time, event timestamp, and action status. You can click on an individual record to get detailed information, including the application, principal name, client IP, and additional information along with the SQL query.
You can also use the Run in Query Editor option to view the audit records. This will open the integrated database query editor, where you can specify your credentials in the SQL server authentication and connect. The SQL query uses the sys.fn_get_audit_file() function for data retrieval, and you can customize it as per your requirement.
Remarks
You can write audit logs to an Azure Storage account behind a virtual network or firewall, but you'll need a general-purpose v2 storage account for this to work. This is a requirement for audit to write to a storage account behind a virtual network or firewall.
Audit logs are written to Append Blobs in an Azure Blob Storage on your Azure subscription, and they're in .xel format, which can be opened with SQL Server Management Studio (SSMS). This is a convenient way to store and review your audit logs.
To configure an immutable log store for server or database-level audit events, follow the instructions provided by Azure Storage. Make sure you've selected Allow additional appends when you configure the immutable blob storage. This will ensure that your audit logs are secure and tamper-proof.
You can write audit logs to an Azure Storage account behind a VNet or firewall, but you'll need to follow the instructions provided in the article "Write audit to a storage account behind VNet and firewall" for specific guidance.
Here are the types of storage accounts that support audit logs:
- General-purpose v2 storage account
- Standard storage account
- Premium storage account with BlockBlobStorage
Note that auditing on read-only replicas is automatically enabled, which can help offload read-only query workloads. For more information about the hierarchy of the storage folders, naming conventions, and log format, see the article "SQL Database audit log format".
View Storage Account Files
Viewing the files in your Azure storage account is a straightforward process. To start, you'll need to navigate to your resource group and open the Azure storage account.
Once you're there, go to your database container and you'll find a folder named sqldbAutiding_ServerAudit_NoRetention. This is where your audit files are stored.
The audit files are in XEL format and use the extended events audit mechanisms for storing audit data. They contain event type, principal time, event timestamp, and action status information.
You can click on an individual record to get detailed information, such as the application, principal name, client IP, and additional information, including the SQL query that was executed.
By default, the query displays the top 100 records based on the event time in the descending order, but you can customize this by modifying the SQL query.
The SQL query uses the sys.fn_get_audit_file() function for data retrieval, allowing you to extract specific records and attributes.
You can export the output in JSON, CSV, or XML format for further analysis or processing.
Here are the possible formats you can export your audit data in:
Frequently Asked Questions
What is auditing in Azure?
Azure auditing helps you identify security gaps by generating, collecting, and analyzing logs from services hosted on Azure, enabling you to strengthen your security policies and mechanisms. By auditing Azure services, you can proactively detect and respond to potential security threats.
Sources
- https://learn.microsoft.com/en-us/azure/azure-sql/database/auditing-overview
- https://learn.microsoft.com/en-us/azure/azure-sql/database/auditing-setup
- https://www.mssqltips.com/sqlservertip/6732/azure-sql-database-auditing/
- https://learn.microsoft.com/en-us/azure/azure-sql/database/audit-log-format
- https://medium.com/@sumathikits86/auditing-for-azure-sql-database-and-azure-synapse-analytics-d3e495a0f93
Featured Images: pexels.com