can't understand .net 2010 tracing and app.config

半世苍凉 提交于 2019-11-30 21:24:17
wageoghe

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!