Have any Question?

You can ask below or enter what you are looking for!

Logging Framework with Kofax TotalAgility

Logging is an essential part of any application and it becomes a critical part of the custom applications built using Kofax TotalAgility. For implementing custom solution in Kofax TotalAgility, .Net Assemblies are utilised and for the logging feature, we do not want to re-invent the wheel by writing from-the-scratch logging components.

Yes, the solution is taking advantage of open-source logging frameworks available in the market and there are plenty !!!

However, choosing a open-source framework that fits in our application is the key factor as these components becomes an integral part and there is no turning back. After a spending sometime on look out suitable for logging frameworks, log4net and NLog were the candidates for us to evaluate.

Both are solid logging frameworks and capabilities are immense. However, there are subtle differences which over weighs one with another. And finally we chose NLog as the logging framework for our product and custom implementations.

Why did we choose NLog over log4net

  1. NLog is setup is seamless.
  2. log4net code base has not been updated for last three years. A question may rise why a framework needs an update if it is stable. The answer is very subjective, however as open-source framework we see more the updates/changes, the code base becomes more stable and it evolves with time.
  3. NLog code base is more cleaner than log4net.

Okay… Now we will setup NLog in a .Net project and it is literally a cake-walk.

Install nuget package “NLog -Version 4.7.0” using following command in Package Manager Console for the desired Visual Studio project.

PM> Install-Package NLog -Version 4.7.0

To enable logging in a custom database, copy the following in the <configuration> section of web.config/app.config and configure the database connection details

 <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" internalLogFile="Nlog.log">
    <targets>
      <target name="file" xsi:type="File"   layout="${longdate}|${level}|${callsite}|${logger}|${threadid}|${windows-identity:domain=false}--${message} ${exception:format=message,stacktrace:separator=*"  fileName="c:\temp\myapplication.log" />
      <target name="database" type="Database">
        <connectionString>
          Data Source=yourservername;Initial Catalog=yourdbname;User Id=dbuser;Password=password;
        </connectionString>
        <commandText>
          insert into acmo_log(time_stamp,level,logger,message,property) values(@time_stamp, @level, @logger, @message, @property);
        </commandText>
        <parameter name="@time_stamp" layout="${date}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@logger" layout="${callsite}" />
        <parameter name="@message" layout="${message}" />
        <parameter name="@property" layout="${event-properties:Property1}" />
      </target>
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" writeTo="file" />
      <logger name="*" minlevel="Debug" appendTo="database" />
    </rules>
  </nlog>

Above configuration will enable NLog to write the log to a text file to ‘c:\temp\myapplication.log’ and as well as in ‘acmo_log’ database table.

Use the following code in the class globally to initialize the Logger object

   private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

NLog supports the following log levels.

  • Trace – very detailed logs, which may include high-volume information such as protocol payloads. This log level is typically only enabled during development
  • Debug – debugging information, less detailed than trace, typically not enabled in production environment.
  • Info – information messages, which are normally enabled in production environment
  • Warn – warning messages, typically for non-critical issues, which can be recovered or which are temporary failures
  • Error – error messages – most of the time these are Exceptions
  • Fatal – very serious errors!

Use the “Logger” object to utilise the different log level methods as mentioned below.

Logger.Debug("ABN is {0}", ABN);
Logger.Error(ex.Message);