dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

System.Diagnostics.TextWriterTraceListener(string fileName) not implemented: does not write anything to the file #28901

Closed blumu closed 4 years ago

blumu commented 5 years ago

The constructor System.Diagnostics.TextWriterTraceListener(string fileName) is not properly implemented. The filename gets stored in a class field (_fileName) but nothing gets ever written to the file.

Repro (F#)

use fileTracer = new System.Diagnostics.TextWriterTraceListener(@"C:\temp\test.log", Name = "foo")
fileTracer.WriteLine("test1")
System.Diagnostics.Trace.Listeners.Add(fileTracer)
System.Diagnostics.Trace.WriteLine("test2")
fileTracer.Flush()

Observed

File C:\temp\test.log is not present on disk.

Expected File

File C:\temp\test.log should be created on disk and contain two lines: "test1" and "test2". Or at least the method should throw an exception, or at the very least the source code and documention should indicate that this API is not fully supported on dotnet core.

See source at: https://github.com/dotnet/corefx/blob/43760cebc9b2773c0959629a443a28111d8b7de8/src/System.Diagnostics.TextWriterTraceListener/src/System/Diagnostics/TextWriterTraceListener.cs#L77

Known work around

First create a FileStream and instantiate the TraceListener using the constructor that takes a Stream instead of a filename:

use logStream = new System.IO.FileStream(@"C:\tempt\test.log", System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
use fileTracer = new System.Diagnostics.TextWriterTraceListener(logStream, Name = "foo", TraceOutputOptions = System.Diagnostics.TraceOptions.DateTime)
fileTracer.WriteLine("test 1")
System.Diagnostics.Trace.Listeners.Add(fileTracer)
System.Diagnostics.Trace.WriteLine("test2")
fileTracer.Flush()
stephentoub commented 5 years ago

Thanks. This was fixed in https://github.com/dotnet/corefx/pull/29234.

blumu commented 5 years ago

@stephentoub Any idea when the nuget with will be released? The latest package at nuget.org is 4.3.0 and dates back from 2016 (https://www.nuget.org/packages/System.Diagnostics.TextWriterTraceListener/4.3.0). Shall I be using a different nuget instead?

danmoseley commented 5 years ago

@blumu are you using .NET Core? It should be in version 2.2

blumu commented 5 years ago

@danmosemsft This is for github project FSharpLu (https://github.com/Microsoft/fsharplu/blob/master/FSharpLu/FSharpLu.fsproj). It's a library project so it's not targetting any particular version of netcore. It currently targets frameworks netstandard2.0 in addition to net452, net461. net462 and net472.

The package System.Diagnostics.TextWriterTraceListener is referenced as follows in the project file:

 <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    ...
    <PackageReference Include="System.Diagnostics.Tracing" Version="4.3.0" />
    <PackageReference Include="System.Diagnostics.TraceSource" Version="4.3.0" />
    <PackageReference Include="System.Diagnostics.TextWriterTraceListener" Version="4.3.0" />
    ...
 </ItemGroup>

After reading your comments I tried removing those package dependencies and I can confirm that the project builds just fines. I'm guessing that it's a sufficient fix that will guarantee that any top-level project depending on FSharpLu.dll that is targetting at least dotnetcore 2.2 will have the right implementation of System.Diagnostics.TextWriterTraceListener with the fix from dotnet/corefx#29234?