.NET Core F#项目不会写入TraceSource

时间:2017-08-12 18:05:32

标签: f# .net-core

我无法让我的F#项目写入TraceSource。

这是我的F#代码:

use logFileStream = new FileStream("log.txt", FileMode.Append, FileAccess.Write)
(
    use logFileListener = new TextWriterTraceListener(logFileStream)
    (
        let traceSource = TraceSource("Test", SourceLevels.All)
        traceSource.Listeners.Add(logFileListener) |> ignore
        traceSource.TraceEvent(TraceEventType.Verbose, 1, "This is a test.")
    )
)

这是我的F#项目:

<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="FSharp.Core" Version="4.1.*" />
    <PackageReference Include="FSharp.NET.Sdk" Version="1.0.*" PrivateAssets="All" />
    <PackageReference Include="System.Diagnostics.TextWriterTraceListener" Version="4.3.0" />
    <PackageReference Include="System.Diagnostics.Tracing" Version="4.3.0" />
  </ItemGroup>
</Project>

为什么log.txt为空?

1 个答案:

答案 0 :(得分:4)

我需要将<DefineConstants>TRACE</DefineConstants>添加到项目文件中的PropertyGroup。

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>netcoreapp1.1</TargetFramework>
  <DefineConstants>TRACE</DefineConstants>
</PropertyGroup>