Collecting Information
Edit this article in GitHub
Version 2.3

Collecting Information

The logging infrastructure in the .NET SDK depends on which .NET runtime you're targerting. For applications that are targeting .NET Framework 4.5+ the SDK uses the Apache Common Infrastructure Libraries and applications that are targeting .NET Core the SDK uses Microsoft.Extensions.Logging.

Logging in applications targeting .NET Framework 4.5+

The Apache Common Infrstucture libraries provides a common interface for logging adapters. The SDK currently uses version 3.3.1 of Common.Logging.

In order to use logging within the SDK, you need to reference the packages for the adapter you want to use. There are adapaters available for all the major logging implementations. More information and a list of available adapters is available on the project website here.

The following steps describe the process for using the Log4Net Adapter with the SDK:

  1. Using the NuGet Package Manager in Visual Studio, include the following package in your project: Common.Logging.Log4Net1213
  2. In your App.Config or Web.Config, add the following elements between the <configuration> and </configuration> tags so that your configuration looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <sectionGroup name="common">
          <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
    
      <common>
        <logging>
          <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1213">
            <arg key="configType" value="INLINE" />
          </factoryAdapter>
        </logging>
      </common>
    
      <log4net>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
          <param name="File" value="C:\temp\log.txt" />
            <layout type="log4net.Layout.PatternLayout">
              <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
        <root>
          <level value="DEBUG" />
            <appender-ref ref="FileAppender" />
        </root>
      </log4net>
      
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
      </startup>
    </configuration>                
    Note: This example creates a FileAppender. There's a wide variety of possible appenders and configuration options you can customize to your liking. For more information regarding customizing your configuration, check out the Log4Net documentation.

Logging in applications targeting .NET Core

As part of the new .NET Core runtime, Microsoft have added a new abstraction interface. This is conceptually very similar to the Common.Logging, however has additional support built in for ASP.NET 5 and other Microsoft libraries.

To get logging to work within the .NET SDK, you need will need to add a package for the adapater to connect with. There are a number of logging implementations based on Mictosoft.Extensions.Logging that can be viewed here. There are also some basic Microsoft provided adapaters such as Debug, Console and EventLog.

The following instructions show how to get up and running with the NLog adapater with the SDK in an ASP.NET 5 web application:
  1. Add the following two dependencies to your project.json:
    "dependencies": {
      "NLog.Extensions.Logging": "1.0.0-*",
      "CouchbaseNetClient": "2.4.0"
    }
  2. Create a nlog.config in the root of your project, an example config could look like this:
    <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          autoReload="true"
          internalLogLevel="Warn"
          internalLogFile="c:\temp\internal-nlog.txt">
    
      <!-- define various log targets -->
      <targets>
        <!-- write logs to file -->
        <target xsi:type="File" name="ownFile-web" fileName="c:\temp\ownFile-web-${shortdate}.log"
                     layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
       
        <target xsi:type="File" name="couchbase" fileName="c:\temp\couchbase-${shortdate}.log"
                 layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />
      </targets>
    
      <rules>
        <logger name="Couchbase.*" minlevel="Trace" writeTo="couchbase" final="true" />
        <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
      </rules>
    </nlog>
  3. in startup.cs updateConfigure with the following:
    using NLog.Extensions.Logging;
    using Couchbase.Logging;
    
      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
          loggerFactory.AddNLog();
          LogManager.ConfigureLoggerFactory(loggerFactory);
          ...

More details on configuring NLog with Microsoft.Extensions.Logging can be found here.