【问题标题】:.Net Custom TraceListener.TraceEvent not firing.Net 自定义 TraceListener.TraceEvent 未触发
【发布时间】:2013-01-25 18:18:02
【问题描述】:

我们有一个自定义 TraceListener(继承自 System.Diagnostics.TraceListener),用于我们的 ASP.NET Web 应用程序日志记录。它一直很好 - 没有问题。然后它突然停止在我们的开发环境中工作(TraceListener.TraceEvent() 停止触发)。我们对它为什么停止工作感到困惑。我们真正对代码所做的唯一更改是添加了更多构建配置(Dev、Test、Stage、Prod)。之前只有Debug和Release。

我注意到,当我使用 Debug 配置在本地进行测试时,TraceListener.TraceEvent() 被触发得很好。当我切换到另一个构建配置(即测试)时,TraceEvent() 将不再被触发。这是我的网络 .csproj 文件的 sn-p:

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

我不确定为什么切换构建配置似乎会关闭我们的日志记录。谁能指出我正确的方向?

【问题讨论】:

    标签: c# asp.net logging msbuild tracelistener


    【解决方案1】:

    解决了这个问题。我调用 System.Diagnostics.TraceSource.TraceEvent() 来广播跟踪事件的代码位于 another 项目中。我需要通过以下方式启用对 THAT 项目的跟踪:

    <DefineConstants>TRACE</DefineConstants>
    

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

    【讨论】:

    • 天啊 - 我刚刚花了一天半的时间把头发拉出来 - 很棒的发现!!!
    猜你喜欢
    • 1970-01-01
    • 2019-09-09
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2018-05-31
    • 2020-07-23
    相关资源
    最近更新 更多