.net 将跟踪输出重定向到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/198322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:18:47  来源:igfitidea点击:

Redirect Trace output to Console

.netvb.net.net-2.0consoletrace

提问by Joel Coehoorn

Let's say I'm working on a little batch-processing console app in VB.Net. I want to be able to structure the app like this:

假设我正在 VB.Net 中开发一个小的批处理控制台应用程序。我希望能够像这样构建应用程序:

Sub WorkerMethod()
   'Do some work
   Trace.WriteLine("Work progress")

   'Do more work
   Trace.WriteLine("Another progress update")

   '...
End Sub


Sub Main()

   'Do any setup, like confirm the user wants to continue or whatever

   WorkerMethod()     

End Sub

Note that I'm using Tracerather than Consolefor my output. This is because the worker method may be called from elsewhere, or even live in a different assembly, and I want to be able to attach different trace listeners to it. So how can I connect the console to the trace?

请注意,我正在使用Trace而不是Console用于我的输出。这是因为 worker 方法可能会从其他地方调用,甚至可能存在于不同的程序集中,我希望能够将不同的跟踪侦听器附加到它。那么如何将控制台连接到跟踪?

I can already do it by defining a simple class (shown below) and adding an instance to the Trace's listeners collection, but I'm wondering if there's a more accepted or built in way to accomplish this:

我已经可以通过定义一个简单的类(如下所示)并将一个实例添加到 Trace 的 listeners 集合中来做到这一点,但我想知道是否有一种更被接受或内置的方法来实现这一点:

Public Class ConsoleTrace
    Inherits Diagnostics.TraceListener

    Public Overloads Overrides Sub Write(ByVal message As String)
        Console.Write(message)
    End Sub

    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        Console.WriteLine(message)
    End Sub
End Class

回答by harpo

You can add the following to your exe's .config file.

您可以将以下内容添加到 exe 的 .config 文件中。

<?xml version="1.0"?>
<configuration>
    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add name="logListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="cat.log" />
                <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener"/>
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

I included the TextWriter as well, in case you're interested in logging to a file.

我还包括了 TextWriter,以防您对登录文件感兴趣。

回答by Scott P

Joel,

乔尔

You could do this instead of the app config method:

您可以这样做而不是应用程序配置方法:

Trace.Listeners.Add(new ConsoleTraceListener());

or this, if you want to manage adding or removing the listener during the life of the app:

或者,如果您想在应用程序的生命周期内管理添加或删除侦听器:

ConsoleTraceListener listener = new ConsoleTraceListener();
Trace.Listeners.Add(listener);

Trace.WriteLine("Howdy");

Trace.Listeners.Remove(listener);

Trace.Close();

回答by Scott Marlowe

Great solution, but I have a situation where I have different dll's being run by the same calling exe, so I don't want to modify the calling exe's .config file. I want each dll to handle it's own alteration of the trace output.

很好的解决方案,但我有一个情况,我有不同的 dll 由同一个调用 exe 运行,所以我不想修改调用 exe 的 .config 文件。我希望每个 dll 处理它自己对跟踪输出的更改。

Easy enough:

足够简单:

Stream outResultsFile = File.Create ("output.txt");
var textListener = new TextWriterTraceListener (outResultsFile);
Trace.Listeners.Add (textListener);

This will, of course, output Trace output to the "output.txt" file.

当然,这会将 Trace 输出输出到“output.txt”文件。