次の方法で共有


Where are the log files? Enable tracing in .NET WCF LOB Adapter

Tracing can come in quite handy during diagnostics and testing an application. The concept of tracing is not new and in Windows Communication Foundation (WCF), tracing is built on top of System.Diagnostics. Windows Communication Foundation (WCF) provides tracing at both transport and application level. The Windows SDK also provides a tool called SvcTraceViewer that helps view these trace files. Tracing is NOT enabled by default. You can configure tracing using the application’s configuration file – either web.config for Web hosted applications or application.exe.config (app.config) for self-hosted applications.

Adapter Developer

The Adapter Developer needs to implement instrumentation in the code. The Adapter Development Wizard generates a trace helper class that can be used to send trace messages.

    // Use HelloWorldAdapterUtilities.Trace in the code to trace the adapter

    public class HelloWorldAdapterUtilities

    {

        static AdapterTrace trace;

        static HelloWorldAdapterUtilities()

     {

       // “HelloWorldAdapter” is the Trace Source

            trace = new AdapterTrace("HelloWorldAdapter");

        }

        public static AdapterTrace Trace

        {

            get

            {

                return trace;

            }

        }

    }

Use the following method at appropriate places in the code with correct TraceEventType to provide trace information at run-time.

HelloWorldAdapterUtilities.Trace.Trace(System.Diagnostics.TraceEventType, "eventCode", "description");

For example –

HelloWorldAdapterUtilities.Trace.Trace(TraceEventType.Error, “CONNECTION_ERROR”, “Unable to establish connection with the target application”);

The developer must follow logging best practices when providing these instrumentation points. In the end, the generated log file should provide useful information for troubleshooting and diagnosing production problems.

Adapter Consumer

The Adapter Consumer can enable tracing for the following trace sources by specifying diagnostics configuration information in the client application configuration file. The tracing can be enabled for the following components:

· Level 2: Custom Adapter Tracing

· Level 1: WCF LOB Adapter SDK Tracing

· Level 0: WCF Tracing

Basic Tracing Terminology

Let’s understand the basic tracing terminology, before we show how to configure tracing. It’s fairly standard and more information can be obtained about this in other sites and blogs.

Trace Source

Use trace source represents an application component and is responsible for generating trace events and trace data. It is typically represented by an application name / assembly name.

 

 

   

Component 

Custom Adapter

WCF LOB Adapter SDK

Trace Source Name

Name passed in the constructor of AdapterTrace

Microsoft.ServiceModel.Adapter

WCF

System.ServiceModel, System.ServiceModel.Messaging

Trace Switch

A trace switch lets the user control the output through switches.

  • Critical
  • Error
  • Warning
  • Information
  • Verbose 

Trace Listener

A trace listener is an object that listens on a particular trace source, receives output from it and persists that output somewhere – a text log file, an Xml Log file, Event Log, Database or any other customized data store.

 

It typically needs three parameters –

1) Trace Listener Type: Refers to a .NET type where listener logic is defined.

2) Trace Listener Name: Defines the name of a listener.

3) Initialize Data: Additional information for the listener such as file path.

 

.NET ships with some built-in trace listeners such as DefaulTraceListener, TextWriterTraceListener, XmlWriterTraceListener and EventLogTraceListener.

 

Here is a sample configuration file that shows how to enable tracing in general.

<configuration>

  <system.diagnostics>

    <sources>

      <source name="{Trace Source}" switchValue="{Trace Switch}, ActivityTracing">

        <listeners>

          <add type="{Trace Listener Type} name="{Trace Listener Name}"/>

        </listeners>

      </source>

    </sources>

  </system.diagnostics>

</configuration>

If the user wants to use the same listener for more than one source, a Shared Trace Listener can be used. See example below.

<configuration>

  <system.diagnostics>

    <sources>

      <source name="{Trace Source}" switchValue="{Trace Switch}, ActivityTracing">

        <listeners>

          <add name="{Shared Trace Listener Name}"/>

        </listeners>

      </source>

    </sources>

   <sharedListeners>

      <add initializeData="{Log File}.svclog" type="{Trace Listener Type}" name="{Shared Trace Listener Name}">

        <filter type="" />

      </add>

    </sharedListeners>

    <trace autoflush="true" />

  </system.diagnostics>

</configuration>

 Enable Tracing in .NET Proxy Project

The following table shows an example diagnostics configuration file that enables logging for all the three components. It uses a shared listener of type XmlWriterTraceListener to log errors in an XML based log file. Once the adapter consumer has used the "Add Adapter Service Reference" plug-in to generate the C# proxy metadata files, an app.config is also generated. Update the app.config to include appropriate diagnostics information.

 

  <system.diagnostics>

    <sources>

Enable Custom Adapter Tracing

      <source name="HelloWorldAdapter" switchValue="Verbose, ActivityTracing">

        <listeners>

          <add name="xmlTrace"/>

        </listeners>

      </source>

Enable WCF LOB Adapter SDK Tracing

      <source name="Microsoft.ServiceModel.Adapters" switchValue="Verbose, ActivityTracing">

        <listeners>

          <add name="xmlTrace"/>

        </listeners>

      </source>

Enable WCF Tracing

      <source name="System.ServiceModel" switchValue="Error" propagateActivity="true">

        <listeners>

          <add name="xmlTrace" />

        </listeners>

      </source>

      <source name="System.ServiceModel.MessageLogging" switchValue="Verbose">

        <listeners>

          <add name="xmlTrace" />

        </listeners>

      </source>

    <sharedListeners>

      <add initializeData="C:\logs\alog.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xmlTrace">

        <filter type="" />

      </add>

    </sharedListeners>

    <trace autoflush="true" />

  </system.diagnostics>

 

Once the tracing is enabled and the source is activated, the trace listener will pick the trace events generated by the application and output them in the log file. This log file can then be viewed using SvcTraceViewer tool.

Enabling tracing within BizTalk Server

If an Adapter Consumer is using the WCF Adapter to consume a WCF LOB Adapter, tracing can still be enabled in the adapter by providing diagnostics information in the BTSNTSVC.exe.config file.

Enabling tracing for Add Adapter Service Reference Visual Studio Plug-In/Consume Adapter Service BizTalk Project Add-In

The tracing for the tools can be enabled by putting the diagnostics secion in the DEVENV.exe.config. The trace source name is "Microsoft.ServiceModel.Channels.Tools.MetadataSearchBrowse". 

      <source name="Microsoft.ServiceModel.Channels.Tools.MetadataSearchBrowse" switchValue="Verbose, ActivityTracing">
        <listeners>
          <add name="xmlTrace"/>
        </listeners>
      </source>

Enjoy tracing!

Comments

  • Anonymous
    April 21, 2007
    After WCF LOB Adapter SDK is installed on the system, the Adapter Developer can use the WCF LOB Adapter