Click or drag to resize

Logging & Tracing via Open Telemetry

With the release of V5.0 SphinxConnector.NET has replaced the Common.Logging library with its own logging abstraction. NuGet packages for following logging frameworks are available:

Setting up Logging

This section is intended to be a quick introduction on how to setup logging in your application. In this example we'll use NLog. To have SphinxConnector.NET use NLog we'll first install the SphinxConnector.Logging.NLog NuGet package and add the following line to our application startup class:

C#
SphinxConnectorLoggerFactory.SetLoggerProvider(new NLogLoggerPovider());

NLog can be configured either in an app or web.config file or programmatically. In this example we'll use the app.config file. First we need to add the config section for NLog to the config file:

Config sections
<configSections>
  <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>

Next we add the NLog configuration:

NLog config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false">
    <targets>
      <target name="console" xsi:type="Console" layout="${longdate} ${level:uppercase=true} ${callsite} ${message} ${exception:format=tostring}"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="console"> </logger>
    </rules>
</nlog>

The NLog configuration is straightforward: We setup a console target and a catch-all logger with a mininum log level of Trace.

Note Note

In a production environment the log level generally should not be set lower than Info except for debugging puposes, because Trace is very verbose and may slow down your application.

OpenTelemetry

To enable tracing via OpenTelemetry, call the AddSource method with "SphinxConnector" as the soure name:

C#
builder.Services.AddOpenTelemetry().
                 WithTracing(tracing =>
                 {
                    tracing.AddSource("SphinxConnector");
                 });