I have been using both build time web.config
transformation with SlowCheetah NuGet package and built-in deployment time transformation. For NLog.config
files I have NLog.*.config
files for each build environment. Previously each of those files had full NLog.config
contents, and had a task in deployment to overwrite NLog.config
with a specific NLog.*.config
and delete all NLog.*.config
afterwards.
Decided today to have these files to be transformable instead, similar to the way web.config
is generated from web.template.config
and web.template.*.config
(with the help of ProjectName.wpp.targets
file), but not replacing NLog.config
at build time (don't want to have servers' log file paths in my localhost runs).
Here's how I've got it working - in ProjectName.wpp.targets
file I've used OnAfterCopyAllFilesToSingleFolderForPackage
"event" to transform NLog.config
file into the temporary intermediate directory (which is subsequently used for publishing). This is the full ProjectName.wpp.targets
file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Make sure Web.config will be there even for package/publish -->
<Target Name="CopyWebTemplateConfig" BeforeTargets="PrepareForBuild">
<Copy SourceFiles="Web.template.config"
DestinationFiles="Web.config"/>
</Target>
<PropertyGroup>
<PrepareForRunDependsOn>
$(PrepareForRunDependsOn);
UpdateWebConfigBeforeRun;
</PrepareForRunDependsOn>
</PropertyGroup>
<PropertyGroup>
<OnAfterCopyAllFilesToSingleFolderForPackage>
$(OnAfterCopyAllFilesToSingleFolderForPackage);
UpdateNLogConfigBeforePublish;
</OnAfterCopyAllFilesToSingleFolderForPackage>
</PropertyGroup>
<!-- This target will run right before you run your app in Visual Studio -->
<Target Name="UpdateWebConfigBeforeRun">
<Message Text="Configuration: $(Configuration): Web.template.$(Configuration).config"/>
<TransformXml Source="Web.template.config"
Transform="Web.template.$(Configuration).config"
Destination="Web.config" />
</Target>
<Target Name="UpdateNLogConfigBeforePublish">
<Message Text="Configuration: $(Configuration): NLog.$(Configuration).config"/>
<TransformXml Source="NLog.config"
Transform="NLog.$(Configuration).config"
Destination="$(IntermediateOutputPath)\Package\PackageTmp\NLog.config" />
</Target>
<!-- Exclude the config template files from the created package -->
<Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
<ItemGroup>
<ExcludeFromPackageFiles Include="Web.template.config;Web.template.*.config"/>
<ExcludeFromPackageFiles Include="NLog.*.config"/>
</ItemGroup>
<Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
</Target>
</Project>
The NLog.*.config
files then use the standard transformations, e.g.:
<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
internalLogFile="c:\temp\ProjectName.ENV.nlog.txt"
xdt:Transform="SetAttributes(internalLogFile)">
<variable name="envName" value="ENV"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
<targets>
<target name="exceptionsMail" to="[email protected]"
xdt:Transform="SetAttributes(to)" xdt:Locator="Match(name)" />
</targets>
<rules>
<logger name="Exceptions" minlevel="Error" writeTo="exceptionsMail"
xdt:Transform="Insert" />
</rules>
</nlog>