控制台托管的WCF服务不跟踪

本文关键字:服务 跟踪 WCF 控制台 | 更新日期: 2023-09-27 18:13:25

我写了一个简单的服务契约(IServiceObject),然后实现了它(ServiceObject)。我将它托管在控制台应用程序中包含的ServiceHost对象中。在我的一个OperationContract方法中,我调用了Trace.WriteLine(...)。我也打电话给Console.WriteLine(...)。在控制台应用程序中,在Open()ServiceHost之前和之后,我调用Trace.WriteLine(...)Console.WriteLine(...)

Trace设置为自动刷新,并有2个监听器(TextWriterTraceListenerConsoleTraceListener)。当控制台应用程序启动时,所有Trace和console WriteLine()调用都被写入各自的日志。因此Trace调用将写入我的文本文件和控制台,console调用将写入我的控制台。

当我的客户端应用程序(一个单独的应用程序)调用OperationContract方法时,控制台屏幕上只显示其中的Console.WriteLine(...)调用。Trace.WriteLine(...)调用不写入控制台屏幕或文本文件。

当我(从OperationContract方法内)查询Trace统计数据(用Console.WriteLine(...)将它们输出到控制台屏幕)时,我被告知Trace(文本和控制台)中有2个侦听器,并且自动刷新打开。

有没有人知道为什么我对Trace.WriteLine(...)的调用只能从OperationContract方法中写入侦听器中的任何一个?是否有一些特定的属性,我需要装饰我的ServiceObject类?有没有我可能遗漏的一些设置?在我看来,跟踪配置正确,因为它可以在任何地方工作,除了在我的OperationContract方法中…

这似乎是一个问题,我的共享库只包含我的OperationContract和它的实现:

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;
        }
    }
}

共享。csproj(我怀疑这是罪魁祸首,但不确定为什么):

<?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>

我构建了一个新的服务器控制台应用程序来托管一个新的共享库和一个新的客户端控制台应用程序来连接它,没有问题。当我将这个项目作为共享库(如上所述)导入时,在ServiceObject.cs中发现的跟踪无法工作。有什么新想法吗?

控制台托管的WCF服务不跟踪

本文描述了如何为WCF启用配置跟踪侦听器/源。有多个部分,如果您错过了其中的一些,我预计跟踪将无法工作。

您尝试过WCF服务跟踪查看器吗?看看这是否对你有帮助