When using BIML within BIDS Helper, if your BIML Script files get complex it may be quite hard to debug BIML Script Execution in order to understand what’s going on behind the scenes.
On Windows Server 2008 it was possible to use the System.Diagnostic.Debug.WriteLine and DebugViewer from SysInternal to do the trick but it seems that this approach doesn’t work anymore on Windows Server 2012. Or, at least, I wasn’t able to make it work. Anyway, in addition to that, I was also trying to have everything logged to a file so one doesn’t have to use and configure DebugViewer to do its job. DebugViewer is great tool, but it’s not really suitable for junior developers.
So I tried to use the fantastic NLog framework in order to create a “standard” way of logging BIML. First of all download it (just get the standard version, no need to grab the “Extended” one) and unpack into a folder named “NLog-3.0” where you prefer.
Now in your SSIS Solution create a BIML file named “BimlLogger.biml” and copy and paste the following code:
<#@ template language="C#" #>
<#@ assembly name="C:\Work\Lab\BIML-Debug\NLog\NLog-3.0\net40\NLog.dll" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="NLog" #>
<#@ import namespace="NLog.Config" #>
<#@ import namespace="NLog.Targets" #>
<#
Logger logger = LogManager.GetLogger("BIML");
LoggingConfiguration config = new LoggingConfiguration();
FileTarget fileTarget = new FileTarget();
config.AddTarget("file", fileTarget);
fileTarget.FileName = @"${nlogdir}\..\..\biml.log";
fileTarget.Layout = "${longdate}|${level:uppercase=true}|${message}";
fileTarget.DeleteOldFileOnStartup = true;
LoggingRule loggingRule = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(loggingRule);
LogManager.Configuration = config;
#>
Change the second line (the “assembly name” line) in order to reflect the path where you unpacked NLog and then you can start to log anything you think it can help you just referencing this file from any other BIML files and then using the “logger” object to write to the log file. Here’s an example of a BIML script file that creates a test package.
<#@ template language="C#" #>
<#@ include file="BimlLogger.biml" #>
<#
logger.Info("Generating Package...");
#>
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Packages>
<Package Name="Test" ConstraintMode="Linear" ProtectionLevel="EncryptSensitiveWithUserKey" />
</Packages>
</Biml>
<#
logger.Info("Done.");
#>
The log file will be created in the “NLog-3.0” folder you created before. Of course you can change this and many other option, since NLog is really flexible and powerful. Documentation and tutorial are here: https://github.com/nlog/nlog/wiki