ThingWorx Edge .NET SDK > ThingWorx Edge .NET SDK Reference > Using Logging with the ThingWorx Edge .NET SDK
  
Using Logging with the ThingWorx Edge .NET SDK
The ThingWorx Edge .NET SDK contains logging methods that provide a mechanism for writing data to text file, and is designed to isolate logging by class to log only what is of interest. The .NET SDK uses the .NET Framework logging mechanism. This topic includes the following information:
Logging Levels
Logging Implementation
Example 17. Log Message Examples
Logging Configuration
Example 18. Log Configuration Example
Logging Levels
The following logging levels are supported (in descending order):
Verbose
Information
Warning
Error
Critical
Choosing a particular logging level for an application also includes any messages that are written out at a lower level. For example, if the logging level for an application is set to Warning, then the Error and Critical levels are also written to the log, and Verbose includes all messages from all levels.
Back to top
Logging Implementation
The following classes and methods are used to implement logging in C# code:
LoggerFactory — The LoggerFactory class is a helper class that simplifies logging, rather than having to create the logging from scratch.
TraceSource getLogger(Type type) — The getLogger method returns a System.Diagnostics.TraceSource that the LoggerFactory uses to perform logging for a specific class. This is usually done on a static non-modifiable member variable.
For example, the following creates a TraceSource based on the SteamThing class:
private static readonly TraceSource LOG =
LoggerFactory.getLogger(typeof(SteamThing));
To log a simple message:
Log(TraceSource tracer, TraceEventType eventType, string message)
Back to top
Example 17. Log Message Examples
Consider the following examples:
This example writes a Verbose Message:
LoggerFactory.Log(LOG, TraceEventType.Verbose, "Initialization Complete");
This example writes a Warning Message that logs a message with parameters:
LoggerFactory.Log(LOG, TraceEventType.Warning,
"Invalid data was entered");
Log(TraceSource tracer, TraceEventType eventType,
string format, params object[] args)
This example writes an Information Message:
LoggerFactory.Log(LOG, TraceEventType.Information,
"Thing {0} started at {1}", thing.getName(), DateTime.Now);
This example writes a Warning Message and logs a message with an exception:
LoggerFactory.Log(LOG, TraceEventType.Warning,
"Thing {0} has exited with code {1}", thing.getName(), code);
Log(TraceSource tracer, TraceEventType eventType, string message,
Exception x)
This example writes an Error Message:
LoggerFactory.Log(LOG, TraceEventType.Error,
"SteamSensor exited.", x);
This example writes a Critical Message that logs only exceptions:
LoggerFactory.Log(LOG, TraceEventType.Critical,
"Memory overrun, exiting", x);
Log(TraceSource tracer, TraceEventType eventType,
Exception x)
Back to top
Logging Configuration
You can configure the .NET SDK to generate very detailed debugging and diagnostic information to a log file during execution. Since the .NET SDK uses the standard .NET System.Diagnostic infrastructure for logging, you configure the logger of the SDK, using the standard .NET logging configuration system.
Here are some important notes about configuring logging for the .NET SDK:
By default, logging is configured using the standard .NET “ App.config” file.
Log messages can be routed to any standard .NET TraceListener. Optionally, the ThingWorx platform provides a FixedFieldTraceListener that you can use to output log messages to a file. It is recommended that you use the FixedFieldTraceListener that the platform provides.
When you configure it, the FixedFieldTraceListener automatically creates a logs directory in the same location as (a sibling to) the running executable file (.exe). This logs directory contains the log files at run time.
Every .NET class can be configured as a specific Trace Source that generates log messages. It is recommended that you add at least the following Trace Sources to your App.config file to receive the most useful amount of information:
com.thingworx.communications.client.BaseClient
com.thingworx.communications.client.ConnectedThingClient
com.thingworx.communications.client.things.VirtualThing
com.thingworx.communications.client.TwApiWrapper
com.thingworx.communications.client.things.filetransfer.FileTransferVirtualThing
com.thingworx.communications.client.things.contentloader.ContentLoaderVirtualThing
The amount of information that is generated can range from very low level Trace messages (the Verbose setting) to nothing at all (the Off setting). The “ SourceLevels Enumeration” can be used to control how much information is written out to the log file. For reference, this is the <add name="SourceSwitch" value="Information" /> element in the example below.
* 
The entire namespace name must be entered for the correct logging source to be used. Note that the console listener has been disabled in the example below.
Back to top
Example 18. Log Configuration Example
An example of the App.config file is provided here, but a detailed explanation of the entire file is beyond the scope of this document. For complete details, see the .NET Framework documentation.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.diagnostics>
<sources>
<source name="com.thingworx.common.utils.JSONUtilities"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.TwApiWrapper"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.BaseClient"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch">
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.ConnectedThingClient"
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.things.contentloader.
ContentLoaderVirtualThing"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.things.filetransfer.
FileTransferVirtualThing"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.communications.client.things.VirtualThing"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="file" />
</listeners>
</source>
<source name="com.thingworx.metadata.annotations.client.
MetadataAnnotationParser"
switchName="SourceSwitch"
switchType="System.Diagnostics.SourceSwitch" >
<listeners>
<add name="file" />
</listeners>
</source>
</sources>
<switches>
<add name="SourceSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="file"
type="com.thingworx.common.logging.FixedFieldTraceListener,
thingWorx-dotnet-common, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" initializeData="false"/>
</sharedListeners>
<trace autoflush="true" indentsize="4" />
</system.diagnostics>
</configuration>
Back to top