.Net自定义TraceListener.TraceEvent事件未触发

6
我们有一个自定义的TraceListener(继承自System.Diagnostics.TraceListener),用于我们的ASP.NET Web应用程序记录日志。一直以来都表现良好,从未出现问题。但突然间在我们的开发环境中停止工作了(TraceListener.TraceEvent()不再触发)。我们对它突然停止工作感到困惑。我们所做的唯一更改是增加了更多的构建配置(Dev、Test、Stage、Prod),之前只有Debug和Release。
我注意到,当我使用Debug配置进行本地测试时,TraceListener.TraceEvent()能够正常触发。但当我切换到其他构建配置(例如Test)时,TraceEvent()就不再被触发了。这是我的Web .csproj文件的一部分:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE;DEBUG;SkipPostSharp</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <ExcludeGeneratedDebugSymbol>false</ExcludeGeneratedDebugSymbol>
  <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Dev|AnyCPU'">
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Test|AnyCPU'">
  <DebugSymbols>true</DebugSymbols>
  <DebugType>full</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE;DEBUG;SkipPostSharp</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <ExcludeGeneratedDebugSymbol>false</ExcludeGeneratedDebugSymbol>
  <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Stage|AnyCPU'">
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Prod|AnyCPU'">
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
  <OutputPath>bin\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>

我不确定为什么切换构建配置似乎会关闭我们的日志记录。有人能给我指点吗?
1个回答

6

问题已经解决。我的代码调用了System.Diagnostics.TraceSource.TraceEvent()方法来广播跟踪事件,但是这段代码在另一个项目中。我需要通过以下方式在那个项目上启用跟踪:

<DefineConstants>TRACE</DefineConstants>

我错误地认为我只需要关注主要目标项目(即Web项目)。现在才明白 - 调用System.Diagnostics.TraceSource.TraceEvent()的项目/程序集必须启用跟踪(通过针对目标构建配置的项目属性)来广播这些跟踪。然后你的跟踪侦听器才能听到它们。:)


天啊 - 我刚花了一天半的时间抓狂在这件事上 - 真是太棒了!!! - viperguynaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接