我使用 Tow 方法将我的应用程序(Windows 应用程序)记录到文件和控制台屏幕
public static void InitLogFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Create);
Trace.Listeners.Add(new TextWriterTraceListener(fs));
Trace.AutoFlush = true;
}
public static void InitConsole()
{
Trace.Listeners.Add(new ConsoleTraceListener());
Trace.AutoFlush = true;
}
当我的 Windows 应用程序启动时,我使用此代码启动控制台屏幕
[DllImport("kernel32.dll",EntryPoint = "GetStdHandle",SetLastError = true,CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll",EntryPoint = "AllocConsole",SetLastError = true,CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
void StartConsole()
{
AllocConsole();
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
var safeFileHandle = new SafeFileHandle(stdHandle, true);
var fileStream = new FileStream(safeFileHandle, FileAccess.Write);
var encoding = Encoding.GetEncoding(MY_CODE_PAGE);
var standardOutput = new StreamWriter(fileStream, encoding) {AutoFlush = true};
Console.SetOut(standardOutput);
}
我的问题是:
我将我的 Windows 应用程序转换windows service
为控制台日志屏幕不工作现在不出现如何让它工作?