If you’re seeing **SQL Server Error 566**—“*An error occurred while writing an audit*”—this is a common audit logging issue that prevents transactions from being recorded properly. The error typically arises when the audit writer service fails to write data to the audit log or journal file, often due to file system permissions, disk space issues, or misconfigured SQL Server settings. The fix involves troubleshooting the underlying cause and adjusting audit-related configurations.

What You'll Need

  • Administrative permissions to SQL Server and the audit storage location.
  • Access to SQL Server Management Studio (SSMS) or the SQL Server configuration manager.
  • Basic knowledge of SQL Server audit configurations (if needed).
  • Sufficient disk space (at least 10% free in the audit storage location).
  • File permissions allowing SQL Server to write to the audit log directory (e.g., `C:\ProgramData\Microsoft\Microsoft SQL Server\MSSQLXXX\OLAP\Audit`).

Step-by-Step

  1. Check disk space and file permissions. SQL Server often fails to write audit logs when the target storage is full or lacks write permissions. Run the following query in SSMS to verify: SELECT name, size, free FROM sys.master_files WHERE database_id = DB_ID('your_database')

    If the audit log folder (e.g., `C:\SQLAuditLogs`) is full, delete old audit files or expand disk space. Ensure the SQL Server service account has write permissions to the folder. For example, on Windows, grant the SQL Server service account (e.g., `NT Service\SQLSERVERAGENT`) read/write access to the folder.

  2. Restart the SQL Server audit writer service. If the issue persists, the audit writer service may be stuck. Restart it via:
    • Windows: Open Services.msc, locate "SQL Server Audit Writer," and restart it.
    • Linux/Unix: Restart the service using the appropriate command (e.g., `sudo systemctl restart sqlserverauditwriter`).

    Check service status with:

    (Get-Service -Name "SQLSERVERAGENT" -ErrorAction SilentlyContinue) | Where-Object {$_.Status -eq "Running"}
  3. Verify audit configuration in SQL Server. Audit settings can conflict with the error. Run: SELECT name, is_disabled FROM sys.server_audits to check if an audit is disabled or misconfigured. If needed, modify or recreate the audit:
    1. Open SSMS > Right-click the server > Tasks > Manage Audit.
    2. Select the affected audit and click Edit.
    3. Adjust settings (e.g., storage type: file or table) and save.

    For file-based audits, ensure the `audit_file_path` is set correctly in SQL Server configuration (via `sqlservr.exe` config file or SSMS properties).

  4. Recreate the audit log file. If the issue is due to a corrupted log file, recreate it:
    1. Delete the old audit log file (e.g., `audit.log` or `audit*.bak`).
    2. Restart SQL Server to trigger a new log file creation.
  5. Update SQL Server and dependencies. Outdated components can cause audit failures. Update:
    • SQL Server itself (via SQL Server Setup).
    • Windows OS and critical services (e.g., Disk Manager, Network Adapter).
  6. Check SQL Server error logs. If the issue persists, inspect detailed logs:
    1. Open SQL Server Error Logs via SSMS > Server Objects > Error Logs.
    2. Look for related errors (e.g., "File not found," "Permission denied").

    Key error codes to search for: 566, 18456, 1222.

If That Did Not Work

If steps 1–6 fail, escalate:
  1. Reinstall SQL Server Audit components (backup first!).
  2. Contact Microsoft Support with specific error logs.
  3. Check for third-party auditing tools interfering.
  4. Temporarily disable auditing to test if the issue is environment-specific.

Common Mistakes

  • Ignoring disk space: Audit logs consume space rapidly. Monitor with `SELECT * FROM sys.dm_server_audit_files`.
  • Misconfigured file paths: Ensure `audit_file_path` matches the actual folder in SQL Server config.
  • Service account permissions: The SQL Server service account must own or have access to the audit log folder.
  • Concurrent writes: Multiple services writing to the same audit folder can cause conflicts.
  • Ignoring error codes: Search for "566" in logs or query `sys.dm_hotstats` for blocked operations.

Tips

  • Set up alerts for disk space thresholds in SQL Server.
  • Use `WAITFOR DELAY '00:00:10'` in stored procedures to avoid audit lockups during long-running transactions.
  • For cloud SQL Server, ensure storage quotas allow audit log expansion.
  • Test audit configurations in a dev environment first.
  • Document audit paths and permissions in your infrastructure logs.

Troubleshooting

  1. Symptoms: SQL Server fails to commit transactions, audit files show zero size, or "Failed to write audit data" errors appear in logs.
  2. Common causes:
    • Insufficient disk space (audit logs grow at ~100MB/minute for high-activity databases).
    • Corrupted audit files or improperly configured paths.
    • Race conditions between SQL Server and audit writer services.
    • Permissions: The SQL Server service account lacks write access to the audit folder.
  3. Advanced checks:
    1. Use `SP_RECONFIGURE` with `AUDIT_RECONFIGURE` parameter to reset audit settings.
    2. Enable audit tracing in SQL Server Agent for real-time monitoring.
    3. Check for orphaned audit files in `C:\Windows\System32\config\audit\*.bak`.
  4. If all else fails:
    • Restore a backup before attempting a full audit reinstall.
    • Contact your DBA or SQL Server support team with the exact error log snippet.

Frequently Asked Questions

Why does SQL Server Error 566 occur?

Error 566 typically happens when SQL Server cannot write to the audit storage location due to disk fullness, permissions, or a corrupted audit writer service. It’s a general-purpose error that masks deeper issues like missing files or service failures.

How do I check if SQL Server is writing audits correctly?

Run this query to verify audit activity: SELECT name, audit_file_name, count(*) FROM sys.dm_server_audit_files WHERE database_id = DB_ID('your_database') GROUP BY name, audit_file_name ORDER BY count(*) DESC; If counts are zero or files are missing, the audit is not functioning.

Can I disable audits temporarily to debug?

Yes, disable audits via SSMS > Server Objects > Audits > Right-click > Disable. Test without auditing to rule out storage conflicts.

What if the audit log folder is on a shared network drive?

Network drives can introduce latency. Place the audit folder on a local disk (e.g., C:\) for reliability, or ensure network latency is acceptable.

How often should I clean old audit logs?

Regularly delete audit files older than 30 days (or as per your retention policy). Use `DROP AUDIT` or `RECONFIGURE` to purge inactive audits.

Does SQL Server 2019 have different audit error handling?

SQL Server 2019+ introduces improved audit stability. If you’re on 2019+, ensure you’re using the latest service pack and audit writer version. Check for new error codes like "Audit not configured properly."