Using Log4Net Nuget Package in SSIS
Monday, January 19, 2015
Log4Net is a robust logging framework that provides programmers granular control over logging to a variety of target outputs (examples include file, console, email, etc.). Detailed documentation can be found on the Apache Log4Net site.
It is possible to use Log4Net within SSIS script tasks/components, but the implementation is a bit different than when used within console or web applications.
1. Create or open an SSIS script task or component. Click ‘Edit Script’.
2. In the Solution Explorer window, right click on the solution and select ‘Manage NuGet Packages’.
If you do not see the option ‘Manage NuGet Packages’, then you will need to install the package manager extension. This can be done from within Visual Studio.
Go to Tools > Extension Manager.
Search the online gallery for ‘NuGet Package Manager’ and install.
3. In the ‘Nuget Package Manager’ window, click ‘Online’ in the left navigation pane and then search for Log4Net. Click ‘Install’ and then Close.
You should now see a Packages.config file added to your project. The Packages.config file stores the version information for the nuget package(s) installed. The following is an example:
When a package is restored, Nuget references the Packages.config file to pull down the correct version of the package (in this case version 2.0.3).
4. In addition to adding the package via Nuget within the SSIS script task, the Log4Net.dll file will also need to be added to the GAC. There are several ways to add a dll to the GAC, Powershell is one option:
[System.Reflection.Assembly]::Load(“System.EnterpriseServices, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)
$publish = New-Object System.EnterpriseServices.Internal.Publish
5. Now we have reached the point where we can start writing some code. Generally, all Log4Net configurations are set in the App.config file or a custom Log4Net config file.
Since the DTEXEC.EXE application runs the SSIS packages, it is not possible to directly associate config files to the package script task/component. Therefore, all Log4Net configuration settings must be set programmatically.
I used the following StackOverflow post as a reference: http://stackoverflow.com/questions/2738130/using-log4net-as-a-logging-mechanism-for-ssis
First, add the following log4net references to the Namespaces
Now add a new member of type log4net.ILog to the Script class
Create a new method called InitLogger
Now add some logging to your main method. In this example all levels of severity are being logged to the file h:\ssis\TestLog_.log
the line of code logger.Info(“Test Log Message”) writes a single line of output to the logging file.
You can adjust the severity level by calling the appropriate method. This is a code sample from www.beefycode.com
"I'm a simple log4net tutorial."
"... better be careful ..."
"ruh-roh: an error occurred"
"OMG we're dooooooomed!"
There are a few other nuances to be aware of. When you close and re-open your SSIS project, you will need to restore the Log4Net Nuget package in the script task/component. To accomplish this, simply re-open the Nuget Package Manager. You will see a message stating ‘Some NuGet packages are missing from this solution. Click to restore from your online package sources’. Click ‘Restore’ in the upper right corner. The Nuget Package Manager will restore the same version of the package referenced in the Packages.config file.
If you want to Update to a newer version of the package, click the ‘Updates’ option in the left navigation pane. All available updates should be displayed.