【问题标题】:Console hosted WCF service does not Trace控制台托管的 WCF 服务不跟踪
【发布时间】:2011-11-04 04:18:21
【问题描述】:

我写了一个简单的服务契约 (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 方法中)跟踪统计信息(使用 Console.WriteLine(...) 将它们输出到控制台屏幕)时,我被告知在跟踪(文本和控制台)中有 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;
        }
    }
}

Shared.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 中发现的跟踪失败。有什么新想法吗?

【问题讨论】:

  • 您能否提供一个最小的代码示例来重现该问题?我猜这不是必需的(因为我想这是某种跟踪配置问题),但它可能对人们阅读和回答问题有所帮助。
  • 我将开始一个具有最少代码的新项目,看看是否可以对其进行跟踪。写完我就贴出来。

标签: c# wcf servicehost


【解决方案1】:

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

【讨论】:

  • 我不想启用 WCF 源。我阅读了这篇文章,但它没有提供解决此问题的线索。我只想知道为什么当我在托管对象中使用 .net Trace 类时,它不会产生任何跟踪输出,即使自对象托管以来配置没有更改。并且托管对象的程序没有跟踪问题,即使在托管对象时也是如此。不过,关于设置 WCF 源的好文章。 (注意:当我启用 WCF 源时,我得到了它们的所有跟踪,但仍然没有 Trace.WriteLine(...) 跟踪)
  • @Schrader:无赖。当您发布代码时,我会在 OP 上查看您的代码。
  • 是的,这让我大吃一惊,我太固执了,不能在没有某种答案的情况下放弃,即使答案是给 MS 的错误报告:P
【解决方案2】:

您是否尝试过 WCF Service Trace Viewer。看看这是否能为您提供任何帮助。

【讨论】:

  • 我在查看来自 WCF 侦听器的数据时使用 ServiceTraceViewer。但是,跟踪侦听器没有显示任何内容,甚至在托管应用程序中运行的跟踪也不显示。虽然我不认为它会出现在那里,因为那些是未链接到 System.Diagnostics.Trace 侦听器的侦听器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多