REQUEST A DEMO

Configuring logging in the fcSDK without a config file

I banged my head against this for a while today, so I figured a post was in order.

I was writing a PowerShell script to re-create a customer scenario today, and I needed to enable debug level logging.

I was building the config on the fly, such as this:

$config = new-object -typename System.Collections.Specialized.NameValueCollection
$config.Add("fchoice.connectionstring",$connectionString);
$config.Add("fchoice.dbtype",$databaseType);
$config.Add("fchoice.disableloginfromfcapp", "false");

$ClarifyApplication = [Fchoice.Foundation.Clarify.ClarifyApplication]

if ($ClarifyApplication::IsInitialized -eq $false ){
$ClarifyApplication::initialize($config) > $null;
}

I created a logging.config file that looks like:

Then added to my config:

$config.Add("fchoice.logconfigfile", "C:\customers\test\logging.config"); 

I ran my script, but I didn’t get a log file.

Something is wrong

I went back to the fcSDK docs on logging configuration, and realized what was happening.

The LogManager is a singleton, and only gets its LogConfigFilePath set upon initialization. So, it has to be set before its initialized. However, the fcSDK does logging when parsing the config values. So you end up with a bit of a chicken and an egg problem. Hence the reason we expose the LogConfigFilePath property seperately, and document that it needs to be set before initializing ClarifyApplication.

The LogConfigFilePath property of LogManager needs to be programatically set before initializing ClarifyApplication.

So, I needed to do this before calling ClarifyApplication.Initialize:

$LogManager = [FChoice.Common.LogManager]
$LogManager::LogConfigFilePath = "C:\customers\test\logging.config"
$LogManager::Reconfigure()

Note that you must call Reconfigure in order for the changes to take effect, as setting the LogConfigFilePath Property will not cause LogManager to reconfigure itself immediately.

Here’s the end result:

[system.reflection.assembly]::LoadWithPartialName("fcsdk") > $null

$config = new-object -typename System.Collections.Specialized.NameValueCollection
$config.Add("fchoice.connectionstring",$connectionString);
$config.Add("fchoice.dbtype",$databaseType);
$config.Add("fchoice.disableloginfromfcapp", "false");

$ClarifyApplication = [Fchoice.Foundation.Clarify.ClarifyApplication]

$LogManager = [FChoice.Common.LogManager]
$LogManager::LogConfigFilePath = "C:\customers\test\logging.config"
$LogManager::Reconfigure()

if ($ClarifyApplication::IsInitialized -eq $false ){
   $ClarifyApplication::initialize($config) > $null;
}

$ClarifySession = $ClarifyApplication::Instance.CreateSession()

It now creates a log file, just as I wanted. This same scenario will apply not only to PowerShell scripts, but to any .NET app (such as C# or VB.NET) that uses the fcSDK.

Hopefully this will save you from some frustration if you run into this scenario.