I tried doing this
using System;
using System.Diagnostics;
using System.IO;
public class Sample
{
public static void Main()
{
Stream traceText = File.Create("trace.txt");
TextWriterTraceListener textListener = new TextWriterTraceListener(traceText);
Trace.Listeners.Add(textListener);
Trace.Write("wth is goin on? I should be appearin in a txt file :(");
Trace.Flush();
}
}
and compiled with cmd, but the program just creates an empty trace.txt file.. why?
btw I'm using the latest .NET Framework version and I'm using notepad++ not VS
Most Trace
class methods (such as Write
) are marked with [Conditional("TRACE")]
attribute, which means calls to them are stripped off by compiler unless corresponding constant is defined.
This constant is usually set by default when you create project in visual studio, but since you compile yourself from command line - you need to define it yourself.
For that, either put
#define TRACE
as the very first line in your code file, or pass /d:TRACE
option to compiler when you compile from command line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.