Custom Logging Extensions

K2 provides two a number of standard logging targets which can output K2 log messages to a number of targets. These include:

These logging providers are configured by editing the file HostServerLogging.config, located by default in [Program Files (x86)]\K2 blackpearl\Host Server\Bin.

To learn more about logging in K2, please refer to the following KB article: http://help.k2.com/en/kb000309.aspx. You can also refer to the following K2 Community project for a sample of a custom logging provider that uses e-mail as the logging target: http://community.k2.com/t5/K2-blackpearl/SourceCode-Logging-Plug-in/ba-p/481

In certain scenarios, it may be necessary to create custom logging extensions that will output the logging information from K2 to some other target, perhaps an enterprise-level system monitoring target or a Microsoft Operations Manager environment.

Logging can potentially output a great deal of information, especially if the log level is set to "all", so take care to write efficient and fast code. Although logging does occur asynchronously by default, given the amount of logging it is a good idea to keep your code optimized as much as possible.

Implementation

  1. Create a new .NET Class project.
  2. Add a reference to SourceCode.Logging.dll, located by default in [Program Files (x86)]\K2 blackpearl\Host Server\Bin\ on the K2 application server.
  3. Add using statements to import the SourceCode.Logging namespaces.
  4. Implement a public class that inherits from BaseExtension.
  5. If necessary, implement public properties and private accessors for any configuration settings needed by your logging extension. The properties will be populated by the values you set in the HostServerLogging configuration file, as long as the public property names exactly match the names used in the configuration settings.
  6. Override and implement the LogMessage(MessageObject messageObject, string logStrMsg) method with your logging code.
  7. Build and deploy your custom logging provider as described in the Deployment and Registration section below the code snippet.
// Add reference to SourceCode.Logging from C:\[Program Files]\K2 blackpearl\Host Server\Bin
using SourceCode.Logging;
using SourceCode.Logging.Extension;


namespace ExtendingTheK2Platform.K2_Server {
 // Implement a public class that inherits from BaseExtension
 public class CustomLogExtensions: BaseExtension {
  // If necessary, implement public properties and private accessors for any configuration settings needed by your logging extension.
  private string _configSetting1;
  private string _configSetting2;

  //These settings are included in the <Extension> entry for your custom logger
  //in the format: <Property Name= "Property" value= "Value" />
  public string ConfigSetting1 {
   get {
    return _configSetting1;
   }
   set {
    _configSetting1 = value;
   }
  }

  public string ConfigSetting2 {
   get {
    return _configSetting2;
   }
   set {
    _configSetting2 = value;
   }
  }

  protected override void LogMessage(MessageObject messageObject, string logStrMsg) {
   // TODO: write code to log your message here
   // you can check the message severity level with the MessageSeverity enum
   // levels which are: Debug, Info, Warning and Error
   // Note that the "LogLevel" configuration setting for your custom provider in the HostServerLogging.config file
   // affects which messages are passed into your logging provider build up a message using the properties
   // of the messageObject.
   StringBuilder sb = new StringBuilder();
   sb.Append(" Severity: ");
   sb.Append(messageObject.MessageSeverity.ToString());
   sb.Append(System.Environment.NewLine);
   sb.Append("Source: ");
   sb.Append(messageObject.MessageSource);
   sb.Append(System.Environment.NewLine);
   sb.Append("TimeStamp: ");
   sb.Append(messageObject.MessageTimestamp.ToString());
   sb.Append(System.Environment.NewLine);
   sb.Append("Message: ");
   sb.Append(messageObject.MessageString);
  }
 }
}

Deployment and Registration

The custom logging provider .dll must be copied to the all the physical K2 application servers (in other words, the servers that run the K2 blackpearl service) where the logging provider is used, and must be registered on each K2 application server by editing the HostServerLogging.config configuration file.

  1. Stop the K2 service
  2. Copy the output assembly to the following folder on all K2 Servers: [Program Files (x86)]\K2 blackpearl\Host Server\BIN
  3. Edit the file [Program Files]\\Host Server\Bin\HostServerLogging.config and register the custom logging extension by adding an entry to the <Extensions> node. See a sample below; replace the values in square brackets with the appropriate values for your logging extension.
  4. Enable the logging extension by adding an entry to the <LogLocationSettings> node. See a sample below, and replace the values in square brackets with the appropriate values for your logging extension. Also configure the LogLevel setting to whatever level of Logging messages you wish to output to your logging extension.
    • "Name" must match the <Extension Name> value you used in step 3 above.
    • "Active" enables or disables the logger.
    • "LogLevel" determines the severity of the messages that will be forwarded to the logger. Available LogLevels are "All", "Debug", "Info", "Warning" and "Error".
  5. Start the K2 service and test your logging provider.
<!--
Stop the K2 service
Copy the output assembly to the following folder on all K2 Servers: [Program Files (x86)]\K2 blackpearl\Host Server\BIN
Edit the file [Program Files]\\Host Server\Bin\HostServerLogging.config and register the custom logging extension by adding an entry to the <Extensions> node. See a sample below; replace the values in square brackets with the appropriate values for your logging extension:
-->
<Extension Name="[SomeName]" type="[LoggerNamespace.LoggerClass]" location="C:\Program Files (x86)\K2 blackpearl\Host Server\Bin\[DLLName].dll">
  <Property Name="[SomeProperty1]" value="[SomeValue1]"/>
  <Property Name="[SomeProperty2]" value="[SomeValue2]"/>
</Extension>

<!--
Enable the logging extension by adding an entry to the <LogLocationSettings> node. See a sample below, and replace the
values in square brackets with the appropriate values for your logging extension.
-->
<LogLocation Name="[SomeName]" Active="True" LogLevel="All" />

If you make any changes to the HostServerLogging.config file, you need to restart the K2 service to pick up the changes.

Debugging

You can debug custom logging extensions through remote debugging and attaching to the K2 server process. If you are debugging a remote K2 server, you will need to enable remote debugging. See http://msdn.microsoft.com/en-us/library/vstudio/y7f5zaaa.aspx for more information. If you are running a multi-K2 server environment (for example, when using NLB) we recommend that you shut down all but once of the K2 servers so that only one K2 server will run the logging extension.

  1. Add breakpoints to your code
  2. Copy the logging extension assembly and .pdb file to the K2 host server as described in the Deploying and Registration section
  3. Modify the logging configuration file HostServerLogging.configand set the AsyncQueueEnabled value to False. <add key="AsyncQueueEnabled" value="False" />. This will ensure that your logging extensions is executed at the moment the event is encountered, rather than putting the event into an asynchronous log-processing queue.
  4. Start the K2 service and then use Visual Studio's Debug > Attach to process option to attach to the K2HostServer.exe process. You may need to check the options Show Processes from all users and Show Processes in all sessions before the K2HostServer.exe process will show up.
  5. When K2 encounters a logging event which is configured to be output to your custom logging extension, K2 Studio should launch and you can step through and debug your code.
  6. When you are done debugging your logging extension, modify the logging configuration file HostServerLogging.configand set the AsyncQueueEnabled value to True to re-enable asynchronous logging, otherwise the performance of your K2 environment may be affected.

Attaching to the K2 host server process and hitting a breakpoint will suspend all execution on the K2 server, so use this approach carefully in any shared K2 environment. It is not recommended that you debug a production K2 server, unless downtime has been scheduled.

K2 blackpearl Developers Reference4.7
Video Links Learn Support
No videos found for this article
K2 on YouTube
No Additional links found for this article
No self-learning content for this article
Try some scenarios...
No relevant support links available for this article