无法理解 .net 2010 跟踪和 app.config
在我的 app.config 中,我想设置 3 个跟踪级别(开关?):详细、警告和无。 在代码的调试版本中,我希望详细开关处于活动状态,在发布版本中我希望发出警告。在特殊情况下,我的应用程序用户可以修改配置文件以禁用所有跟踪。
我希望调试跟踪输出在控制台上,而发布跟踪仅输出到日志文件。
我写了以下内容:
[...]
<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>
[...]
然后在代码中我这样调用跟踪:
Public Shared Sub HandleException(ByVal ex As Exception)
Trace.WriteLine(ex.Message, "Error")
[...]
我认为我缺少一些东西。我该如何告诉 Trace 方法使用正确的开关?我的应用程序用户如何更改配置文件以允许跟踪或禁用它?
谢谢。
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>
[...]
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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎将通过 Trace.Write 和 Trace.WriteLine 进行日志记录/跟踪的概念与使用 TraceSource 对象进行日志记录/跟踪的概念混合在一起。
TraceSource 对象允许您拥有可单独控制(通过开关)的“日志记录对象”,这样您就可以为某些代码打开日志记录,并为代码的其他部分关闭日志记录。 TraceSource 对象的输出可以配置为转到不同的 TraceListener(或相同的 TraceListener)。 Trace.WriteLine 并不是非常灵活。它只能配置为一个级别(即全局上您可以在“调试”或“信息”或其他位置进行日志记录),而使用 TraceSources,一个 TraceSource 可以在“调试”处进行日志记录,另一个可以在“信息”处进行日志记录,而另一个可以完全关闭。
请参阅这些链接中我的答案,了解如何配置 TraceSource 以及如何在代码中使用它们的一些示例。
如何跨类使用 TraceSource
通过 app.config 关闭跟踪
最好的日志记录方法是什么?
将跟踪方法添加到 System.Diagnostics.TraceListener
关于您希望跟踪/日志记录在调试与调试中如何工作发布后,您可以有两个不同的 app.config 文件。两者都定义相同的 TraceSource(即同一组“命名”跟踪/日志记录对象)。在与调试版本一起使用的 app.config 中,您可以将跟踪/日志记录级别设置为一个值 Debug/Info/Whatever,并且您可以将输出定向到控制台和/或文件和/或其他内容。在与调试版本一起使用的 app.config 中,您可以将跟踪/日志记录级别设置为不同的值(或关闭)并将输出定向到文件。
在上面的两篇文章中,我都包含了有关 System.Diagnostics 信息的其他几个链接,包括 Ukadc.Diagnostics项目。该项目提供了一个非常有趣的格式化功能,可与基于 System.Diagnostics 的 TraceListener 一起使用(前提是侦听器来自 Ukadc.Diagnostics),而无需对实际跟踪/日志记录语句进行任何更改。格式化功能与 log4net 和 NLog 提供的类似。
阅读我上面链接的信息,看看是否有帮助。尝试使用 TraceSources 而不仅仅是 Trace.WriteLine。当您对此感到满意时,也许可以查看 Ukadc.Diagnostics。
You seem to be mixing the concept of logging/tracing via Trace.Write and Trace.WriteLine with logging/tracing using TraceSource objects.
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.
See my answers in these links for some examples of how to configure TraceSources and how to use them in code.
How to use TraceSource across classes
Turning tracing off via app.config
What's the best approach to logging?
Add Trace methods to System.Diagnostics.TraceListener
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.
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.
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.