简体   繁体   中英

Console hosted WCF service does not Trace

I have written a simple service contract ( IServiceObject ) and then implemented it ( ServiceObject ). I host it in a ServiceHost object that is contained within a console application. Within one of my OperationContract methods I call Trace.WriteLine(...) . I also call Console.WriteLine(...) . Within the console application, before and after I Open() the ServiceHost , I call Trace.WriteLine(...) and Console.WriteLine(...) .

Trace is set to autoflush, and has 2 listeners ( TextWriterTraceListener and ConsoleTraceListener ). When the console application fires up, all Trace and Console WriteLine() calls are written to their respective logs. So the Trace call will write to my text file and to my console and the Console call will write to my console.

When my client application (a separate application) calls the OperationContract method, only the Console.WriteLine(...) calls within it are displayed on the console screen. The Trace.WriteLine(...) calls are not written to the console screen or the text file.

When I query (from within the OperationContract method) for the Trace stats (outputting them to the console screen with Console.WriteLine(...) ) I am told that there are 2 listeners in Trace (Text and Console) and that autoflush is on.

Does anyone know why my calls to Trace.WriteLine(...) would fail to write to either of the listeners only from within the OperationContract method? Are there some specific attributes I need to decorate my ServiceObject class with? Are there some settings I might be missing somewhere? It seems to me that the Tracing is configured correctly since it works everywhere except from within my OperationContract methods...

This seems to be problem, my shared library that contains only my OperationContract and its implementation:

IServiceObject.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;

namespace SuccessEHS.Dev.Shared
{
    [ServiceContract]
    public interface IServiceObject
    {
        [OperationContract]
        bool Test(string text);
    }
}

ServiceObject.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.ServiceModel;

namespace SuccessEHS.Dev.Shared
{
    public class ServiceObject : IServiceObject
    {
        public bool Test(string text)
        {
            Console.Write("Testing 1");
            Trace.Write("..2");
            Console.Write("..3");
            Trace.Write("..4");
            Console.WriteLine("..5");

            Console.WriteLine("CW: {0}", text);
            Trace.WriteLine(string.Format("TW: {0}", text));

            return true;
        }
    }
}

Shared.csproj (which I suspect is the culprit but am not sure why):

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{23F9A333-9CC8-43FA-8A01-06BEA8B9D0E6}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Shared</RootNamespace>
    <AssemblyName>SharedTest</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <TargetFrameworkProfile />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <CodeAnalysisLogFile>bin\Debug\Shared.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>
    <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
    <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
    <CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
    <CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
    <CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <OutputPath>bin\x86\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <CodeAnalysisLogFile>bin\Release\Shared.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile>
    <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression>
    <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories>
    <CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
    <CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories>
    <CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

I built a new server console app to host a new shared library and a new client console app to connect to it and had no issues. When I then imported this project as the shared library (above) the Tracing failed to work that was found in ServiceObject.cs. Any new ideas?

This article describes how to enable and configure trace listeners/sources for WCF. There are multiple pieces, and if you miss some of them I expect tracing would not work.

Have you tried the WCF Service Trace Viewer . See if this provides you with any assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM