# Azure Table Audit

Table audit supports both Cosmos DB Table API & Azure Storage account tables.

There are support for several different configuraiton options depending on your needs.

# Storage account configuration

# Supply storage account

Uses MassTransit.Azure.Cosmos.Table (opens new window)
Uses MassTransit.Extensions.DependencyInjection (opens new window)

namespace AuditAzureTableWithStorageAccount
{
    using MassTransit;
    using Microsoft.Azure.Cosmos.Table;
    using Microsoft.Extensions.DependencyInjection;

    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("INSERT STORAGE ACCOUNT CONNECTION STRING");
            string auditTableName = "messageaudittable";

            services.AddMassTransit(x =>
            {
                x.AddBus(bus => Bus.Factory.CreateUsingInMemory(cfg =>
                {
                    cfg.UseAzureTableAuditStore(storageAccount, auditTableName);
                }));
            });
        }
    }
}

# Supply your own table

Uses MassTransit.Azure.Cosmos.Table (opens new window)
Uses MassTransit.Extensions.DependencyInjection (opens new window)

namespace AuditAzureTableWithTableSupplied
{
    using MassTransit;
    using Microsoft.Azure.Cosmos.Table;
    using Microsoft.Extensions.DependencyInjection;

    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("INSERT STORAGE ACCOUNT CONNECTION STRING");
            CloudTableClient client = storageAccount.CreateCloudTableClient();
            CloudTable table = client.GetTableReference("audittablename");
            table.CreateIfNotExists();

            services.AddMassTransit(x =>
            {
                x.AddBus(bus => Bus.Factory.CreateUsingInMemory(cfg =>
                {
                    cfg.UseAzureTableAuditStore(table);
                }));
            });
        }
    }
}

# Partition Key Strategy

When using Azure Tables it is important to use the relevant partition key strategy to the likely queries you'll perform on the message data. The default partition key supplied is using the message context type (e.g "SEND" & "CONSUME"). However if you would like to override this, you can supply your own strategy by specifying this on configuration.

NOTE

Note: Please consider the official documentation for guidance on partition key strategy here (opens new window)

Uses MassTransit.Azure.Cosmos.Table (opens new window)
Uses MassTransit.Extensions.DependencyInjection (opens new window)

namespace AuditAzureTableWithCustomPartitionKey
{
    using MassTransit;
    using MassTransit.Azure.Table.Audit;
    using Microsoft.Azure.Cosmos.Table;
    using Microsoft.Extensions.DependencyInjection;

    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("INSERT STORAGE ACCOUNT CONNECTION STRING");
            string auditTableName = "messageaudittable";
            string PartitionKey = "CustomPartitionKey";

            services.AddMassTransit(x =>
            {
                x.AddBus(bus => Bus.Factory.CreateUsingInMemory(cfg =>
                {
                    cfg.UseAzureTableAuditStore(storageAccount, auditTableName, new ConstantPartitionKeyFormatter(PartitionKey));
                }));
            });
        }
    }

    class ConstantPartitionKeyFormatter :
        IPartitionKeyFormatter
    {
        readonly string _partitionKey;

        public ConstantPartitionKeyFormatter(string partitionKey)
        {
            _partitionKey = partitionKey;
        }

        public string Format<T>(AuditRecord record)
            where T : class
        {
            return _partitionKey;
        }
    }
}

# Audit Filter

You can configure the message filter on auditing as below.

Uses MassTransit.Azure.Cosmos.Table (opens new window)
Uses MassTransit.Extensions.DependencyInjection (opens new window)

namespace AuditAzureTableWithMessageTypeFilter
{
    using System.Collections.Generic;
    using MassTransit;
    using Microsoft.Azure.Cosmos.Table;
    using Microsoft.Extensions.DependencyInjection;

    class Program
    {
        static void Main(string[] args)
        {
            var services = new ServiceCollection();

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("INSERT STORAGE ACCOUNT CONNECTION STRING");
            string auditTableName = "messageaudittable";

            services.AddMassTransit(x =>
            {
                x.AddBus(bus => Bus.Factory.CreateUsingInMemory(cfg =>
                {
                    cfg.UseAzureTableAuditStore(storageAccount, auditTableName, filter => filter.Exclude(typeof(LargeMessage), typeof(SecretMessage)));
                }));
            });
        }
    }

    class SecretMessage
    {
        public string TopSecretData { get; set; }
    }

    class LargeMessage
    {
        public IEnumerable<string> HugeArray { get; set; }
    }
}