无法理解.NET 2010跟踪和的app.configNET、config、app

2023-09-03 02:46:20 作者:時光帶走的人莪不稀罕

在我的app.config我要设置3跟踪级别(开关):冗长,预警和没有。 在的code中的调试版本,我想详细开关被激活,在释放我想警告。在特殊情况下,我的应用程序的用户可以通过修改配置文件来禁用所有痕迹。

In my app.config I want to set 3 tracing levels (switches?): verbose, warning and none. In the debug version of the code, I want the verbose switch to be active, in the release I want warning. In special cases my application users can modify the config file to disable all traces.

我想调试跟踪到控制台上的输出,同时释放仅跟踪到日志文件。

I want debug traces to output on the console, while release traces only to a log file.

我,已经写了以下内容:

I',ve written the following:

[...]
<system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
          <source name="debug" switchName="debug">
            <listeners>
              <add name="FileLog"/>
              <add name="console"/>
            </listeners>
          </source>

          <source name="release" switchName="release">
            <listeners>
              <add name="FileLog"/>
            </listeners>
          </source>

          <source name="silent" switchName="none">
            <listeners/>
          </source>
        </sources>


        <switches>
            <add name="debug" value="Verbose"/>
            <add name="release" value="Warning"/>
            <add name="none" value="Off"/>
        </switches>


        <!--<sharedListeners>
            <add name="FileLog" type="System.Diagnostics.TextWriterTraceListener"  traceOutputOptions="DateTime" initializeData="felix.log"/>
            <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false" />
        </sharedListeners>-->

        <trace autoflush="false" indentsize="4">
          <listeners>
              <add name="FileLog" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="DateTime" initializeData="felix.log"/>
              <add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
              <remove name="Default"/>
          </listeners>
        </trace>

    </system.diagnostics>
[...]

然后在code我呼叫跟踪是这样的:

Then in code I call trace like this:

Public Shared Sub HandleException(ByVal ex As Exception)
   Trace.WriteLine(ex.Message, "Error")

[...]

[...]

有我丢失的东西,我认为。我如何来跟踪方法的正确开关使用说?如何让我的应用程序的用户更改配置文件,以便跟踪或禁用呢?

There's something I'm missing I think. How do I say to the Trace method the right switch to use?? How can my application users change the config file to allow for tracing or disable it?

感谢。

推荐答案

您似乎混合记录的概念/通过Trace.Write和Trace.WriteLine日志记录跟踪/使用TraceSource对象跟踪。

You seem to be mixing the concept of logging/tracing via Trace.Write and Trace.WriteLine with logging/tracing using TraceSource objects.

TraceSource对象,让你有单独控制的(通过交换机)记录对象,这样你可以把记录了一段你的code和关闭你的code等部位。从TraceSource对象的输出可以被配置去不同TraceListeners(或同一的TraceListener)。 Trace.WriteLine是不是真的很灵活。它只能与一个级别上进行配置(即全球范围内,你可以登录在调试或信息或其他),而与TraceSources,人们TraceSource可以记录在调试和另外一个可以记录在信息,而另外一个可完全关闭。

TraceSource objects allow you to have individually controllable (via switches) "logging objects" such that you can turn logging on for some of your code and off for other parts of your code. The output from TraceSource objects can be configured to go to different TraceListeners (or to the same TraceListener). Trace.WriteLine is not really very flexible. It can be configured with only one level (i.e. globally you can log at Debug or Info or whatever), whereas with TraceSources, one TraceSource could be logging at Debug and another one could be logging at Info while another one could be completely Off.

请参阅我的答案,在这些链接中如何配置TraceSources以及如何使用它们在code一些例子。

See my answers in these links for some examples of how to configure TraceSources and how to use them in code.

How使用TraceSource跨类

Turning通过的app.config

[VB.net]什么是日志记录的最佳方法?

Add跟踪方法,以System.Diagnostics.TraceListener

至于你怎么想你的跟踪/日志在调试工作VS版本中,可以有两个不同的app.config文件。双方将定义相同TraceSources(即同一组命名的跟踪/日志对象)。在App.config使用与调试版本,您可以设置跟踪/日志记录级别到一个值调试/信息/不管,你可能会直接输出到控制台和/或文件和/或什么的。在App.config要与调试使用的版本,您可以直接输出到一个文件中设置跟踪/日志记录级别为不同的值(或关),一个第二。

Regarding how you want your tracing/logging to work in debug vs release, you can have two different app.config files. Both would define the same TraceSources (i.e. the same set of "named" tracing/logging objects). In the app.config to be used with debug builds, you might set the tracing/logging level to one value Debug/Info/Whatever and you might direct the output to the Console and/or a File and/or whatever. In the app.config to be used with debug builds, you might set the tracing/logging level to a different value (or Off)a nd direct the output to a File.

在两个以上我的职位包括几个环节,以信息System.Diagnostics程序,包括 Ukadc.Diagnostics 项目。此项目为使用与您的实际跟踪/日志报表没有改变一个非常有趣的格式化功能基于System.Diagnostics程序TraceListeners(提供的听众来自Ukadc.Diagnostics)。格式化能力是类似于由log4net的和NLOG提供

In both of the posts above I include several other links to information on System.Diagnostics, including the Ukadc.Diagnostics project. This project provides a very interesting formatting capability for use with System.Diagnostics based TraceListeners (provided the listeners come from Ukadc.Diagnostics) with NO changes in your actual tracing/logging statements. The formatting capability is similar to that provided by log4net and NLog.

请阅读我上面链接的信息,并看看是否有帮助。尝试使用TraceSources而不仅仅是Trace.WriteLine。如果你是舒服,也许看看Ukadc.Diagnostics。

Read the information that I linked above and see if it helps. Try using TraceSources rather than just Trace.WriteLine. When you are comfortable with that, maybe look into Ukadc.Diagnostics.