Class AuditLogsApi
- Namespace
- Cloudflare.NET.AuditLogs
- Assembly
- Cloudflare.NET.dll
Provides methods for interacting with the Cloudflare Audit Logs API.
Audit logs record actions taken on account resources and are retained for 30 days.
public class AuditLogsApi : ApiResource, IAuditLogsApi
- Inheritance
-
AuditLogsApi
- Implements
- Inherited Members
Constructors
AuditLogsApi(HttpClient, ILoggerFactory)
Provides methods for interacting with the Cloudflare Audit Logs API.
Audit logs record actions taken on account resources and are retained for 30 days.
public AuditLogsApi(HttpClient httpClient, ILoggerFactory loggerFactory)
Parameters
httpClientHttpClientloggerFactoryILoggerFactory
Methods
GetAccountAuditLogsAsync(string, ListAuditLogsFilters?, CancellationToken)
Gets audit logs for the account.
Note: Audit logs are only available for the past 30 days. This is a beta API and error handling may be incomplete.
public Task<CursorPaginatedResult<AuditLog>> GetAccountAuditLogsAsync(string accountId, ListAuditLogsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
accountIdstringThe account identifier.
filtersListAuditLogsFiltersOptional filtering and pagination options.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<CursorPaginatedResult<AuditLog>>
A page of audit logs with cursor information.
Examples
// Get recent audit logs
var logs = await client.AuditLogs.GetAccountAuditLogsAsync(accountId,
new ListAuditLogsFilters(
Since: DateTime.UtcNow.AddDays(-7),
Limit: 100));
foreach (var log in logs.Items)
{
Console.WriteLine($"{log.Action.Time}: {log.Action.Type} by {log.Actor.Email}");
}
Exceptions
- ArgumentException
Thrown when
accountIdis null or whitespace.- HttpRequestException
Thrown when the API returns an HTTP error status code.
- CloudflareApiException
Thrown when the API returns a non-success response.
GetAllAccountAuditLogsAsync(string, ListAuditLogsFilters?, CancellationToken)
Gets all audit logs for the account, automatically handling pagination.
Note: Audit logs are only available for the past 30 days. Consider using time filters to limit results for large accounts.
public IAsyncEnumerable<AuditLog> GetAllAccountAuditLogsAsync(string accountId, ListAuditLogsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
accountIdstringThe account identifier.
filtersListAuditLogsFiltersOptional filtering options. Cursor is ignored.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<AuditLog>
An async enumerable of all matching audit logs.
Examples
// Get all failed actions in the last week
var filters = new ListAuditLogsFilters(
Since: DateTime.UtcNow.AddDays(-7),
ActionResults: new[] { "failure" });
await foreach (var log in client.AuditLogs.GetAllAccountAuditLogsAsync(accountId, filters))
{
Console.WriteLine($"Failed: {log.Action.Type} - {log.Action.Description}");
}
Exceptions
- ArgumentException
Thrown when
accountIdis null or whitespace.- HttpRequestException
Thrown when the API returns an HTTP error status code.
- CloudflareApiException
Thrown when the API returns a non-success response.
ListAllUserAuditLogsAsync(ListAuditLogsFilters?, CancellationToken)
Gets all audit logs for the authenticated user, automatically handling pagination.
Audit logs are only available for the past 30 days. Consider using time filters to limit results for active users.
public IAsyncEnumerable<AuditLog> ListAllUserAuditLogsAsync(ListAuditLogsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListAuditLogsFiltersOptional filtering options. Cursor is ignored.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<AuditLog>
An async enumerable of all matching user audit logs.
Examples
// Get all failed actions in the last week
var filters = new ListAuditLogsFilters(
Since: DateTime.UtcNow.AddDays(-7),
ActionResults: new[] { "failure" });
await foreach (var log in client.AuditLogs.ListAllUserAuditLogsAsync(filters))
{
Console.WriteLine($"Failed: {log.Action.Type} - {log.Action.Description}");
}
Exceptions
- HttpRequestException
Thrown when the API returns an HTTP error status code.
- CloudflareApiException
Thrown when the API returns a non-success response.
ListUserAuditLogsAsync(ListAuditLogsFilters?, CancellationToken)
Gets audit logs for actions taken by the authenticated user.
Audit logs are only available for the past 30 days. Shows actions taken by the user across all their accounts.
public Task<CursorPaginatedResult<AuditLog>> ListUserAuditLogsAsync(ListAuditLogsFilters? filters = null, CancellationToken cancellationToken = default)
Parameters
filtersListAuditLogsFiltersOptional filtering and pagination options.
cancellationTokenCancellationTokenA cancellation token.
Returns
- Task<CursorPaginatedResult<AuditLog>>
A page of audit logs with cursor information.
Examples
// Get recent user audit logs
var logs = await client.AuditLogs.ListUserAuditLogsAsync(
new ListAuditLogsFilters(
Since: DateTime.UtcNow.AddDays(-7),
Limit: 100));
foreach (var log in logs.Items)
{
Console.WriteLine($"{log.Action.Time}: {log.Action.Type} by {log.Actor.Email}");
}
Exceptions
- HttpRequestException
Thrown when the API returns an HTTP error status code.
- CloudflareApiException
Thrown when the API returns a non-success response.