Because I wanted to configure adaptive sampling only for my release-configuration in the production environment I wondered whether it is possible to transform ApplicationInsights.config similar to transforming web.config in Web-projects.
Turned out that this can be achieved by using the XML-Document-Transform namespace, which is mapped to the xdt prefix.
For each build-configuration I added an ApplicationInsights.<build-configuration>.config file.
My build-configurations are:
- Debug_PROD
- Debug_TEST
- Release_PROD
- Release_TEST
resulting in the following ApplicationInsights-config-files:
- ApplicationInsights.Debug_PROD.config
- ApplicationInsights.Debug_TEST.config
- ApplicationInsights.Release_PROD.config
- ApplicationInsights.Release_TEST.config
The TelemetrySinks-element of my ApplicationInsights.config looks like this:
<TelemetrySinks>
<Add Name="default">
<TelemetryProcessors>
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector"/>
<Add Type="Microsoft.ApplicationInsights.Extensibility.AutocollectedMetricsExtractor, Microsoft.ApplicationInsights"/>
</TelemetryProcessors>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
</Add>
</TelemetrySinks>
For my ApplicationInsights.Release_PROD.config I used the following transformation, which adds adaptive sampling and reduces the transmitted telemetry items to 5 per second:
<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<!-- In case configuration is not the root element, replace it with root element in source configuration file -->
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<TelemetrySinks>
<Add Name="default">
<TelemetryProcessors>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel" xdt:Transform="Insert">
<MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
</Add>
</TelemetryProcessors>
</Add>
</TelemetrySinks>
</ApplicationInsights>
The resulting TelemetrySinks-element of my ApplicationInsights.config looks like this:
<TelemetrySinks>
<Add Name="default">
<TelemetryProcessors>
<Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryProcessor, Microsoft.AI.PerfCounterCollector"/>
<Add Type="Microsoft.ApplicationInsights.Extensibility.AutocollectedMetricsExtractor, Microsoft.ApplicationInsights"/>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.AdaptiveSamplingTelemetryProcessor, Microsoft.AI.ServerTelemetryChannel">
<MaxTelemetryItemsPerSecond>5</MaxTelemetryItemsPerSecond>
</Add>
</TelemetryProcessors>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
</Add>
</TelemetrySinks>
To run the transformation during the build-process I created the following target in my csproj-file:
<Target Name="TransformApplicationInsights_AfterCopyApplicationInsightsConfigToOutputDirectory" AfterTargets="CopyApplicationInsightsConfigToOutputDirectory" Condition="Exists('ApplicationInsights.$(Configuration).config')">
<Message Text="Transforming ApplicationInsights.config to bin\$(Configuration)\ApplicationInsights.config after CopyApplicationInsightsConfigToOutputDirectory using ApplicationInsights.$(Configuration).config" Importance="high" />
<TransformXml Source="ApplicationInsights.config" Transform="ApplicationInsights.$(Configuration).config" Destination="bin\$(Configuration)\ApplicationInsights.config" />
</Target>
It’s important to use the rarely documented “CopyApplicationInsightsConfigToOutputDirectory” target for the AfterTargets-setting to avoid your transformed ApplicationInsights.config to be overwritten with the original one by the build process.
Have fun!